fsanyoto commited on
Commit
ecc3360
·
verified ·
1 Parent(s): afe5f37

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. RELEASES.json +1 -1
  2. VERSION +1 -1
  3. api/deps.py +60 -4
  4. api/loopy_catalog_tools.py +10 -1
  5. api/main.py +120 -14
  6. api/routes_account_health.py +27 -10
  7. api/routes_admin.py +1 -172
  8. api/routes_amazon_projects.py +160 -21
  9. api/routes_connectors.py +110 -9
  10. api/routes_external.py +119 -10
  11. api/routes_odoo_tables.py +56 -12
  12. api/sync_worker.py +4 -2
  13. api/test_odoo_rank_refresh.py +3 -3
  14. api/tool_groups/apps.py +9 -1
  15. platform/core/activity_log.py +7 -6
  16. platform/core/amazon_project_results.py +116 -1
  17. platform/core/amazon_projects.py +3 -2
  18. platform/core/connector_sync.py +28 -2
  19. platform/core/external_access.py +77 -27
  20. platform/core/table_store.py +49 -30
  21. platform/core/user_tables.py +2 -0
  22. platform/core/users.py +2 -112
  23. platform/core/windows.py +4 -1
  24. platform/harness/amazon_client.py +43 -5
  25. platform/harness/amazon_store.py +384 -67
  26. platform/modules/account_health.py +161 -7
  27. platform/modules/amazon_profit.py +210 -21
  28. platform/modules/procurement.py +27 -10
  29. platform/modules/returns_refunds.py +80 -31
  30. platform/modules/warehouse.py +8 -2
  31. web/public/AIOS_DEPLOY_RECEIPT +1 -1
  32. web/src/apps/AppRail.tsx +3 -1
  33. web/src/apps/account-health/AppView.tsx +175 -10
  34. web/src/apps/amazon-profit/AppView.tsx +47 -22
  35. web/src/apps/amazon-profit/ProjectResults.tsx +122 -137
  36. web/src/apps/amazon-profit/Projects.tsx +332 -135
  37. web/src/connectors/ConnectorsPage.tsx +139 -32
  38. web/src/connectors/connectors.css +15 -0
  39. web/src/customer-grid/AmazonPeriodPicker.tsx +2 -4
  40. web/src/customer-grid/CustomerGrid.tsx +16 -3
  41. web/src/customer-grid/cells.ts +13 -3
  42. web/src/customer-grid/windows.ts +3 -1
  43. web/src/settings/ActivityLogPage.tsx +200 -156
  44. web/src/settings/ExternalAccessPane.tsx +84 -40
  45. web/src/settings/NotifyDestinations.tsx +170 -79
  46. web/src/settings/SecurityPane.tsx +233 -154
  47. web/src/settings/SettingsModal.tsx +16 -449
  48. web/src/settings/activityLog.css +114 -36
  49. web/src/settings/activityLog.ts +85 -0
  50. web/src/settings/channels/channels.css +124 -0
RELEASES.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "current": "8538d73390",
3
  "releases": []
4
  }
 
1
  {
2
+ "current": "659eeb316",
3
  "releases": []
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- 8538d73390
 
1
+ 659eeb316
api/deps.py CHANGED
@@ -1809,17 +1809,62 @@ def _touch_active(uname):
1809
  pass
1810
 
1811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1812
  def require_session(request: Request, response: Response) -> Session:
1813
  """Verify the cookie, resolve the tenant, and REISSUE the cookie (the 8h idle window is
1814
  refreshed on use; the 30-day absolute expiry is carried over, never extended)."""
 
 
1815
  raw = request.cookies.get(aios_session.COOKIE_NAME)
1816
  if not raw:
 
 
1817
  raise err(401, "no_session", "sign in to continue")
1818
  claims = aios_session.read(raw)
1819
  if not claims:
 
 
1820
  raise err(401, "invalid_session", "Your session ended. Sign in again.")
1821
  user = _user_for(claims)
1822
  if not user:
 
 
1823
  raise err(401, "invalid_session", "Your session ended. Sign in again.")
1824
  identity = {_TENANT_HEADER: str(claims["t"]),
1825
  _PRINCIPAL_HEADER: _principal(claims["t"], user["username"])}
@@ -1835,8 +1880,12 @@ def require_session(request: Request, response: Response) -> Session:
1835
  except KeyError:
1836
  # An unknown tenant slug is a 401, not a 404: the cookie is not a valid identity for this
1837
  # deployment, and confirming which slugs exist would answer a question we were not asked.
 
 
1838
  raise err(401, "invalid_session", "Your session ended. Sign in again.")
1839
- fresh, _ = aios_session.mint(claims["t"], user["username"], int(user.get("epoch") or 0),
 
 
1840
  absolute_expiry=claims["x"])
1841
  aios_session.set_cookie(response, request, fresh)
1842
  # ⭐ STAMP THE TENANT THIS ANSWER WAS SERVED FOR (owner report 2026-08-09: "I can see
@@ -1857,11 +1906,13 @@ def require_session(request: Request, response: Response) -> Session:
1857
  # `session.tenant` read back from something the request could influence.
1858
  response.headers[_TENANT_HEADER] = str(claims["t"])
1859
  response.headers[_PRINCIPAL_HEADER] = _principal(claims["t"], user["username"])
1860
- # Observe only AFTER authentication admission. Read methods stay store-read-only; only an
1861
- # admitted mutation can launch the persisted stamp, without moving it outside request timing.
 
 
1862
  # qa-runner has no global account record. Never let the normal activity telemetry create or
1863
  # touch one through the default tenant-#0 users store.
1864
- if (not user.get("qa_runner")
1865
  and not (request.method == "GET" and request.url.path == "/api/v1/notifications")):
1866
  _observe_active(claims["t"], user.get("username"))
1867
  if request.method not in {"GET", "HEAD", "OPTIONS"}:
@@ -1877,6 +1928,8 @@ def require_changes_session(request: Request, response: Response) -> Session | N
1877
  verified signed cookie is enough to return that no-data response; resolving the account record
1878
  would itself turn those legacy requests into a permanent Neon keepalive.
1879
  """
 
 
1880
  if store.backend() != "pg":
1881
  return require_session(request, response)
1882
  if request.headers.get("x-aios-change-check") == "focus-v1":
@@ -1918,6 +1971,9 @@ def require_session_or_bearer(request: Request, response: Response) -> Session:
1918
 
1919
  `require_session` stays cookie only. A bearer session never reissues the cookie.
1920
  """
 
 
 
1921
  bearer = _bearer_header(request)
1922
  if bearer is None or request.cookies.get(aios_session.COOKIE_NAME):
1923
  try:
 
1809
  pass
1810
 
1811
 
1812
+ def refuse_view_as_write(request: Request):
1813
+ """Refuse every view-as method except GET and HEAD before any write."""
1814
+ if "X-View-As" in request.headers and request.method not in {"GET", "HEAD"}:
1815
+ raise err(403, "view_as_read_only", "Viewing as a member is read-only.")
1816
+
1817
+
1818
+ def _view_as_refused():
1819
+ return err(403, "view_as_refused", "This member view is unavailable.")
1820
+
1821
+
1822
+ def _view_as_user(request, actor, tenant, rt):
1823
+ """Resolve an active member of the actor's tenant and audit this request once."""
1824
+ if "X-View-As" not in request.headers:
1825
+ return actor
1826
+ if not perms.is_admin(actor):
1827
+ raise _view_as_refused()
1828
+ started_at = datetime.now(timezone.utc).isoformat()
1829
+ target = request.headers["X-View-As"].strip().lower()
1830
+ if not target:
1831
+ raise _view_as_refused()
1832
+ rec = copy.deepcopy(_session_registry(target).get(target))
1833
+ if (not rec or not rec.get("active", True) or rec.get("role", "user") != "user"
1834
+ or str(rec.get("tenant") or "").strip().lower() != tenant):
1835
+ raise _view_as_refused()
1836
+ member = users._public(target, rec)
1837
+ audit_key = (tenant, actor["username"], target)
1838
+ if getattr(request.state, "_view_as_audited", None) != audit_key:
1839
+ from core import activity_log
1840
+ activity_log.append(
1841
+ rt, kind="view_as", object={"app": "settings", "id": target},
1842
+ by=actor["username"], on_behalf_of=target,
1843
+ changes={"member": {"before": None, "after": target},
1844
+ "started_at": {"before": None, "after": started_at}})
1845
+ request.state._view_as_audited = audit_key
1846
+ return member
1847
+
1848
+
1849
  def require_session(request: Request, response: Response) -> Session:
1850
  """Verify the cookie, resolve the tenant, and REISSUE the cookie (the 8h idle window is
1851
  refreshed on use; the 30-day absolute expiry is carried over, never extended)."""
1852
+ refuse_view_as_write(request)
1853
+ viewing_as = "X-View-As" in request.headers
1854
  raw = request.cookies.get(aios_session.COOKIE_NAME)
1855
  if not raw:
1856
+ if viewing_as:
1857
+ raise _view_as_refused()
1858
  raise err(401, "no_session", "sign in to continue")
1859
  claims = aios_session.read(raw)
1860
  if not claims:
1861
+ if viewing_as:
1862
+ raise _view_as_refused()
1863
  raise err(401, "invalid_session", "Your session ended. Sign in again.")
1864
  user = _user_for(claims)
1865
  if not user:
1866
+ if viewing_as:
1867
+ raise _view_as_refused()
1868
  raise err(401, "invalid_session", "Your session ended. Sign in again.")
1869
  identity = {_TENANT_HEADER: str(claims["t"]),
1870
  _PRINCIPAL_HEADER: _principal(claims["t"], user["username"])}
 
1880
  except KeyError:
1881
  # An unknown tenant slug is a 401, not a 404: the cookie is not a valid identity for this
1882
  # deployment, and confirming which slugs exist would answer a question we were not asked.
1883
+ if viewing_as:
1884
+ raise _view_as_refused()
1885
  raise err(401, "invalid_session", "Your session ended. Sign in again.")
1886
+ actor = user
1887
+ user = _view_as_user(request, actor, claims["t"], rt)
1888
+ fresh, _ = aios_session.mint(claims["t"], actor["username"], int(actor.get("epoch") or 0),
1889
  absolute_expiry=claims["x"])
1890
  aios_session.set_cookie(response, request, fresh)
1891
  # ⭐ STAMP THE TENANT THIS ANSWER WAS SERVED FOR (owner report 2026-08-09: "I can see
 
1906
  # `session.tenant` read back from something the request could influence.
1907
  response.headers[_TENANT_HEADER] = str(claims["t"])
1908
  response.headers[_PRINCIPAL_HEADER] = _principal(claims["t"], user["username"])
1909
+ if viewing_as:
1910
+ response.headers["X-Viewing-As"] = user["username"]
1911
+ # Ordinary reads only observe activity. View-as reads write their audit without
1912
+ # changing either person's activity stamps.
1913
  # qa-runner has no global account record. Never let the normal activity telemetry create or
1914
  # touch one through the default tenant-#0 users store.
1915
+ if (not viewing_as and not user.get("qa_runner")
1916
  and not (request.method == "GET" and request.url.path == "/api/v1/notifications")):
1917
  _observe_active(claims["t"], user.get("username"))
1918
  if request.method not in {"GET", "HEAD", "OPTIONS"}:
 
1928
  verified signed cookie is enough to return that no-data response; resolving the account record
1929
  would itself turn those legacy requests into a permanent Neon keepalive.
1930
  """
1931
+ if "X-View-As" in request.headers:
1932
+ return require_session(request, response)
1933
  if store.backend() != "pg":
1934
  return require_session(request, response)
1935
  if request.headers.get("x-aios-change-check") == "focus-v1":
 
1971
 
1972
  `require_session` stays cookie only. A bearer session never reissues the cookie.
1973
  """
1974
+ refuse_view_as_write(request)
1975
+ if "X-View-As" in request.headers:
1976
+ return _cookie_session(request, response)
1977
  bearer = _bearer_header(request)
1978
  if bearer is None or request.cookies.get(aios_session.COOKIE_NAME):
1979
  try:
api/loopy_catalog_tools.py CHANGED
@@ -305,8 +305,10 @@ def catalog_pdf_within_budget(session, args, render=None, budget=None):
305
  import contextvars
306
  import sys
307
  import threading
 
308
  import catalog_store
309
  import app_templates
 
310
 
311
  render = render or sys.modules[__name__].catalog_pdf
312
  budget = export_budget() if budget is None else budget
@@ -329,8 +331,15 @@ def catalog_pdf_within_budget(session, args, render=None, budget=None):
329
  import logging
330
  logging.getLogger(__name__).warning("Catalog export result not saved (%s)", type(exc).__name__)
331
 
 
 
 
 
 
 
 
332
  context = contextvars.copy_context()
333
- worker = threading.Thread(target=context.run, args=(work,), daemon=True, name="catalog-pdf")
334
  try:
335
  worker.start()
336
  except Exception:
 
305
  import contextvars
306
  import sys
307
  import threading
308
+ import time
309
  import catalog_store
310
  import app_templates
311
+ from core import request_timing
312
 
313
  render = render or sys.modules[__name__].catalog_pdf
314
  budget = export_budget() if budget is None else budget
 
331
  import logging
332
  logging.getLogger(__name__).warning("Catalog export result not saved (%s)", type(exc).__name__)
333
 
334
+ def run():
335
+ # HTTP completion cancels its timing state, so the export needs its own bounded scope.
336
+ with request_timing.request_scope() as measured:
337
+ measured.deadline = time.monotonic() + catalog_store.EXPORT_STALE_SECONDS
338
+ with request_timing.thread_scope():
339
+ work()
340
+
341
  context = contextvars.copy_context()
342
+ worker = threading.Thread(target=context.run, args=(run,), daemon=True, name="catalog-pdf")
343
  try:
344
  worker.start()
345
  except Exception:
api/main.py CHANGED
@@ -1662,16 +1662,17 @@ def _sync_now_detail(provider, tenant, *, ok, detail="", cause=None):
1662
  return sentence + suffix
1663
 
1664
 
1665
- def _run_sync_requests(outcome=None, *, during_odoo=False, hold_first_load=None):
1666
- """Drain Sync now intents on the resident resync thread, the mirror's single writer.
1667
 
1668
- D-29 / intake F6: HTTP handlers only queue intents. Provider calls stay here, before the
1669
- resident projection pass. A failed request cannot discard another tenant's queued work.
1670
  `outcome`, when given, receives `{provider: "continuing" | "done" | "failed"}` so the loop can
1671
  project only what a desk wake actually changed.
1672
  """
1673
  from core import connector_sync
1674
- _drain_pending_amazon_revocations()
 
1675
 
1676
  def progress_log(provider, tenant, receipt, rt):
1677
  """Publish only authored progress shapes, with one bounded detail per running receipt."""
@@ -1772,7 +1773,11 @@ def _run_sync_requests(outcome=None, *, during_odoo=False, hold_first_load=None)
1772
  # A first-load continuation never runs inside an Odoo pass: it requeues itself without end.
1773
  hold = during_odoo if hold_first_load is None else hold_first_load
1774
  # Claim only work this turn executes. A later provider remains queued with an honest wait.
1775
- pending = connector_sync.take_sync_requests(limit=1, exclude_providers=("odoo",) if during_odoo else (),
 
 
 
 
1776
  refresh_remote=True,
1777
  exclude_requested_by=("first load",) if hold else ())
1778
  while True:
@@ -2424,14 +2429,40 @@ def _rebuild_odoo_relational(why, previous_meta_fingerprint=None, meta_state_out
2424
  **({"cause": _raw_cause(key)} if _raw_cause(key) else {})}
2425
  for key, db_failure in blocking.items() if isinstance(db_failure, dict)]
2426
  if failed_databases is not None and table_keys is not None:
2427
- # D-2679: a changed-tables pass keeps the failures of databases it did not run.
2428
  try:
2429
  prior = (_rt.get(_rel.CONFIG_KEY) or {}).get("failedDatabases") or []
2430
  except Exception: # noqa: BLE001
2431
  prior = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2432
  failed_databases = [
2433
  item for item in prior if isinstance(item, dict)
2434
- and item.get("key") not in database_keys] + failed_databases
 
2435
  try:
2436
  _rel.record_sync_outcome(_rt, ok=not safe_errors, at=refreshed_at,
2437
  failed_databases=failed_databases)
@@ -3234,15 +3265,77 @@ def _sheet_warm_after_pass(tenant, good, generation_before, rebuilt):
3234
 
3235
  def _sync_idle_wait(event, seconds):
3236
  """The resync loop's wait. A `sync_worker.py --once` job exits here when it is idle (S82-T10)."""
 
 
 
3237
  import sync_runtime
3238
  return sync_runtime.idle_wait(event, seconds)
3239
 
3240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3241
  def _store_resync_loop():
3242
  """Keep the analytical mirror current — the API-process stand-in for the re-sync the
3243
  Streamlit app piggybacks on page renders. Measure memos key on the pool stamp, so a
3244
  refreshed pool re-reads the freshly synced store."""
3245
- if _freeze_holds("store resync"):
 
3246
  return
3247
  import time as _t
3248
  from core import connector_sync
@@ -3278,6 +3371,7 @@ def _store_resync_loop():
3278
  last_full_pass_at = _t.time()
3279
  # The least time between two desk-only passes of an Amazon first-load continuation.
3280
  desk_min_spacing_seconds = 30.0
 
3281
  # H73: the start of the last daily-slot pass, and whether the boot restore already ran.
3282
  last_slot_pass_at = _memory.get("slotPassAt")
3283
  slot_restored = False
@@ -3368,7 +3462,7 @@ def _store_resync_loop():
3368
  _slot = (globals().get("_resolve_resync_slot") or (lambda: None))()
3369
 
3370
  def _near_slot():
3371
- # A first-load continuation does not start this close to the next Odoo slot.
3372
  if _slot is None:
3373
  return False
3374
  import odoo_relational as _rel_near
@@ -3383,6 +3477,7 @@ def _store_resync_loop():
3383
  # END drifted later every day and past the 26-hour stale line).
3384
  _woken = bool(_idle_wait(connector_sync.sync_wake_event(), min(
3385
  max(0.0, _every - (_t.time() - last_full_pass_at)),
 
3386
  float("inf") if backfill_next_at is None else max(0.0, backfill_next_at - _t.time()))))
3387
  connector_sync.sync_wake_event().clear()
3388
  _slot_due = False
@@ -3396,6 +3491,7 @@ def _store_resync_loop():
3396
  _woken = bool(_idle_wait(connector_sync.sync_wake_event(), min(
3397
  0.0 if _rel_slot.slot_due(_slot_now, _slot, last_slot_pass_at)
3398
  else max(0.0, _rel_slot.slot_after(_slot_now, _slot) + 1.0 - _slot_now),
 
3399
  float("inf") if backfill_next_at is None else max(0.0, backfill_next_at - _slot_now))))
3400
  connector_sync.sync_wake_event().clear()
3401
  _slot_now = _t.time()
@@ -3428,6 +3524,11 @@ def _store_resync_loop():
3428
  f"{_slot['tz']} slot; cleared the slot_pass_interrupted failure")
3429
  else:
3430
  RESYNC_STATE.pop("nextPassAt", None)
 
 
 
 
 
3431
  # ⛔⛔ HOTFIX 2026-09-29 — A RESTART INSIDE THE DAILY WINDOW IS NOT A SYNC. The owner
3432
  # capped syncing at once a day, and a deploy is not a reason to sync again. So the first
3433
  # pass after a boot that lands within one cadence of the last scheduled pass only catches
@@ -3470,13 +3571,16 @@ def _store_resync_loop():
3470
  else:
3471
  _project_desk(_desk, _cad["tenant"])
3472
  if not slot_restored:
3473
- _pull_amazon("boot", False)
 
3474
  # The restored mirror warms the workbooks once; Sheet warm reads only the mirror.
3475
  _sheet_warm_after_pass(_cad["tenant"], True, None, True)
3476
  slot_restored = True
3477
  print(f"[aios-api] store resync: no Odoo pass outside the daily slot; the next one "
3478
  f"runs at {RESYNC_STATE['nextPassAt']} ({_slot['at']} {_slot['tz']})")
3479
  continue
 
 
3480
  passes += 1
3481
  backfill_next_at = None
3482
  if not _continuation:
@@ -3723,9 +3827,10 @@ def _store_resync_loop():
3723
  except Exception as e: # noqa: BLE001
3724
  print(f"[aios-api] meta relational refresh failed (resync): "
3725
  f"{type(e).__name__}: {e}")
3726
- if _resumed.get("amazon"):
 
3727
  _pull_amazon("boot", False)
3728
- else:
3729
  # The daily slot pass refreshes every Amazon table: a late extra pass yesterday must not defer it.
3730
  RESYNC_STATE["slotPass"] = _slot_claimed
3731
  try:
@@ -3739,7 +3844,8 @@ def _store_resync_loop():
3739
  try:
3740
  from harness import runtime as _runtime
3741
  _rt_resync = _runtime.get_runtime(_cad["tenant"])
3742
- _amzn_state = _rebuild_amazon_relational("resync", _rt_resync, _amzn_previous)
 
3743
  last_amzn_fingerprint = (_amzn_state["fingerprint"]
3744
  if _amzn_state.get("known") else None)
3745
  except Exception as e: # noqa: BLE001
 
1662
  return sentence + suffix
1663
 
1664
 
1665
+ def _run_sync_requests(outcome=None, *, during_odoo=False, hold_first_load=None, amazon_only=False):
1666
+ """Drain admitted Sync now intents before their resident projection.
1667
 
1668
+ HTTP handlers queue intents. The slot loop owns Odoo and Meta, and its detached turn owns Amazon.
1669
+ A failed request cannot discard another tenant's queued work.
1670
  `outcome`, when given, receives `{provider: "continuing" | "done" | "failed"}` so the loop can
1671
  project only what a desk wake actually changed.
1672
  """
1673
  from core import connector_sync
1674
+ if amazon_only or globals().get("_AMAZON_DESK", {}).get("job") is None:
1675
+ _drain_pending_amazon_revocations()
1676
 
1677
  def progress_log(provider, tenant, receipt, rt):
1678
  """Publish only authored progress shapes, with one bounded detail per running receipt."""
 
1773
  # A first-load continuation never runs inside an Odoo pass: it requeues itself without end.
1774
  hold = during_odoo if hold_first_load is None else hold_first_load
1775
  # Claim only work this turn executes. A later provider remains queued with an honest wait.
1776
+ excluded = ({"odoo", "meta_ads"} if amazon_only else
1777
+ ({"odoo"} if during_odoo else set()))
1778
+ if not amazon_only and globals().get("_AMAZON_DESK", {}).get("managed"):
1779
+ excluded.add("amazon_sp")
1780
+ pending = connector_sync.take_sync_requests(limit=1, exclude_providers=excluded,
1781
  refresh_remote=True,
1782
  exclude_requested_by=("first load",) if hold else ())
1783
  while True:
 
2429
  **({"cause": _raw_cause(key)} if _raw_cause(key) else {})}
2430
  for key, db_failure in blocking.items() if isinstance(db_failure, dict)]
2431
  if failed_databases is not None and table_keys is not None:
2432
+ # Unassessed failures survive unless their exact missing-binding refusal is disproved.
2433
  try:
2434
  prior = (_rt.get(_rel.CONFIG_KEY) or {}).get("failedDatabases") or []
2435
  except Exception: # noqa: BLE001
2436
  prior = []
2437
+ recovered_database_keys = set()
2438
+ for item in prior:
2439
+ if not isinstance(item, dict):
2440
+ continue
2441
+ key = item.get("key")
2442
+ if (key in database_keys or key in database_failures
2443
+ or item.get("code") != "materialization_failed"
2444
+ or item.get("stage") != "materialization"
2445
+ or item.get("cause") !=
2446
+ "Refused: the database has no readable mirror binding"
2447
+ or not _rel.is_read_through_key(key)):
2448
+ continue
2449
+ try:
2450
+ _base, owner = _rel.split_table_key(key)
2451
+ if owner is not None or not _ut.get_definition(key, st=_rt):
2452
+ continue
2453
+ if not any(_key == key and bucket in _rel.enabled_buckets(_rt)
2454
+ for bucket, _key, _label, _fields in _rel.TABLES):
2455
+ continue
2456
+ with _rt.mirror_handle().ro_cursor() as cur:
2457
+ nrows = routes_odoo_tables.population(key, cur=cur, rt=_rt)
2458
+ if isinstance(nrows, int) and nrows >= 0:
2459
+ recovered_database_keys.add(key)
2460
+ except Exception: # noqa: BLE001
2461
+ pass # A missing, filling or unreadable mirror cannot prove recovery.
2462
  failed_databases = [
2463
  item for item in prior if isinstance(item, dict)
2464
+ and item.get("key") not in database_keys
2465
+ and item.get("key") not in recovered_database_keys] + failed_databases
2466
  try:
2467
  _rel.record_sync_outcome(_rt, ok=not safe_errors, at=refreshed_at,
2468
  failed_databases=failed_databases)
 
3265
 
3266
  def _sync_idle_wait(event, seconds):
3267
  """The resync loop's wait. A `sync_worker.py --once` job exits here when it is idle (S82-T10)."""
3268
+ if globals().get("_AMAZON_DESK", {}).get("job") is not None:
3269
+ # An unclaimed or finishing desk turn still owns work, even between receipt states.
3270
+ return event.wait(seconds)
3271
  import sync_runtime
3272
  return sync_runtime.idle_wait(event, seconds)
3273
 
3274
 
3275
+ def _amazon_intraday_pass(tenant_key, previous_fingerprint):
3276
+ """Run due Amazon tables and their projection without advancing the Odoo pass clock."""
3277
+ if globals().get("_AMAZON_DESK", {}).get("job") is not None:
3278
+ return previous_fingerprint
3279
+ try:
3280
+ from harness import amazon_store, runtime
3281
+ rt = runtime.get_runtime(tenant_key)
3282
+ if not amazon_store.credential(tenant_key, rt=rt) or amazon_store.first_load_job_live(rt):
3283
+ return previous_fingerprint
3284
+ plan = amazon_store.scheduled_sync_plan(tenant_key, rt=rt)
3285
+ if not plan["tables"]:
3286
+ return previous_fingerprint
3287
+ print("[aios-api] amazon intraday pass due: " + ", ".join(plan["tables"]), flush=True)
3288
+ RESYNC_STATE["slotPass"] = False
3289
+ _pull_amazon("intraday")
3290
+ state = _rebuild_amazon_relational("intraday", rt, previous_fingerprint)
3291
+ return state["fingerprint"] if state.get("known") else None
3292
+ except Exception as exc:
3293
+ print(f"[aios-api] amazon intraday pass failed ({type(exc).__name__}); "
3294
+ "the next cadence check retries it", flush=True)
3295
+ return None
3296
+
3297
+
3298
+ def _amazon_desk_pass(tenant, *, project=True):
3299
+ """Run one Amazon desk claim off the slot clock and collect its projection after publication."""
3300
+ import process_role
3301
+ import time
3302
+ if not process_role.may_write():
3303
+ return False
3304
+ from core import connector_sync
3305
+ state = globals().setdefault("_AMAZON_DESK", {"managed": True, "job": None, "outcome": {}})
3306
+ job = state["job"]
3307
+ if job is not None:
3308
+ if job.is_alive() or not project:
3309
+ return True
3310
+ job.join()
3311
+ state["job"] = None
3312
+ _project_desk(state["outcome"], tenant)
3313
+ receipt = connector_sync.sync_request("amazon_sp", tenant)
3314
+ if (not receipt or receipt["state"] != "queued" or receipt.get("requestedBy") == "first load"
3315
+ or max(receipt.get("notBefore") or 0,
3316
+ connector_sync.sync_not_before("amazon_sp", tenant) or 0) > time.time()):
3317
+ return False
3318
+ outcome = state["outcome"] = {}
3319
+
3320
+ def drain():
3321
+ try:
3322
+ _run_sync_requests(outcome, hold_first_load=True, amazon_only=True)
3323
+ except Exception as exc:
3324
+ print(f"[aios-api] amazon desk turn failed ({type(exc).__name__})", flush=True)
3325
+ finally:
3326
+ connector_sync.sync_wake_event().set()
3327
+
3328
+ # This turn inherits store-resync's heavy admission, including tenant-worker scope.
3329
+ state["job"] = process_role.start("store-resync", "heavy", drain)
3330
+ return state["job"] is not None
3331
+
3332
+
3333
  def _store_resync_loop():
3334
  """Keep the analytical mirror current — the API-process stand-in for the re-sync the
3335
  Streamlit app piggybacks on page renders. Measure memos key on the pool stamp, so a
3336
  refreshed pool re-reads the freshly synced store."""
3337
+ import process_role as _loop_role
3338
+ if not _loop_role.may_write() or _freeze_holds("store resync"):
3339
  return
3340
  import time as _t
3341
  from core import connector_sync
 
3371
  last_full_pass_at = _t.time()
3372
  # The least time between two desk-only passes of an Amazon first-load continuation.
3373
  desk_min_spacing_seconds = 30.0
3374
+ amazon_check_seconds = 60.0
3375
  # H73: the start of the last daily-slot pass, and whether the boot restore already ran.
3376
  last_slot_pass_at = _memory.get("slotPassAt")
3377
  slot_restored = False
 
3462
  _slot = (globals().get("_resolve_resync_slot") or (lambda: None))()
3463
 
3464
  def _near_slot():
3465
+ # Completed desk projections wait inside the existing first-load slot quiet window.
3466
  if _slot is None:
3467
  return False
3468
  import odoo_relational as _rel_near
 
3477
  # END drifted later every day and past the 26-hour stale line).
3478
  _woken = bool(_idle_wait(connector_sync.sync_wake_event(), min(
3479
  max(0.0, _every - (_t.time() - last_full_pass_at)),
3480
+ amazon_check_seconds,
3481
  float("inf") if backfill_next_at is None else max(0.0, backfill_next_at - _t.time()))))
3482
  connector_sync.sync_wake_event().clear()
3483
  _slot_due = False
 
3491
  _woken = bool(_idle_wait(connector_sync.sync_wake_event(), min(
3492
  0.0 if _rel_slot.slot_due(_slot_now, _slot, last_slot_pass_at)
3493
  else max(0.0, _rel_slot.slot_after(_slot_now, _slot) + 1.0 - _slot_now),
3494
+ amazon_check_seconds,
3495
  float("inf") if backfill_next_at is None else max(0.0, backfill_next_at - _slot_now))))
3496
  connector_sync.sync_wake_event().clear()
3497
  _slot_now = _t.time()
 
3524
  f"{_slot['tz']} slot; cleared the slot_pass_interrupted failure")
3525
  else:
3526
  RESYNC_STATE.pop("nextPassAt", None)
3527
+ _cadence_due = not passes or _t.time() - last_full_pass_at >= _every
3528
+ _amazon_desk_busy = (_amazon_desk_pass(_cad["tenant"], project=not (_slot_due or _near_slot()))
3529
+ if _slot is not None or globals().get("_AMAZON_DESK") else False)
3530
+ if (not _slot_due if _slot is not None else not _cadence_due):
3531
+ last_amzn_fingerprint = _amazon_intraday_pass(_cad["tenant"], last_amzn_fingerprint)
3532
  # ⛔⛔ HOTFIX 2026-09-29 — A RESTART INSIDE THE DAILY WINDOW IS NOT A SYNC. The owner
3533
  # capped syncing at once a day, and a deploy is not a reason to sync again. So the first
3534
  # pass after a boot that lands within one cadence of the last scheduled pass only catches
 
3571
  else:
3572
  _project_desk(_desk, _cad["tenant"])
3573
  if not slot_restored:
3574
+ if not _amazon_desk_busy:
3575
+ _pull_amazon("boot", False)
3576
  # The restored mirror warms the workbooks once; Sheet warm reads only the mirror.
3577
  _sheet_warm_after_pass(_cad["tenant"], True, None, True)
3578
  slot_restored = True
3579
  print(f"[aios-api] store resync: no Odoo pass outside the daily slot; the next one "
3580
  f"runs at {RESYNC_STATE['nextPassAt']} ({_slot['at']} {_slot['tz']})")
3581
  continue
3582
+ if (_slot is None and not _cadence_due and _odoo_requested is None and not _continuation):
3583
+ continue
3584
  passes += 1
3585
  backfill_next_at = None
3586
  if not _continuation:
 
3827
  except Exception as e: # noqa: BLE001
3828
  print(f"[aios-api] meta relational refresh failed (resync): "
3829
  f"{type(e).__name__}: {e}")
3830
+ # The independent desk retains Amazon's pull and projection while Odoo completes its pass.
3831
+ if not _amazon_desk_busy and _resumed.get("amazon"):
3832
  _pull_amazon("boot", False)
3833
+ elif not _amazon_desk_busy:
3834
  # The daily slot pass refreshes every Amazon table: a late extra pass yesterday must not defer it.
3835
  RESYNC_STATE["slotPass"] = _slot_claimed
3836
  try:
 
3844
  try:
3845
  from harness import runtime as _runtime
3846
  _rt_resync = _runtime.get_runtime(_cad["tenant"])
3847
+ _amzn_state = ({} if _amazon_desk_busy else
3848
+ _rebuild_amazon_relational("resync", _rt_resync, _amzn_previous))
3849
  last_amzn_fingerprint = (_amzn_state["fingerprint"]
3850
  if _amzn_state.get("known") else None)
3851
  except Exception as e: # noqa: BLE001
api/routes_account_health.py CHANGED
@@ -5,7 +5,8 @@ from urllib.parse import urlencode
5
  from deps import Session, err, require_session
6
 
7
  TEMPLATE_KIND = "account_health"
8
- router = APIRouter(prefix=f"/api/v1/app-kinds/{TEMPLATE_KIND}", tags=["account-health"])
 
9
  SOURCE = "ut_amzn_account_health"
10
 
11
 
@@ -101,7 +102,7 @@ def owners(session):
101
  return sorted((n for n in names if n), key=str.lower)
102
 
103
 
104
- @router.get("/summary")
105
  def read_summary(app: str = Query(..., min_length=1), search: str = Query(""),
106
  offset: int = Query(0, ge=0), limit: int = Query(50, ge=1),
107
  metric: str | None = Query(None), session: Session = Depends(_app_reader)):
@@ -115,7 +116,7 @@ def read_summary(app: str = Query(..., min_length=1), search: str = Query(""),
115
  return {**out, "canEdit": _may_edit(session, _app(session, app))}
116
 
117
 
118
- @router.get("/history")
119
  def read_history(metric: str = Query(..., min_length=1), cursor: str | None = Query(None),
120
  limit: int = Query(400, ge=1), session: Session = Depends(_app_reader)):
121
  ah = _module()
@@ -133,7 +134,7 @@ def read_history(metric: str = Query(..., min_length=1), cursor: str | None = Qu
133
  return _call(ah.history, _tenant(session), metric, cursor=cursor, limit=limit, st=session.runtime)
134
 
135
 
136
- @router.get("/items")
137
  def list_items(app: str = Query(..., min_length=1), status: str | None = Query(None),
138
  search: str = Query(""), offset: int = Query(0, ge=0), metric: str | None = Query(None),
139
  cursor: str | None = Query(None), limit: int = Query(50, ge=1),
@@ -149,7 +150,7 @@ def list_items(app: str = Query(..., min_length=1), status: str | None = Query(N
149
  return {**listed, "owners": owners(session), "canEdit": _may_edit(session, _app(session, app))}
150
 
151
 
152
- @router.get("/items/{item_id}")
153
  def read_item(item_id: str, app: str = Query(..., min_length=1),
154
  session: Session = Depends(_app_reader)):
155
  ah = _module()
@@ -163,7 +164,7 @@ def read_item(item_id: str, app: str = Query(..., min_length=1),
163
  "canEdit": _may_edit(session, _app(session, app))}
164
 
165
 
166
- @router.patch("/items/{item_id}")
167
  def patch_item(item_id: str, owner: str | None = Body(None), status: str | None = Body(None),
168
  due: str | None = Body(None), note: str | None = Body(None),
169
  session: Session = Depends(_app_writer)):
@@ -176,13 +177,13 @@ def patch_item(item_id: str, owner: str | None = Body(None), status: str | None
176
  due=due, note=note, st=session.runtime)
177
 
178
 
179
- @router.get("/settings")
180
  def read_settings(session: Session = Depends(_app_reader)):
181
  ah = _module()
182
  return _call(ah.settings, _tenant(session), st=session.runtime)
183
 
184
 
185
- @router.put("/settings")
186
  def write_settings(alertDistancePct: float | None = Body(None), defaultOwner: str | None = Body(None),
187
  riskOverdueDays: float | None = Body(None),
188
  session: Session = Depends(_app_writer)):
@@ -191,7 +192,7 @@ def write_settings(alertDistancePct: float | None = Body(None), defaultOwner: st
191
  default_owner=defaultOwner, risk_overdue_days=riskOverdueDays, st=session.runtime)
192
 
193
 
194
- @router.get("/risks")
195
  def list_risks(app: str = Query(..., min_length=1), status: str | None = Query(None),
196
  search: str = Query(""), offset: int = Query(0, ge=0), limit: int = Query(50, ge=1),
197
  session: Session = Depends(_app_reader)):
@@ -205,7 +206,7 @@ def list_risks(app: str = Query(..., min_length=1), status: str | None = Query(N
205
  return {**out, "owners": owners(session), "canEdit": _may_edit(session, _app(session, app))}
206
 
207
 
208
- @router.patch("/risks/{item_id}")
209
  def patch_risk(item_id: str, owner: str | None = Body(None), status: str | None = Body(None),
210
  caseId: str | None = Body(None), appealDate: str | None = Body(None),
211
  outcome: str | None = Body(None), note: str | None = Body(None),
@@ -218,6 +219,22 @@ def patch_risk(item_id: str, owner: str | None = Body(None), status: str | None
218
  case_id=caseId, appeal_date=appealDate, outcome=outcome, note=note, st=session.runtime)
219
 
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  # --------------------------------------------------------------------------- R62 source narrowing
222
 
223
  def _narrowed(session):
 
5
  from deps import Session, err, require_session
6
 
7
  TEMPLATE_KIND = "account_health"
8
+ router = APIRouter(tags=["account-health"])
9
+ app_router = APIRouter(prefix=f"/api/v1/app-kinds/{TEMPLATE_KIND}", tags=["account-health"])
10
  SOURCE = "ut_amzn_account_health"
11
 
12
 
 
102
  return sorted((n for n in names if n), key=str.lower)
103
 
104
 
105
+ @app_router.get("/summary")
106
  def read_summary(app: str = Query(..., min_length=1), search: str = Query(""),
107
  offset: int = Query(0, ge=0), limit: int = Query(50, ge=1),
108
  metric: str | None = Query(None), session: Session = Depends(_app_reader)):
 
116
  return {**out, "canEdit": _may_edit(session, _app(session, app))}
117
 
118
 
119
+ @app_router.get("/history")
120
  def read_history(metric: str = Query(..., min_length=1), cursor: str | None = Query(None),
121
  limit: int = Query(400, ge=1), session: Session = Depends(_app_reader)):
122
  ah = _module()
 
134
  return _call(ah.history, _tenant(session), metric, cursor=cursor, limit=limit, st=session.runtime)
135
 
136
 
137
+ @app_router.get("/items")
138
  def list_items(app: str = Query(..., min_length=1), status: str | None = Query(None),
139
  search: str = Query(""), offset: int = Query(0, ge=0), metric: str | None = Query(None),
140
  cursor: str | None = Query(None), limit: int = Query(50, ge=1),
 
150
  return {**listed, "owners": owners(session), "canEdit": _may_edit(session, _app(session, app))}
151
 
152
 
153
+ @app_router.get("/items/{item_id}")
154
  def read_item(item_id: str, app: str = Query(..., min_length=1),
155
  session: Session = Depends(_app_reader)):
156
  ah = _module()
 
164
  "canEdit": _may_edit(session, _app(session, app))}
165
 
166
 
167
+ @app_router.patch("/items/{item_id}")
168
  def patch_item(item_id: str, owner: str | None = Body(None), status: str | None = Body(None),
169
  due: str | None = Body(None), note: str | None = Body(None),
170
  session: Session = Depends(_app_writer)):
 
177
  due=due, note=note, st=session.runtime)
178
 
179
 
180
+ @app_router.get("/settings")
181
  def read_settings(session: Session = Depends(_app_reader)):
182
  ah = _module()
183
  return _call(ah.settings, _tenant(session), st=session.runtime)
184
 
185
 
186
+ @app_router.put("/settings")
187
  def write_settings(alertDistancePct: float | None = Body(None), defaultOwner: str | None = Body(None),
188
  riskOverdueDays: float | None = Body(None),
189
  session: Session = Depends(_app_writer)):
 
192
  default_owner=defaultOwner, risk_overdue_days=riskOverdueDays, st=session.runtime)
193
 
194
 
195
+ @app_router.get("/risks")
196
  def list_risks(app: str = Query(..., min_length=1), status: str | None = Query(None),
197
  search: str = Query(""), offset: int = Query(0, ge=0), limit: int = Query(50, ge=1),
198
  session: Session = Depends(_app_reader)):
 
206
  return {**out, "owners": owners(session), "canEdit": _may_edit(session, _app(session, app))}
207
 
208
 
209
+ @app_router.patch("/risks/{item_id}")
210
  def patch_risk(item_id: str, owner: str | None = Body(None), status: str | None = Body(None),
211
  caseId: str | None = Body(None), appealDate: str | None = Body(None),
212
  outcome: str | None = Body(None), note: str | None = Body(None),
 
219
  case_id=caseId, appeal_date=appealDate, outcome=outcome, note=note, st=session.runtime)
220
 
221
 
222
+ router.include_router(app_router)
223
+
224
+
225
+ @router.get("/api/v1/account-health/categories/{key}")
226
+ def read_category(key: str, session: Session = Depends(_app_reader)):
227
+ ah = _module()
228
+ context = _call(_narrowed, session)
229
+
230
+ def allowed(row, snapshot):
231
+ evidence = _call(ah.category_source, _tenant(session), session.runtime, row, snapshot)
232
+ return _source_allowed({"sourceEvidence": evidence}, context)
233
+
234
+ return _call(ah.category_detail, _tenant(session), key, user=session.user,
235
+ allowed=allowed if context is not None else None, st=session.runtime)
236
+
237
+
238
  # --------------------------------------------------------------------------- R62 source narrowing
239
 
240
  def _narrowed(session):
api/routes_admin.py CHANGED
@@ -143,17 +143,7 @@ def _view(uname, rec, governed=None, st=None, surface_keys=None):
143
  # editor will open, so the two cannot disagree.
144
  "accessSummary": _access_summary(rec, uname=uname, governed=governed, st=st,
145
  surface_keys=surface_keys),
146
- "perms_v": int(rec.get("perms_v") or 0),
147
- # S79-T63: only a copied test account carries `test`, so a real account's keys stay exact.
148
- **({"test": _test_marker(rec)} if _test_marker(rec) else {})}
149
-
150
-
151
- def _test_marker(rec):
152
- mark = (rec or {}).get(users.TEST_ACCOUNT_KEY)
153
- if not isinstance(mark, dict):
154
- return None
155
- return {"copiedFrom": mark.get("copied_from"), "createdBy": mark.get("created_by"),
156
- "createdAt": mark.get("created_at")}
157
 
158
 
159
  def _access_summary(rec, uname="", governed=None, st=None, surface_keys=None):
@@ -464,9 +454,6 @@ def update_user(username: str, body: dict = Body(default=None),
464
  if result == "last_admin":
465
  raise err(400, "last_admin",
466
  "an active tenant must retain at least one administrator")
467
- if result in _TEST_ACCOUNT_REFUSALS:
468
- # R42: a test account gets no email and no admin role, refused in the core writer.
469
- raise err(400, result, _TEST_ACCOUNT_REFUSALS[result])
470
  if result != "updated":
471
  raise err(404, "no_such_user", f"no account named {uname!r}")
472
 
@@ -552,164 +539,6 @@ def expire_password(username: str, session: Session = Depends(admin_gate)):
552
  return Response(status_code=204)
553
 
554
 
555
- # ── S79-T63 (R28, R42): test accounts that copy one member's permissions exactly ─────────────────
556
- _TEST_ACCOUNT_REFUSALS = {
557
- "test_account_no_email": "A test account cannot have an email address.",
558
- "test_account_no_whatsapp": "A test account cannot connect WhatsApp.",
559
- "test_account_not_admin": "A test account cannot be an administrator.",
560
- }
561
-
562
-
563
- def _copy_refusal(exc):
564
- return err(exc.status, exc.code, str(exc))
565
-
566
-
567
- #: The en and em dash, built from code points so this file carries no dash character (rule 2).
568
- def _copy_failure(exc, verb):
569
- # A store error can carry any text; rule 2 keeps dashes off the screen.
570
- import dash_norm
571
- cause = dash_norm.plain(exc.cause).strip().rstrip(".")
572
- message = f"The test account was not {verb}. {cause}."
573
- if exc.leftovers:
574
- message += (" These parts could not be removed: " + ", ".join(exc.leftovers)
575
- + ". Remove the test account again to finish.")
576
- elif verb == "created":
577
- message += " Nothing was kept."
578
- return err(503, "test_account_failed", message)
579
-
580
-
581
- def _test_account_steps(session):
582
- """Copy steps for grant stores outside `core`: Connector grants, through their own route."""
583
- import core.perm_scope as perm_scope
584
- import routes_keychain as keychain
585
-
586
- def held():
587
- doc = session.runtime.get(perm_scope.CONNECTOR_GRANT_KEY, fresh=True)
588
- if doc is not None and not isinstance(doc, dict):
589
- raise RuntimeError("connector access could not be read")
590
- return doc or {}
591
-
592
- def copy_grants(username, source):
593
- known = {kind["key"] for kind in keychain.connector_grant_kinds()}
594
- kinds = sorted(k for k in (held().get(source) or ()) if k in known)
595
- if kinds:
596
- keychain.put_connector_grants(username, {"granted": kinds}, session)
597
-
598
- def clear_grants(username):
599
- if username in held():
600
- keychain.put_connector_grants(username, {"granted": []}, session)
601
-
602
- return [("connector access", copy_grants, clear_grants)]
603
-
604
-
605
- @router.get("/admin/users/{username}/test-account/preview")
606
- def preview_test_account(username: str, session: Session = Depends(admin_gate)):
607
- """What a test account copied from this member would carry, before anything is written."""
608
- from core import test_accounts, user_tables
609
- reg = _registry()
610
- try:
611
- plan = test_accounts.plan(username, reg, tenant=_tenant_of(session.user),
612
- st=session.runtime)
613
- lent = user_tables.lend_defs(session.runtime, schema_only=True)
614
- except test_accounts.CopyRefused as exc:
615
- raise _copy_refusal(exc)
616
- except Exception as exc: # noqa: BLE001
617
- raise err(503, "store_unavailable", "Access unavailable. Try again.") from exc
618
- limits = _source_limits(plan["source"], reg.get(plan["source"]), session)
619
- body = test_accounts.preview(plan, st=lent, limits=limits)
620
- _label_grants(session, body["copy"]["grants"])
621
- return body
622
-
623
-
624
- def _source_limits(uname, rec, session):
625
- """The source's row limits, phrased for the administrator; None if they cannot be read.
626
-
627
- Field walls are lifted for the phrasing only, so a condition on a hidden field is named
628
- instead of reading "Limited by your access". The filters themselves are the source's.
629
- """
630
- import core.perm_scope as perm_scope
631
- try:
632
- reader = deepcopy(users._public(uname, rec))
633
- for entry in (reader.get("perms") or {}).values():
634
- if isinstance(entry, dict):
635
- entry.pop("hiddenFields", None)
636
- return perm_scope.scope_limits(reader, st=session.runtime)
637
- except Exception: # noqa: BLE001
638
- return None
639
-
640
-
641
- #: What a preview row says when a shared item's name cannot be read. Never a raw id.
642
- _GRANT_FALLBACK = {"view": "A shared view", "folder": "A shared folder", "database": "A database",
643
- "field": "A shared field", "cohort": "A locked view", "workbook": "A shared sheet"}
644
- #: Naming stops after this many seconds; every grant is still listed, the rest by kind.
645
- _LABEL_BUDGET_S = 3.0
646
-
647
-
648
- def _label_grants(session, grants):
649
- """Name each grant through the share door's own resolver (`routes_shares._object_ref`)."""
650
- import time
651
- try:
652
- from routes_shares import _object_ref
653
- except Exception: # noqa: BLE001
654
- _object_ref = None
655
- deadline = time.monotonic() + _LABEL_BUDGET_S
656
- for grant in grants:
657
- if grant.get("label"):
658
- continue
659
- label = None
660
- if time.monotonic() < deadline:
661
- try:
662
- if grant["kind"] == "workbook":
663
- # `_object_ref` has no workbook branch; the workbook's own name is the label.
664
- import core.sheets as sheets
665
- label = str(sheets.get_workbook(session.runtime, grant["oid"]).get("name") or "")
666
- elif _object_ref is not None:
667
- label = str(_object_ref(session, grant["kind"], grant["oid"])[0] or "")
668
- except Exception: # noqa: BLE001
669
- label = None
670
- label = (label or "").strip()
671
- grant["label"] = label or _GRANT_FALLBACK.get(grant["kind"], "A shared item")
672
-
673
-
674
- @router.post("/admin/test-accounts", status_code=201)
675
- def create_test_account(body: dict = Body(default=None), session: Session = Depends(admin_gate)):
676
- """Create a test account whose permissions copy `source` exactly. The password shows once."""
677
- from core import test_accounts
678
- body = body if isinstance(body, dict) else {}
679
- _require_store()
680
- try:
681
- made = test_accounts.create(body.get("source"), admin=session.uname,
682
- tenant=_tenant_of(session.user), st=session.runtime,
683
- steps=_test_account_steps(session))
684
- except test_accounts.CopyRefused as exc:
685
- raise _copy_refusal(exc)
686
- except test_accounts.CopyFailed as exc:
687
- raise _copy_failure(exc, "created")
688
- except Exception as exc: # noqa: BLE001
689
- raise err(503, "store_unavailable", "The test account was not created. Try again.") from exc
690
- rec = _registry().get(made["username"])
691
- if not isinstance(rec, dict):
692
- raise err(503, "store_unavailable", "The test account was not saved. Try again.")
693
- return {"user": _view(made["username"], rec), "password": made["password"]}
694
-
695
-
696
- @router.delete("/admin/test-accounts/{username}", status_code=204)
697
- def delete_test_account(username: str, session: Session = Depends(admin_gate)):
698
- """Remove a test account, its sign in and every grant naming it, in one call."""
699
- from core import test_accounts
700
- _require_store()
701
- try:
702
- test_accounts.remove(username, tenant=_tenant_of(session.user), st=session.runtime,
703
- granter=session.uname, steps=_test_account_steps(session))
704
- except test_accounts.CopyRefused as exc:
705
- raise _copy_refusal(exc)
706
- except test_accounts.CopyFailed as exc:
707
- raise _copy_failure(exc, "fully removed")
708
- except Exception as exc: # noqa: BLE001
709
- raise err(503, "store_unavailable", "The test account was not removed. Try again.") from exc
710
- return Response(status_code=204)
711
-
712
-
713
  # ── C-PERM (wave 15): per-module access + permanent filters + hidden fields ──────────────────
714
  #: The modules the permission editor governs — the GRID surfaces, the ones with a field schema
715
  #: to filter and hide. Amendment 6 shipped the wall on `customer_data` alone and said this list
 
143
  # editor will open, so the two cannot disagree.
144
  "accessSummary": _access_summary(rec, uname=uname, governed=governed, st=st,
145
  surface_keys=surface_keys),
146
+ "perms_v": int(rec.get("perms_v") or 0)}
 
 
 
 
 
 
 
 
 
 
147
 
148
 
149
  def _access_summary(rec, uname="", governed=None, st=None, surface_keys=None):
 
454
  if result == "last_admin":
455
  raise err(400, "last_admin",
456
  "an active tenant must retain at least one administrator")
 
 
 
457
  if result != "updated":
458
  raise err(404, "no_such_user", f"no account named {uname!r}")
459
 
 
539
  return Response(status_code=204)
540
 
541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  # ── C-PERM (wave 15): per-module access + permanent filters + hidden fields ──────────────────
543
  #: The modules the permission editor governs — the GRID surfaces, the ones with a field schema
544
  #: to filter and hide. Amendment 6 shipped the wall on `customer_data` alone and said this list
api/routes_amazon_projects.py CHANGED
@@ -12,7 +12,7 @@ from pathlib import Path
12
  import sys
13
  from typing import Any
14
 
15
- from fastapi import APIRouter, Body, Depends, HTTPException
16
 
17
  from core import amazon_projects as projects
18
  from core import amazon_project_results as project_results
@@ -219,7 +219,7 @@ def _distinct(found, pairs):
219
 
220
 
221
  def cell_values(records):
222
- """R19 and R67: the average progress as a stored pct and "N of M completed", blank with no counted project."""
223
  counted = projects.summary(records)
224
  if not counted["total"]:
225
  return {PROGRESS_KEY: None, LABEL_KEY: None}
@@ -343,28 +343,34 @@ def _result_sources(session, targets):
343
  return available
344
 
345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  def _result_figure(con, config, bounds, skus, available):
347
  """Reuse daily metric definitions and reconcile their union against separate source totals."""
348
  if not skus:
349
  return {"state": "unavailable", "reason": "Assign an Amazon target before measuring results."}
350
 
351
- class ProjectMirror(sku_action_results.Mirror):
352
- def entity(self):
353
- self.field(sku_action_results.ECONOMICS, "msku")
354
- return '"msku" IN (' + ", ".join("?" for _ in skus) + ")", tuple(skus)
355
-
356
- def raw(self, table, days, values, **kwargs):
357
- plan = super().raw(table, days, values, **kwargs)
358
- sku = self.field(table, *kwargs["sku_fields"])
359
- head, tail = plan.sql.split(" FROM ", 1)
360
- coverage = sku_action_results.Plan(
361
- head + ", " + sku_action_results._q(sku) + ' AS "__project_member" FROM ' + tail,
362
- plan.args, plan.sources)
363
- self.coverage.append((table, tuple(days), coverage))
364
- return plan
365
-
366
- mirror = ProjectMirror(con, {}, available)
367
- mirror.coverage = []
368
  try:
369
  mirror.select_scope(bounds)
370
  except sku_action_results.SourceUnavailable as exc:
@@ -406,6 +412,81 @@ def _complete_result_membership(con, coverage, bounds, sources, skus, names=("cu
406
  return True
407
 
408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  @router.get("")
410
  def row_projects(grain: str | None = None, key: str | None = None,
411
  session: Session = Depends(require_session)):
@@ -508,6 +589,61 @@ def project_result_figures(project_id: str, session: Session = Depends(require_s
508
  con.close()
509
 
510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511
  @router.patch("/{project_id}")
512
  def edit_project(project_id: str, body: Any = Body(default=None), session: Session = Depends(require_session)):
513
  """Change the named fields. Targets named here are added."""
@@ -522,8 +658,11 @@ def edit_project(project_id: str, body: Any = Body(default=None), session: Sessi
522
  record = _call(projects.update, runtime, project_id, values, by=_by(session))
523
  if targets:
524
  record = _call(projects.add_targets, runtime, project_id, targets, by=_by(session))["project"]
525
- # Only status and progress move a cell: a name or note edit repaints only the targets it adds.
526
- _repaint(runtime, _target_pairs(runtime, project_id) if {"status", "progress"} & set(values) else targets)
 
 
 
527
  return {"project": _project(record, projects._today())}
528
 
529
 
 
12
  import sys
13
  from typing import Any
14
 
15
+ from fastapi import APIRouter, Body, Depends, HTTPException, Query
16
 
17
  from core import amazon_projects as projects
18
  from core import amazon_project_results as project_results
 
219
 
220
 
221
  def cell_values(records):
222
+ """Store average progress with Completed at 100 and the completion count, blank when none count."""
223
  counted = projects.summary(records)
224
  if not counted["total"]:
225
  return {PROGRESS_KEY: None, LABEL_KEY: None}
 
343
  return available
344
 
345
 
346
+ class ProjectMirror(sku_action_results.Mirror):
347
+ """Read a deduplicated project SKU union and retain source evidence for every target day."""
348
+
349
+ def __init__(self, con, skus, available):
350
+ super().__init__(con, {}, available)
351
+ self.skus, self.coverage = tuple(skus), []
352
+
353
+ def entity(self):
354
+ self.field(sku_action_results.ECONOMICS, "msku")
355
+ return '"msku" IN (' + ", ".join("?" for _ in self.skus) + ")", self.skus
356
+
357
+ def raw(self, table, days, values, **kwargs):
358
+ plan = super().raw(table, days, values, **kwargs)
359
+ sku = self.field(table, *kwargs["sku_fields"])
360
+ head, tail = plan.sql.split(" FROM ", 1)
361
+ coverage = sku_action_results.Plan(
362
+ head + ", " + sku_action_results._q(sku) + ' AS "__project_member" FROM ' + tail,
363
+ plan.args, plan.sources)
364
+ self.coverage.append((table, tuple(days), coverage))
365
+ return plan
366
+
367
+
368
  def _result_figure(con, config, bounds, skus, available):
369
  """Reuse daily metric definitions and reconcile their union against separate source totals."""
370
  if not skus:
371
  return {"state": "unavailable", "reason": "Assign an Amazon target before measuring results."}
372
 
373
+ mirror = ProjectMirror(con, skus, available)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  try:
375
  mirror.select_scope(bounds)
376
  except sku_action_results.SourceUnavailable as exc:
 
412
  return True
413
 
414
 
415
+ _SERIES_UNAVAILABLE = {
416
+ "margin": "Margin requires daily net profit with verified Odoo or typed COGS. "
417
+ "amzn_economics alone cannot reconcile that cost.",
418
+ "returns": "Returns require FBA and MFN customer return events. "
419
+ "amzn_economics has no independent returned-unit aggregate.",
420
+ "returns_pct": "Returns % requires returned units over shipped units. "
421
+ "amzn_economics cannot independently reconcile those quantities.",
422
+ "refunds_pct": "Refunds % requires deduplicated refund units over shipped units. "
423
+ "amzn_economics cannot independently reconcile those quantities.",
424
+ "roas": "ROAS requires attributed ad sales over ad spend. "
425
+ "amzn_economics has no independent attributed ad-sales aggregate.",
426
+ }
427
+
428
+
429
+ def _series_figure(key, figure, bounds):
430
+ """Adapt existing sales and units definitions without publishing partial source windows."""
431
+ if key in _SERIES_UNAVAILABLE:
432
+ return {"reason": _SERIES_UNAVAILABLE[key]}
433
+ if figure.get("reason"):
434
+ return {"reason": figure["reason"]}
435
+ current = figure.get("current")
436
+ if not current or current["status"] != "available":
437
+ return {"reason": (current or {}).get("reason") or "Some target source days are unavailable."}
438
+ if key != "sales_yoy":
439
+ return {"total": current["value"], "points": current["series"], "unit": figure["unit"],
440
+ "reason": "Today is incomplete and excluded." if bounds["to"] == bounds["current"][1].isoformat() else None}
441
+ prior = figure["comparison"]
442
+ if prior["status"] != "available" or prior["value"] is None or prior["value"] <= 0:
443
+ return {"reason": prior.get("reason") or "Sales YoY requires positive sales in the matching prior window."}
444
+ points = []
445
+ for now, before in zip(current["series"], prior["series"]):
446
+ if before["value"] is None or before["value"] <= 0:
447
+ return {"reason": "Sales YoY requires positive sales on every matching prior day."}
448
+ points.append({"date": now["date"], "value": 100 * (now["value"] - before["value"]) / before["value"]})
449
+ return {"total": 100 * (current["value"] - prior["value"]) / prior["value"], "points": points,
450
+ "reason": "Today is incomplete and excluded." if bounds["to"] == bounds["current"][1].isoformat() else None}
451
+
452
+
453
+ def _series_economics_total(con, key, bounds, skus, available):
454
+ """Aggregate original economics rows without the display plan, daily rollup or display total."""
455
+ if key in _SERIES_UNAVAILABLE:
456
+ return {"value": None, "reason": _SERIES_UNAVAILABLE[key]}
457
+ if not skus or not bounds["days"]:
458
+ return {"value": None, "reason": "No completed target days are available in this range."}
459
+ mirror = ProjectMirror(con, skus, available)
460
+ try:
461
+ mirror.select_scope(bounds)
462
+ field = mirror.field(sku_action_results.ECONOMICS,
463
+ "sales_net_units_sold" if key == "units" else "sales_net_product_sales_amount")
464
+ number = f'TRY_CAST({sku_action_results._q(field)} AS DOUBLE)'
465
+ day = sku_action_results._day("start_date")
466
+ marks = ", ".join("?" for _ in skus)
467
+
468
+ def total(days):
469
+ row = con.execute(
470
+ f'SELECT SUM({number}), count(*), count({number}), count(DISTINCT (msku, {day})) '
471
+ f'FROM amzn_economics WHERE msku IN ({marks}) AND marketplace_id = ? '
472
+ f'AND sales_net_product_sales_currency_code = ? AND {day} >= ? AND {day} < ?',
473
+ (*skus, *mirror.scope, *days)).fetchone()
474
+ if (not row[1] or row[1] != row[2] or row[3] != len(skus) * bounds["days"]
475
+ or not project_results._finite(row[0])):
476
+ raise sku_action_results.SourceUnavailable("amzn_economics: some target values or days are unavailable.")
477
+ return float(row[0])
478
+
479
+ value = total(bounds["current"])
480
+ if key == "sales_yoy":
481
+ prior = total(bounds["comparison"])
482
+ if prior <= 0:
483
+ raise sku_action_results.SourceUnavailable("Sales YoY requires positive prior sales.")
484
+ value = 100 * (value - prior) / prior
485
+ return {"value": value, "reason": None}
486
+ except sku_action_results.SourceUnavailable as exc:
487
+ return {"value": None, "reason": str(exc)}
488
+
489
+
490
  @router.get("")
491
  def row_projects(grain: str | None = None, key: str | None = None,
492
  session: Session = Depends(require_session)):
 
589
  con.close()
590
 
591
 
592
+ @router.get("/{project_id}/series")
593
+ def project_result_series(project_id: str, start: str | None = Query(None, alias="from"),
594
+ end: str | None = Query(None, alias="to"), scope: str = Query("all"),
595
+ session: Session = Depends(require_session)):
596
+ """Serve the preset daily charts within the intersection of project targets and the modal filter."""
597
+ runtime = _reader(session)
598
+ record = _call(projects.get, runtime, project_id)
599
+ targets = _call(projects.targets, runtime, project_id)
600
+ con = None
601
+ try:
602
+ bounds = project_results.series_window(record, start, end, today=projects._today())
603
+ selected = project_results.series_scope(scope)
604
+ selected_targets = ([{"grain": "parent_asin" if selected["grain"] == "parent" else selected["grain"],
605
+ "key": key} for key in selected["keys"]] if selected else [])
606
+ available = _result_sources(session, targets + selected_targets)
607
+ con = routes_sku_actions._results_mirror(runtime)
608
+ reason = None
609
+ try:
610
+ skus = projects.resolve_target_skus(con, targets)
611
+ if selected:
612
+ selected_skus = set(projects.resolve_target_skus(con, selected_targets))
613
+ skus = [sku for sku in skus if sku in selected_skus]
614
+ if not skus:
615
+ reason = "No project targets match the selected scope."
616
+ elif not bounds["days"]:
617
+ reason = "Today is incomplete. No completed days are available in this range."
618
+ except projects.MirrorUnreadable as exc:
619
+ skus, reason = [], str(exc)
620
+ figures = {}
621
+
622
+ def read(key):
623
+ if reason:
624
+ return {"reason": reason}
625
+ metric = "sales" if key == "sales_yoy" else key
626
+ if metric not in _SERIES_UNAVAILABLE and metric not in figures:
627
+ figures[metric] = _result_figure(con, {"metric": metric}, bounds, skus, available)
628
+ return _series_figure(key, figures.get(metric, {}), bounds)
629
+
630
+ return project_results.series(record, bounds=bounds, read=read,
631
+ independent=lambda key: _series_economics_total(con, key, bounds, skus, available))
632
+ except HTTPException:
633
+ raise
634
+ except (project_results.ValidationFailed, sku_action_results.ValidationFailed):
635
+ raise err(409, "project_results_not_validated",
636
+ "A series total does not tie to its independent Amazon economics aggregate.") from None
637
+ except ValueError as exc:
638
+ raise err(400, "invalid_project_results", str(exc)) from None
639
+ except Exception as exc: # noqa: BLE001
640
+ print(f"[amazon-projects] series failed: {type(exc).__name__}: {exc}", file=sys.stderr)
641
+ raise err(503, "project_results_unavailable", "Project result sources are unavailable.") from None
642
+ finally:
643
+ if con is not None:
644
+ con.close()
645
+
646
+
647
  @router.patch("/{project_id}")
648
  def edit_project(project_id: str, body: Any = Body(default=None), session: Session = Depends(require_session)):
649
  """Change the named fields. Targets named here are added."""
 
658
  record = _call(projects.update, runtime, project_id, values, by=_by(session))
659
  if targets:
660
  record = _call(projects.add_targets, runtime, project_id, targets, by=_by(session))["project"]
661
+ # A status or progress edit repaints every assigned target, including when a project reopens.
662
+ if {"status", "progress"} & set(values):
663
+ _repaint(runtime, _target_pairs(runtime, project_id))
664
+ elif targets:
665
+ _repaint(runtime, targets)
666
  return {"project": _project(record, projects._today())}
667
 
668
 
api/routes_connectors.py CHANGED
@@ -65,7 +65,7 @@ from core import schema_overlay
65
  #: one question, and this module used to answer it with an unguarded `os.environ.get("ODOO_URL")`
66
  #: while `routes_keychain` answered it correctly. Importing the correct one is the fix; writing a
67
  #: second correct one here would leave the same two-copies shape that produced the defect.
68
- from routes_keychain import TENANT_WIDE_TYPES, allowed_scopes, env_odoo_available
69
  #: ⛔⛔ W49-T16 / C3 — THE DEPENDENTS ROUTER IS MOUNTED FROM HERE, NOT FROM `main.py`. In a
70
  #: worktree-parallel wave `main.py` lives in exactly ONE lane's fence, so a router built in any
71
  #: other lane has nowhere to be mounted from (five consecutive waves lost an `include_router`
@@ -1095,15 +1095,25 @@ def _contact_rows(rt, uname, is_admin, incomplete):
1095
  def read(source, keys, fn):
1096
  try:
1097
  fn()
1098
- except Exception: # noqa: BLE001
 
 
1099
  for key in keys:
1100
  by_key[key]["state"] = "unavailable"
1101
- incomplete.append({"source": source, "cause": "Connection status unavailable.",
 
 
1102
  "recommendation": "Try again."})
1103
 
1104
  def whatsapp():
1105
- import whatsapp_identity
1106
- if whatsapp_identity.status(rt.key, uname).get("state") == "connected":
 
 
 
 
 
 
1107
  by_key["whatsapp"]["state"] = "connected"
1108
 
1109
  def slack():
@@ -3056,6 +3066,88 @@ def _amazon_mirror_completion(rt):
3056
  return None, False
3057
 
3058
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3059
  def _daily_completed_sync(rt, connector_key):
3060
  """Read the stored last good sync; never create a mirror or contact a provider."""
3061
  if connector_key == "amazon_sp":
@@ -3154,8 +3246,11 @@ def connector_sync_status(connector_key: str, connection_id: str = None,
3154
  if connector_key not in registered:
3155
  raise err(404, "sync_provider_unknown",
3156
  f"{connector_key} is not registered for connector sync")
 
 
3157
  rt = (_odoo_status_reads(session.runtime) if connector_key == "odoo"
3158
- else _ConnectorStatusReads(session.runtime, (connector_sync.JOBS_KEY, connector_sync.STATE_KEY, connector_sync.STATUS_KEY)
 
3159
  if connector_key == "amazon_sp" else ()))
3160
  resolved_connection = (_odoo_connection_slot(rt, connection_id)
3161
  if connector_key == "odoo" else connection_id)
@@ -3179,9 +3274,15 @@ def connector_sync_status(connector_key: str, connection_id: str = None,
3179
  if connector_key == "amazon_sp" and completed is None and known and _amazon_restore_pending(rt):
3180
  completed = _amazon_saved_completion(rt)
3181
  answer["restoring"] = True
3182
- answer.update(schedule="daily", lastCompletedAt=completed, lastSyncAt=completed,
3183
- lastCompletedKnown=known, canSync=bool(session.admin),
3184
- **_daily_sync_staleness(completed))
 
 
 
 
 
 
3185
  return _safe_connector_status(connector_key, answer)
3186
 
3187
 
 
65
  #: one question, and this module used to answer it with an unguarded `os.environ.get("ODOO_URL")`
66
  #: while `routes_keychain` answered it correctly. Importing the correct one is the fix; writing a
67
  #: second correct one here would leave the same two-copies shape that produced the defect.
68
+ from routes_keychain import TENANT_WIDE_TYPES, allowed_scopes, env_odoo_available, stored_business_tz
69
  #: ⛔⛔ W49-T16 / C3 — THE DEPENDENTS ROUTER IS MOUNTED FROM HERE, NOT FROM `main.py`. In a
70
  #: worktree-parallel wave `main.py` lives in exactly ONE lane's fence, so a router built in any
71
  #: other lane has nowhere to be mounted from (five consecutive waves lost an `include_router`
 
1095
  def read(source, keys, fn):
1096
  try:
1097
  fn()
1098
+ except Exception as exc: # noqa: BLE001
1099
+ logging.getLogger(__name__).warning("contact_check=%s exception=%s",
1100
+ source, type(exc).__name__)
1101
  for key in keys:
1102
  by_key[key]["state"] = "unavailable"
1103
+ label = {"whatsapp": "WhatsApp", "slack": "Slack", "mcp": "MCP",
1104
+ "loopable-api": "Loopable API"}[source]
1105
+ incomplete.append({"source": source, "cause": f"{label} connection status unavailable.",
1106
  "recommendation": "Try again."})
1107
 
1108
  def whatsapp():
1109
+ from core import account_rows, users
1110
+ from whatsapp_identity import WhatsAppRefusal
1111
+ # Directory status reads the reader's canonical account, never registration challenges.
1112
+ record = account_rows.get(uname, st=users.store, fresh=True)
1113
+ if not isinstance(record, dict) or str(record.get("tenant") or "royal-imports") != rt.key:
1114
+ raise WhatsAppRefusal("This account is not available in this workspace.")
1115
+ connection = record.get("whatsapp")
1116
+ if isinstance(connection, dict) and connection.get("phone"):
1117
  by_key["whatsapp"]["state"] = "connected"
1118
 
1119
  def slack():
 
3066
  return None, False
3067
 
3068
 
3069
+ def _amazon_table_schedule(rt, connection_id=None, now=None):
3070
+ """Read account cadence metadata without restoring a mirror or reading source rows."""
3071
+ import duckdb
3072
+ from core import keychain
3073
+ from harness import amazon_store as az, datastore
3074
+
3075
+ now = now or datetime.now(timezone.utc)
3076
+ result = {"tableSchedule": [], "scheduleState": "unavailable",
3077
+ "displayTimezone": stored_business_tz(rt) or "UTC"}
3078
+
3079
+ def read(cursor, entries):
3080
+ schedule = []
3081
+ for entry in entries:
3082
+ cid = entry["connection_id"]
3083
+ prefix = az._account_prefix(cid)
3084
+ keys = [prefix + az.entity_key(family + table)
3085
+ for table in az.TABLES for family in ("", "sync_tick_", az.WATERMARK_PREFIX)]
3086
+ rows = cursor.execute(
3087
+ "SELECT entity, phase, rows, updated_at, "
3088
+ "CASE WHEN phase = 'throttled' THEN cursor_wd ELSE NULL END FROM _sync_state "
3089
+ "WHERE entity IN (SELECT unnest(?))", [keys]).fetchall()
3090
+ markers = {entity: {"state": phase, "rows": count, "updated_at": _sync_stamp(at),
3091
+ "detail": _sync_stamp(detail)}
3092
+ for entity, phase, count, at, detail in rows}
3093
+ states = {table: state for table in az.TABLES
3094
+ if (state := markers.get(prefix + az.entity_key("sync_tick_" + table)))}
3095
+ if any(not state["updated_at"] for state in states.values()):
3096
+ raise ValueError("Amazon cadence metadata has an invalid timestamp")
3097
+ # C1 owns every due time, including throttle retries and calendar periods.
3098
+ for row in az.sync_schedule(states, now):
3099
+ table = row["table"]
3100
+ tick = states.get(table) or {}
3101
+ watermark = markers.get(prefix + az.entity_key(az.WATERMARK_PREFIX + table)) or {}
3102
+ landed = markers.get(prefix + az.entity_key(table)) or {}
3103
+ completed = (tick if tick.get("state") == "done"
3104
+ and landed.get("state") not in ("refused", "failed", "throttled") else watermark)
3105
+ schedule.append({**row, "connectionId": cid, "account": entry["label"],
3106
+ "lastSyncAt": (completed.get("updated_at")
3107
+ if completed.get("state") == "done" else None),
3108
+ "state": (landed.get("state")
3109
+ if landed.get("state") in ("refused", "failed", "throttled")
3110
+ else tick.get("state") or "never_run")})
3111
+ return schedule
3112
+
3113
+ try:
3114
+ path = az._tenant_mirror_path(rt.key, rt=rt)
3115
+ if not keychain.list_entries(rt, "amazon_sp"):
3116
+ return {**result, "scheduleState": "empty"}
3117
+ entries = az.account_entries(rt.key, rt)
3118
+ cid = str(connection_id or "").strip()
3119
+ if cid and cid != "default":
3120
+ entries = [entry for entry in entries if entry["connection_id"] == cid]
3121
+ if not entries:
3122
+ return result
3123
+ if not path.is_file():
3124
+ return result
3125
+ if path.resolve() == Path(datastore.DB_PATH).resolve():
3126
+ schedule = read(datastore.ro_cursor(), entries)
3127
+ else:
3128
+ with datastore.bounded_connection(duckdb.connect(str(path), read_only=True)) as cursor:
3129
+ schedule = read(cursor, entries)
3130
+ return {**result, "tableSchedule": schedule, "scheduleState": "ready"}
3131
+ except Exception as exc:
3132
+ raise_if_resource_error(exc)
3133
+ return result
3134
+
3135
+
3136
+ def _amazon_sync_staleness(completed, schedule, now=None):
3137
+ """Use the fastest Amazon table's cadence and Odoo's existing grace period."""
3138
+ import odoo_relational as rel
3139
+ fastest = max(amazon_client.CADENCE[table]["perDay"]
3140
+ for table in amazon_client.GROUP_A)
3141
+ allowed = 86400 / fastest + rel.STALE_GRACE_SECONDS
3142
+ stamps = [row.get("lastSyncAt") for row in schedule if row["perDay"] == fastest]
3143
+ if not stamps:
3144
+ stamps = [completed]
3145
+ now = now or datetime.now(timezone.utc)
3146
+ stale = any((now - datetime.fromisoformat(stamp)).total_seconds() > allowed
3147
+ for value in stamps if (stamp := _sync_stamp(value)))
3148
+ return {"stale": stale, "staleAfterSeconds": allowed}
3149
+
3150
+
3151
  def _daily_completed_sync(rt, connector_key):
3152
  """Read the stored last good sync; never create a mirror or contact a provider."""
3153
  if connector_key == "amazon_sp":
 
3246
  if connector_key not in registered:
3247
  raise err(404, "sync_provider_unknown",
3248
  f"{connector_key} is not registered for connector sync")
3249
+ from core import keychain
3250
+ import odoo_relational as rel
3251
  rt = (_odoo_status_reads(session.runtime) if connector_key == "odoo"
3252
+ else _ConnectorStatusReads(session.runtime, (connector_sync.JOBS_KEY, connector_sync.STATE_KEY,
3253
+ connector_sync.STATUS_KEY, keychain.KEY, rel.CONFIG_KEY)
3254
  if connector_key == "amazon_sp" else ()))
3255
  resolved_connection = (_odoo_connection_slot(rt, connection_id)
3256
  if connector_key == "odoo" else connection_id)
 
3274
  if connector_key == "amazon_sp" and completed is None and known and _amazon_restore_pending(rt):
3275
  completed = _amazon_saved_completion(rt)
3276
  answer["restoring"] = True
3277
+ answer.update(lastCompletedAt=completed, lastSyncAt=completed,
3278
+ lastCompletedKnown=known, canSync=bool(session.admin))
3279
+ if connector_key == "amazon_sp":
3280
+ now = datetime.now(timezone.utc)
3281
+ schedule = _amazon_table_schedule(rt, resolved_connection, now)
3282
+ answer.update(schedule="per_table", **schedule,
3283
+ **_amazon_sync_staleness(completed, schedule["tableSchedule"], now))
3284
+ else:
3285
+ answer.update(schedule="daily", **_daily_sync_staleness(completed))
3286
  return _safe_connector_status(connector_key, answer)
3287
 
3288
 
api/routes_external.py CHANGED
@@ -153,6 +153,20 @@ def _activity_store_view(real, insert):
153
  return view
154
 
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  class _PreviewRuntime:
157
  """Lazy copy-on-write view of one tenant runtime; no method may reach a real writer.
158
 
@@ -180,11 +194,20 @@ class _PreviewRuntime:
180
  if key not in self._docs:
181
  import application_registry
182
  value = application_registry._read_strict(self._real, key)
 
 
 
 
 
 
 
 
 
183
  value = copy.deepcopy(value)
184
  self._initial[key] = copy.deepcopy(value)
185
  self._docs[key] = value
186
  try:
187
- revision = self._real.revision(key)
188
  except Exception:
189
  revision = None
190
  self._base_revisions[key] = copy.deepcopy(revision)
@@ -328,6 +351,70 @@ class _ConfirmationRuntime:
328
  def tenant(self):
329
  return getattr(self._real, "tenant", None)
330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  def update(self, name, callback, flush="sync", expect_rev=None, audit_row=None):
332
  from core import view_audit
333
  key = str(name)
@@ -1903,6 +1990,22 @@ def list_keys(session: Session = Depends(require_session)):
1903
  return {"keys": keys, "can_mint": _can_use_analyst(session)}
1904
 
1905
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1906
  @router.patch("/keys/{key_id}")
1907
  def edit_key(key_id: str, body=Body(default=None), session: Session = Depends(require_session)):
1908
  """Edit metadata and narrowing scope without ever reading or replacing the secret."""
@@ -3224,11 +3327,8 @@ def _validate_personal_view_draft(session: Session, database: str, raw: dict) ->
3224
  if any(key not in visible for key in _view_referenced_fields(config)):
3225
  raise err(400, "assistant_unknown_field",
3226
  "Every requested field must be readable in this database.")
3227
- # The ordinary Assistant validator owns filter syntax and operands. fields=[] keeps values
3228
- # out of this validation result while still applying the live database/field/row walls.
3229
- filters = config.get("filters")
3230
- _external_read_scope(
3231
- session, database, fields=[], filters=None if filters == [] else filters)
3232
 
3233
 
3234
  def _saved_view_filters(config):
@@ -3237,9 +3337,9 @@ def _saved_view_filters(config):
3237
  _assert_query_filter_bounds([value] if isinstance(value, dict) else value)
3238
  import aios_grid
3239
 
3240
- def refuse():
3241
  raise err(400, "view_filters_unsupported",
3242
- "These filters cannot be saved without changing their meaning. "
3243
  "Use colId, op and value leaves, with conj and children for nested groups.")
3244
 
3245
  def node(raw):
@@ -3251,8 +3351,17 @@ def _saved_view_filters(config):
3251
  or not isinstance(raw[key], list) or not raw[key]):
3252
  refuse()
3253
  return {"conj": raw.get("conj", "and"), "children": [node(child) for child in raw[key]]}
3254
- if (set(raw) - {"colId", "op", "value", "value2", "values", "id", "locked"}
3255
- or not isinstance(raw.get("colId"), str) or raw.get("op") not in aios_grid.FILTER_OPS):
 
 
 
 
 
 
 
 
 
3256
  refuse()
3257
  for key in ("value", "value2"):
3258
  value = raw.get(key)
 
153
  return view
154
 
155
 
156
+ def _sheet_row_revision(tx, key):
157
+ """Read the header's store revision on the same transaction as its sheet rows."""
158
+ from core import store_pg, view_audit
159
+ if isinstance(tx, sheets._PgSheetRows):
160
+ row = tx.con.execute(store_pg._q(
161
+ "SELECT rev, updated_at FROM {tbl} WHERE key = %s", "store_kv", tx.slug),
162
+ (tx.namespace + key,)).fetchone()
163
+ return store_pg._revision_value(row[0], row[1]) if row else store_pg._revision_value(None, None)
164
+ reader = getattr(type(tx), "revision", None)
165
+ if callable(reader):
166
+ return reader(tx, key)
167
+ raise view_audit.RevisionUnverifiable("The Sheet transaction cannot verify its header revision.")
168
+
169
+
170
  class _PreviewRuntime:
171
  """Lazy copy-on-write view of one tenant runtime; no method may reach a real writer.
172
 
 
194
  if key not in self._docs:
195
  import application_registry
196
  value = application_registry._read_strict(self._real, key)
197
+ revision = None
198
+ if key.startswith(sheets.WORKBOOK_KEY_PREFIX) and sheets.is_row_workbook(value):
199
+ # Hydrate through the reader, never delegate a native writer to this preview.
200
+ with sheets._sheet_rows_transaction(self._real) as tx:
201
+ if tx is None:
202
+ raise err(503, "workbook_unavailable", "The workbook snapshot is unavailable. Try again.")
203
+ head = tx.get(key)
204
+ value = sheets._verify_row_layout(tx, head) if sheets.is_row_workbook(head) else head
205
+ revision = _sheet_row_revision(tx, key)
206
  value = copy.deepcopy(value)
207
  self._initial[key] = copy.deepcopy(value)
208
  self._docs[key] = value
209
  try:
210
+ revision = revision if revision is not None else self._real.revision(key)
211
  except Exception:
212
  revision = None
213
  self._base_revisions[key] = copy.deepcopy(revision)
 
351
  def tenant(self):
352
  return getattr(self._real, "tenant", None)
353
 
354
+ @contextmanager
355
+ def sheet_rows_transaction(self, *, write=False):
356
+ """Guard native Sheet writes under the header lock with the preview's store revision."""
357
+ from core import view_audit
358
+ with sheets._sheet_rows_transaction(self._real, write=write) as tx:
359
+ if tx is None:
360
+ yield tx
361
+ return
362
+ owner = self
363
+ held = {}
364
+
365
+ class GuardedRows:
366
+ def get(self, key, *, lock=False, **selection):
367
+ if lock and not write:
368
+ raise view_audit.RevisionUnverifiable("A Sheet read cannot acquire write authority.")
369
+ value = tx.get(key, lock=lock, **selection)
370
+ if lock:
371
+ expected = owner._expected.get(key)
372
+ rev = expected.get("rev") if isinstance(expected, dict) else None
373
+ if (not key.startswith(sheets.WORKBOOK_KEY_PREFIX) or type(rev) is not int
374
+ or held):
375
+ raise view_audit.RevisionUnverifiable("The Sheet writer addressed an unpreviewed header.")
376
+ current = _sheet_row_revision(tx, key)
377
+ if current.get("rev") != rev or ("token" in expected and current.get("token") != expected["token"]):
378
+ raise view_audit.ViewRevisionStale(rev_current=current.get("rev"), rev_sent=rev)
379
+ before = (sheets._verify_row_layout(tx, value) if sheets.is_row_workbook(value)
380
+ else tx.get(key))
381
+ held.update(key=key, before=copy.deepcopy(before), head=value)
382
+ return value
383
+
384
+ def revision(self, key):
385
+ return _sheet_row_revision(tx, key)
386
+
387
+ def put(self, key, value):
388
+ header = held.get("key")
389
+ if not header or (key != header and key != sheets.sheet_row_key(
390
+ header[len(sheets.WORKBOOK_KEY_PREFIX):], value.get("id", ""))):
391
+ raise view_audit.RevisionUnverifiable("The Sheet writer addressed an unpreviewed workbook.")
392
+ if key == header and value and value.get("id") != header[len(sheets.WORKBOOK_KEY_PREFIX):]:
393
+ raise view_audit.RevisionUnverifiable("The Sheet header identity changed.")
394
+ tx.put(key, value)
395
+
396
+ def delete(self, key):
397
+ head = held.get("head") or {}
398
+ if key not in {sheets.sheet_row_key(head["id"], ref["id"])
399
+ for ref in head.get("rowSheets", [])}:
400
+ raise view_audit.RevisionUnverifiable("The Sheet writer addressed an unpreviewed sheet.")
401
+ tx.delete(key)
402
+
403
+ yield GuardedRows()
404
+ if held:
405
+ key = held["key"]
406
+ head = tx.get(key)
407
+ after = sheets._verify_row_layout(tx, head) if sheets.is_row_workbook(head) else head
408
+ produced = _sheet_row_revision(tx, key)
409
+ if held:
410
+ self._expected[key] = copy.deepcopy(produced)
411
+ if held["before"] != after:
412
+ previous = self._committed_documents.get(key)
413
+ self._committed_documents[key] = {
414
+ "before": previous["before"] if previous else held["before"],
415
+ "after": copy.deepcopy(after),
416
+ }
417
+
418
  def update(self, name, callback, flush="sync", expect_rev=None, audit_row=None):
419
  from core import view_audit
420
  key = str(name)
 
1990
  return {"keys": keys, "can_mint": _can_use_analyst(session)}
1991
 
1992
 
1993
+ @router.post("/keys/verify")
1994
+ def verify_owned_key(response: Response, body=Body(default=None), session: Session = Depends(require_session)):
1995
+ """Return a private diagnosis for this session owner's key without accepting bearer sign-in."""
1996
+ require_external_analyst(session)
1997
+ token = body.get("token") if isinstance(body, dict) else None
1998
+ if not isinstance(token, str) or len(token) > 1024:
1999
+ token = ""
2000
+ try:
2001
+ result = external_access.verify_for_owner(
2002
+ session.runtime, session.uname, session.tenant, token)
2003
+ except external_access.ExternalAccessError as exc:
2004
+ _management_refusal(exc)
2005
+ response.headers["Cache-Control"] = "no-store"
2006
+ return result
2007
+
2008
+
2009
  @router.patch("/keys/{key_id}")
2010
  def edit_key(key_id: str, body=Body(default=None), session: Session = Depends(require_session)):
2011
  """Edit metadata and narrowing scope without ever reading or replacing the secret."""
 
3327
  if any(key not in visible for key in _view_referenced_fields(config)):
3328
  raise err(400, "assistant_unknown_field",
3329
  "Every requested field must be readable in this database.")
3330
+ # Saved predicates use Table's cleaner. The reader still validates permanent permission filters.
3331
+ _external_read_scope(session, database, fields=[], filters=None)
 
 
 
3332
 
3333
 
3334
  def _saved_view_filters(config):
 
3337
  _assert_query_filter_bounds([value] if isinstance(value, dict) else value)
3338
  import aios_grid
3339
 
3340
+ def refuse(message=None):
3341
  raise err(400, "view_filters_unsupported",
3342
+ message or "These filters cannot be saved without changing their meaning. "
3343
  "Use colId, op and value leaves, with conj and children for nested groups.")
3344
 
3345
  def node(raw):
 
3351
  or not isinstance(raw[key], list) or not raw[key]):
3352
  refuse()
3353
  return {"conj": raw.get("conj", "and"), "children": [node(child) for child in raw[key]]}
3354
+ if any(key in raw for key in ("valueRef", "value2Ref")):
3355
+ refuse("Cell references cannot be saved through this tool because workbook and cell access "
3356
+ "cannot be verified here. Use literal values or a readable rhs field instead.")
3357
+ op = raw.get("op")
3358
+ if not isinstance(op, str) or op not in aios_grid.FILTER_OPS:
3359
+ name = json.dumps(op[:120]) if isinstance(op, str) else "(missing or invalid)"
3360
+ refuse(f"Unsupported filter operator {name}. Use a supported operator: "
3361
+ + ", ".join(sorted(aios_grid.FILTER_OPS)) + ".")
3362
+ if (set(raw) - {"colId", "op", "value", "value2", "values", "id", "locked",
3363
+ "dateWindow", "dateMode", "rhs"}
3364
+ or not isinstance(raw.get("colId"), str)):
3365
  refuse()
3366
  for key in ("value", "value2"):
3367
  value = raw.get(key)
api/routes_odoo_tables.py CHANGED
@@ -1393,6 +1393,22 @@ def require_amazon_mirror(table_key, session, definition=None, st=None):
1393
  cur.close()
1394
 
1395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1396
  register_source_provider(_odoo_sources)
1397
  register_source_provider(_amazon_sources)
1398
  register_source_provider(_meta_sources)
@@ -1609,6 +1625,8 @@ def _amazon_page_source(cur, key, definition=None, *, binding=False, allow_looku
1609
  """Resolve one admitted Amazon target and keep query failures distinct from stored tables."""
1610
  import amazon_relational as amazon
1611
 
 
 
1612
  try:
1613
  if binding:
1614
  columns = amazon.mirror_columns(cur, amazon.SOURCE[key], strict=True)
@@ -1621,7 +1639,7 @@ def _amazon_page_source(cur, key, definition=None, *, binding=False, allow_looku
1621
  raise err(503, "amazon_source_unavailable",
1622
  "This Amazon database could not be resolved. Retry the request.") from exc
1623
  if spec is not None and spec.get("missing"):
1624
- raise err(503, "amazon_mirror_not_loaded", spec["missing"])
1625
  if spec is not None and '_connection_id' in {
1626
  name for name, _kind in amazon.mirror_columns(cur, amazon.SOURCE[key], strict=True)}:
1627
  spec = {**spec, 'cols': {**spec['cols'], '_connection_id': (
@@ -1657,7 +1675,10 @@ def _source_for(cur, table_key, *, allow_lookups=True):
1657
  if module_cache_source(key) is not None:
1658
  return None
1659
  if key.startswith("ut_odoo_model_"):
1660
- return _rel().dynamic_model_source(cur, key)
 
 
 
1661
  if key.startswith('ut_meta_'):
1662
  return _meta_source(cur, key)
1663
  if key.startswith("ut_amzn_"):
@@ -1673,7 +1694,7 @@ def _source_for(cur, table_key, *, allow_lookups=True):
1673
  if not spec:
1674
  return None
1675
  if spec.get("missing"):
1676
- raise err(503, "amazon_mirror_not_loaded", spec["missing"])
1677
  from harness import datastore
1678
  if _rel().base_table_key(table_key) == _rel().BILLS_KEY:
1679
  try:
@@ -1712,7 +1733,7 @@ def _source_for(cur, table_key, *, allow_lookups=True):
1712
  if not cols:
1713
  if key.startswith("ut_amzn_"):
1714
  import amazon_relational
1715
- raise err(503, "amazon_mirror_not_loaded",
1716
  amazon_relational.missing_source(key)["missing"])
1717
  return None # the mirror has no such table on this deployment
1718
  have_by_alias[alias] = cols
@@ -2642,16 +2663,33 @@ def _materialised_table(table_key, *, cur=None, st=None):
2642
  if module_cache_source(table_key) is not None:
2643
  return True
2644
  if str(table_key).startswith("ut_odoo_model_"):
2645
- if st is not None and (_ut().get_definition(table_key, st=st) or {}).get("readThrough") is not True:
 
 
2646
  return True
2647
  own_cursor = cur is None
2648
  if own_cursor:
2649
  if st is None:
2650
  return True
2651
- cur = _table_cursor(st, table_key)
 
 
 
 
 
 
 
2652
  try:
2653
- return _rel().dynamic_model_source(cur, table_key,
2654
- tenant_key=getattr(st, "key", None)) is None
 
 
 
 
 
 
 
 
2655
  finally:
2656
  if own_cursor and cur is not None:
2657
  cur.close()
@@ -2663,7 +2701,7 @@ def _materialised_table(table_key, *, cur=None, st=None):
2663
  own_cursor = cur is None
2664
  cur = datastore.ro_cursor() if own_cursor else cur
2665
  try:
2666
- definition = _ut().get(table_key, st=st) if st is not None else None
2667
  spec = _amazon_page_source(cur, table_key, definition, allow_lookups=False)
2668
  return spec is None
2669
  finally:
@@ -4009,6 +4047,8 @@ def resolved_fields(table_key, defn):
4009
  if str(table_key).startswith("ut_amzn_"):
4010
  import amazon_relational as amazon
4011
  connected = table_key in amazon.SOURCE
 
 
4012
  else:
4013
  connected = table_key in _sources()
4014
  if not connected:
@@ -4108,7 +4148,7 @@ def _compiled_read(table_key, session, *, filters=None, filterConj="and", sorts=
4108
  # try, not inside it.
4109
  try:
4110
  cur, mismatch = (_mirror_cur(session, table_key)
4111
- if (table_key.startswith(("ut_amzn_", "ut_meta_"))
4112
  or rel.base_table_key(table_key) == rel.SHIPMENTS_KEY
4113
  or rel.base_table_key(table_key) in rel.stock_source_specs())
4114
  else _mirror_cur(session))
@@ -5117,8 +5157,10 @@ def odoo_table_rows(table_key: str,
5117
  ids = _record_ids(body)
5118
 
5119
  # Admission resolves this Amazon database without publishing a process-wide binding.
5120
- page = _page_defs(session) if str(table_key).startswith("ut_amzn_") else None
5121
  admitted = require_amazon_mirror(table_key, session, st=page[0] if page else None)
 
 
5122
  if (admitted is None if page is not None else _materialised_table(table_key, st=session.runtime)):
5123
  return _audit_personal_response(table_key, _materialised_window(
5124
  table_key, session, offset, limit, filters, filterConj,
@@ -5746,8 +5788,10 @@ def odoo_table_aggregates(table_key: str, body: dict = Body(default=None),
5746
  if group_limit is not None and (type(group_limit) is not int or not 1 <= group_limit <= MAX_GROUPS):
5747
  raise err(400, "bad_selection", f"groupLimit must be a page size from 1 to {MAX_GROUPS}")
5748
 
5749
- page = _page_defs(session) if str(table_key).startswith("ut_amzn_") else None
5750
  admitted = require_amazon_mirror(table_key, session, st=page[0] if page else None)
 
 
5751
 
5752
  if (admitted is None if page is not None else _materialised_table(table_key, st=session.runtime)):
5753
  with _materialised_read(table_key, session, query, payload.get("related"), ids=ids) as read:
 
1393
  cur.close()
1394
 
1395
 
1396
+ def require_dynamic_odoo_mirror(table_key, session, st=None):
1397
+ """Admit a saved dynamic definition before resolving its publication or refusal."""
1398
+ if not str(table_key).startswith('ut_odoo_model_'):
1399
+ return None
1400
+ from routes_tables import _defn_or_refuse
1401
+ try:
1402
+ _defn_or_refuse(session, table_key, st=st, defs_only=True, scope_applied=True)
1403
+ except HTTPException as exc:
1404
+ if exc.status_code == 404:
1405
+ raise err(404, 'odoo_definition_not_published',
1406
+ 'This Odoo database has no published definition. Enable it in connection '
1407
+ 'settings and complete its first sync before reading rows.') from exc
1408
+ raise
1409
+ return not _materialised_table(table_key, st=st if st is not None else session.runtime)
1410
+
1411
+
1412
  register_source_provider(_odoo_sources)
1413
  register_source_provider(_amazon_sources)
1414
  register_source_provider(_meta_sources)
 
1625
  """Resolve one admitted Amazon target and keep query failures distinct from stored tables."""
1626
  import amazon_relational as amazon
1627
 
1628
+ if (definition or {}).get('oversize'):
1629
+ definition = {**definition, 'readThrough': True}
1630
  try:
1631
  if binding:
1632
  columns = amazon.mirror_columns(cur, amazon.SOURCE[key], strict=True)
 
1639
  raise err(503, "amazon_source_unavailable",
1640
  "This Amazon database could not be resolved. Retry the request.") from exc
1641
  if spec is not None and spec.get("missing"):
1642
+ raise err(409, "amazon_mirror_not_loaded", spec["missing"])
1643
  if spec is not None and '_connection_id' in {
1644
  name for name, _kind in amazon.mirror_columns(cur, amazon.SOURCE[key], strict=True)}:
1645
  spec = {**spec, 'cols': {**spec['cols'], '_connection_id': (
 
1675
  if module_cache_source(key) is not None:
1676
  return None
1677
  if key.startswith("ut_odoo_model_"):
1678
+ try:
1679
+ return _rel().dynamic_model_source(cur, key)
1680
+ except _rel().Refused as exc:
1681
+ raise err(409, 'odoo_mirror_binding_incomplete', str(exc)) from exc
1682
  if key.startswith('ut_meta_'):
1683
  return _meta_source(cur, key)
1684
  if key.startswith("ut_amzn_"):
 
1694
  if not spec:
1695
  return None
1696
  if spec.get("missing"):
1697
+ raise err(409, "amazon_mirror_not_loaded", spec["missing"])
1698
  from harness import datastore
1699
  if _rel().base_table_key(table_key) == _rel().BILLS_KEY:
1700
  try:
 
1733
  if not cols:
1734
  if key.startswith("ut_amzn_"):
1735
  import amazon_relational
1736
+ raise err(409, "amazon_mirror_not_loaded",
1737
  amazon_relational.missing_source(key)["missing"])
1738
  return None # the mirror has no such table on this deployment
1739
  have_by_alias[alias] = cols
 
2663
  if module_cache_source(table_key) is not None:
2664
  return True
2665
  if str(table_key).startswith("ut_odoo_model_"):
2666
+ definition = _ut().get_definition(table_key, st=st) if st is not None else {}
2667
+ mandatory = bool((definition or {}).get('readThrough') or (definition or {}).get('oversize'))
2668
+ if st is not None and not mandatory:
2669
  return True
2670
  own_cursor = cur is None
2671
  if own_cursor:
2672
  if st is None:
2673
  return True
2674
+ try:
2675
+ cur = _table_cursor(st, table_key)
2676
+ except _rt().DatastoreMismatch as exc:
2677
+ raise err(409, 'cross_tenant_store', str(exc)) from exc
2678
+ except _rel().Refused as exc:
2679
+ raise err(409, 'odoo_mirror_binding_incomplete', str(exc)) from exc
2680
+ except RuntimeError as exc:
2681
+ raise err(409, 'odoo_mirror_not_loaded', str(exc)) from exc
2682
  try:
2683
+ try:
2684
+ spec = _rel().dynamic_model_source(cur, table_key,
2685
+ tenant_key=getattr(st, "key", None))
2686
+ except _rel().Refused as exc:
2687
+ raise err(409, 'odoo_mirror_binding_incomplete', str(exc)) from exc
2688
+ if mandatory and spec is None:
2689
+ raise err(409, 'odoo_mirror_not_loaded',
2690
+ 'This Odoo database has no complete mirror publication. Complete its '
2691
+ 'sync in connection settings before reading rows.')
2692
+ return spec is None
2693
  finally:
2694
  if own_cursor and cur is not None:
2695
  cur.close()
 
2701
  own_cursor = cur is None
2702
  cur = datastore.ro_cursor() if own_cursor else cur
2703
  try:
2704
+ definition = _ut().get_definition(table_key, st=st) if st is not None else None
2705
  spec = _amazon_page_source(cur, table_key, definition, allow_lookups=False)
2706
  return spec is None
2707
  finally:
 
4047
  if str(table_key).startswith("ut_amzn_"):
4048
  import amazon_relational as amazon
4049
  connected = table_key in amazon.SOURCE
4050
+ elif str(base).startswith('ut_odoo_model_'):
4051
+ connected = True
4052
  else:
4053
  connected = table_key in _sources()
4054
  if not connected:
 
4148
  # try, not inside it.
4149
  try:
4150
  cur, mismatch = (_mirror_cur(session, table_key)
4151
+ if (table_key.startswith(("ut_amzn_", "ut_meta_", "ut_odoo_model_"))
4152
  or rel.base_table_key(table_key) == rel.SHIPMENTS_KEY
4153
  or rel.base_table_key(table_key) in rel.stock_source_specs())
4154
  else _mirror_cur(session))
 
5157
  ids = _record_ids(body)
5158
 
5159
  # Admission resolves this Amazon database without publishing a process-wide binding.
5160
+ page = _page_defs(session) if str(table_key).startswith(("ut_amzn_", "ut_odoo_model_")) else None
5161
  admitted = require_amazon_mirror(table_key, session, st=page[0] if page else None)
5162
+ if str(table_key).startswith('ut_odoo_model_'):
5163
+ admitted = True if require_dynamic_odoo_mirror(table_key, session, st=page[0]) else None
5164
  if (admitted is None if page is not None else _materialised_table(table_key, st=session.runtime)):
5165
  return _audit_personal_response(table_key, _materialised_window(
5166
  table_key, session, offset, limit, filters, filterConj,
 
5788
  if group_limit is not None and (type(group_limit) is not int or not 1 <= group_limit <= MAX_GROUPS):
5789
  raise err(400, "bad_selection", f"groupLimit must be a page size from 1 to {MAX_GROUPS}")
5790
 
5791
+ page = _page_defs(session) if str(table_key).startswith(("ut_amzn_", "ut_odoo_model_")) else None
5792
  admitted = require_amazon_mirror(table_key, session, st=page[0] if page else None)
5793
+ if str(table_key).startswith('ut_odoo_model_'):
5794
+ admitted = True if require_dynamic_odoo_mirror(table_key, session, st=page[0]) else None
5795
 
5796
  if (admitted is None if page is not None else _materialised_table(table_key, st=session.runtime)):
5797
  with _materialised_read(table_key, session, query, payload.get("related"), ids=ids) as read:
api/sync_worker.py CHANGED
@@ -35,9 +35,10 @@ def configure(env=None):
35
  worker_db = worker_dir / f"{safe}.duckdb"
36
  env["AIOS_DUCKDB_STORE_DIR"] = str(worker_dir)
37
  env["AIOS_DUCKDB_PATH"] = str(worker_db)
38
- # The launcher (request_class.py --env) sizes these to the box. Unset: the 2 vCPU values.
39
- env["AIOS_DUCKDB_MEMORY"] = env.get("AIOS_WORKER_DUCKDB_MEMORY") or "4GB"
40
  env["AIOS_DUCKDB_THREADS"] = env.get("AIOS_WORKER_DUCKDB_THREADS") or "1"
 
41
  # The worker holds its own file read-write. Mirror versions are for the web workers only.
42
  env["AIOS_MIRROR_VERSIONS"] = "0"
43
  env["AIOS_UVICORN_WORKERS"] = "1"
@@ -129,6 +130,7 @@ def main(argv=None):
129
  print(f"[sync-worker] pid={os.getpid()} tenant={tenant} "
130
  f"scope={os.environ.get('AIOS_WORKER_SCOPE') or 'primary'} duckdb={worker_db} "
131
  f"memory_limit={os.environ['AIOS_DUCKDB_MEMORY']} threads={os.environ['AIOS_DUCKDB_THREADS']} "
 
132
  f"nice={niceness} job={mode or 'none'}",
133
  flush=True)
134
  print("[sync-worker] amazon_store.sync includes Brand Analytics reports in the worker role", flush=True)
 
35
  worker_db = worker_dir / f"{safe}.duckdb"
36
  env["AIOS_DUCKDB_STORE_DIR"] = str(worker_dir)
37
  env["AIOS_DUCKDB_PATH"] = str(worker_db)
38
+ # Leave room for Python and the web processes when a full Brand Analytics week is parsed.
39
+ env["AIOS_DUCKDB_MEMORY"] = env.get("AIOS_WORKER_DUCKDB_MEMORY") or "6GB"
40
  env["AIOS_DUCKDB_THREADS"] = env.get("AIOS_WORKER_DUCKDB_THREADS") or "1"
41
+ env["AIOS_DUCKDB_MAX_TEMP_SIZE"] = env.get("AIOS_WORKER_DUCKDB_MAX_TEMP_SIZE") or "16GB"
42
  # The worker holds its own file read-write. Mirror versions are for the web workers only.
43
  env["AIOS_MIRROR_VERSIONS"] = "0"
44
  env["AIOS_UVICORN_WORKERS"] = "1"
 
130
  print(f"[sync-worker] pid={os.getpid()} tenant={tenant} "
131
  f"scope={os.environ.get('AIOS_WORKER_SCOPE') or 'primary'} duckdb={worker_db} "
132
  f"memory_limit={os.environ['AIOS_DUCKDB_MEMORY']} threads={os.environ['AIOS_DUCKDB_THREADS']} "
133
+ f"spill_limit={os.environ['AIOS_DUCKDB_MAX_TEMP_SIZE']} "
134
  f"nice={niceness} job={mode or 'none'}",
135
  flush=True)
136
  print("[sync-worker] amazon_store.sync includes Brand Analytics reports in the worker role", flush=True)
api/test_odoo_rank_refresh.py CHANGED
@@ -353,7 +353,7 @@ class RankRefreshTests(unittest.TestCase):
353
  # own duration (hotfix 2026-09-29), so a clock that jumped a whole cadence inside
354
  # every pass would ask for a zero wait.
355
  clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60,
356
- strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
357
  ns = dict(os=os, _restore_sync_request_desks=lambda: None, RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], _SEED_THREAD=None,
358
  _configured_mirror_entities=lambda *_: None, freeze_notice=FREEZE_OFF,
359
  _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False,
@@ -675,7 +675,7 @@ class RankRefreshTests(unittest.TestCase):
675
  return rebuild_ok
676
 
677
  clock = SimpleNamespace(sleep=sleep, time=lambda: now,
678
- strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
679
  ns = dict(os=os, _restore_sync_request_desks=lambda: None, RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], _SEED_THREAD=None,
680
  _configured_mirror_entities=lambda *_: None,
681
  freeze_notice=SimpleNamespace(enabled=lambda: frozen),
@@ -957,7 +957,7 @@ class RankRefreshTests(unittest.TestCase):
957
  if status is not None:
958
  store.docs[rel._ut().ODOO_SYNC_STATUS_KEY] = dict(status)
959
  clock = SimpleNamespace(sleep=wait, monotonic=lambda: now[0], time=lambda: now[0],
960
- strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
961
  ns = dict(os=os, _restore_sync_request_desks=lambda: None, RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], _SEED_THREAD=None,
962
  _configured_mirror_entities=lambda *_: None, freeze_notice=FREEZE_OFF,
963
  _resolve_resync_cadence=lambda: dict(
 
353
  # own duration (hotfix 2026-09-29), so a clock that jumped a whole cadence inside
354
  # every pass would ask for a zero wait.
355
  clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60,
356
+ strftime=lambda *_: 'synthetic-time', gmtime=lambda *_: ())
357
  ns = dict(os=os, _restore_sync_request_desks=lambda: None, RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], _SEED_THREAD=None,
358
  _configured_mirror_entities=lambda *_: None, freeze_notice=FREEZE_OFF,
359
  _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False,
 
675
  return rebuild_ok
676
 
677
  clock = SimpleNamespace(sleep=sleep, time=lambda: now,
678
+ strftime=lambda *_: 'synthetic-time', gmtime=lambda *_: ())
679
  ns = dict(os=os, _restore_sync_request_desks=lambda: None, RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], _SEED_THREAD=None,
680
  _configured_mirror_entities=lambda *_: None,
681
  freeze_notice=SimpleNamespace(enabled=lambda: frozen),
 
957
  if status is not None:
958
  store.docs[rel._ut().ODOO_SYNC_STATUS_KEY] = dict(status)
959
  clock = SimpleNamespace(sleep=wait, monotonic=lambda: now[0], time=lambda: now[0],
960
+ strftime=lambda *_: 'synthetic-time', gmtime=lambda *_: ())
961
  ns = dict(os=os, _restore_sync_request_desks=lambda: None, RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], _SEED_THREAD=None,
962
  _configured_mirror_entities=lambda *_: None, freeze_notice=FREEZE_OFF,
963
  _resolve_resync_cadence=lambda: dict(
api/tool_groups/apps.py CHANGED
@@ -470,7 +470,15 @@ def apply_template(ctx, args):
470
  allowed_databases=allowed, semantic_records=records,
471
  field_contracts=_fields(ctx, allowed), **attribution)
472
  return _result(result)
473
- except (applications.ApplicationError, PermissionError) as exc:
 
 
 
 
 
 
 
 
474
  _refuse(exc)
475
 
476
 
 
470
  allowed_databases=allowed, semantic_records=records,
471
  field_contracts=_fields(ctx, allowed), **attribution)
472
  return _result(result)
473
+ except applications.ApplicationError as exc:
474
+ # Keep typed refusals and inaccessible-App responses on their original path.
475
+ if type(exc) is applications.ApplicationError and "does not exist" not in str(exc):
476
+ kind = json.dumps(args["templateKind"], ensure_ascii=True)
477
+ exc = applications.ApplicationError(
478
+ f"Cannot apply templateKind {kind}. {str(exc).rstrip('.')}. "
479
+ "Check this template's inputs with apps.templates and retry.")
480
+ _refuse(exc)
481
+ except PermissionError as exc:
482
  _refuse(exc)
483
 
484
 
platform/core/activity_log.py CHANGED
@@ -12,7 +12,7 @@ import re
12
 
13
  DATABASE_KEY = 'ut_activity'
14
  SYSTEM_KIND = 'activity'
15
- KINDS = frozenset(('record', 'field', 'cell', 'file', 'app', 'view'))
16
  OBJECT_TYPES = frozenset(('database', 'workbook', 'file', 'app'))
17
  CHANGES_MAX_BYTES = 4096
18
  RETENTION_MONTHS = 13
@@ -242,7 +242,7 @@ def seal_copy_batch(rt, events, outbox, *, backup_path, definitions=None):
242
  def append(rt, *, kind, object, by, via=None, on_behalf_of=None, changes=None, _delivery=None):
243
  """Append one C5 event and remove at most PRUNE_BATCH expired events in the same transaction."""
244
  if kind not in KINDS:
245
- raise ValueError('kind must be record, field, cell, file, app or view.')
246
  object_type, object_key, object_id = _object(object)
247
  by = _text(by, 'by')
248
  via = _text(via, 'via', optional=True)
@@ -522,12 +522,13 @@ class PgActivityStore:
522
  needed = {f'{self.name}_{suffix}' for suffix in (
523
  'delivery', 'time', 'object_time', 'object_id_time', 'person_time')}
524
  if (set(_COLUMNS) | {'delivery_key', 'delivery_version'} <= {r[0] for r in columns}
525
- and needed <= {r[0] for r in indexes} and constraint and "'view'" in constraint[0]):
 
526
  return
527
  con.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))', (self.table,))
528
  con.execute(f'''CREATE TABLE IF NOT EXISTS {self.table} (
529
  id BIGSERIAL PRIMARY KEY, occurred_at TIMESTAMPTZ NOT NULL,
530
- kind TEXT NOT NULL CHECK (kind IN ('record','field','cell','file','app','view')),
531
  object_type TEXT NOT NULL, object_key TEXT NOT NULL, object_id TEXT NOT NULL,
532
  actor TEXT NOT NULL, via TEXT, on_behalf_of TEXT,
533
  changes JSONB NOT NULL, changes_truncated BOOLEAN NOT NULL,
@@ -541,11 +542,11 @@ class PgActivityStore:
541
  'SELECT pg_get_constraintdef(oid) FROM pg_constraint '
542
  'WHERE conrelid = %s::regclass AND conname = %s',
543
  (self.table, constraint)).fetchone()
544
- if prior is None or "'view'" not in prior[0]:
545
  con.execute(f'ALTER TABLE {self.table} DROP CONSTRAINT IF EXISTS "{constraint}"')
546
  # The former constraint already admitted every existing event kind.
547
  con.execute(f'ALTER TABLE {self.table} ADD CONSTRAINT "{constraint}" '
548
- "CHECK (kind IN ('record','field','cell','file','app','view')) NOT VALID")
549
  for suffix, columns in (
550
  ('time', 'occurred_at DESC, id DESC'),
551
  ('object_time', 'object_type, object_key, occurred_at DESC, id DESC'),
 
12
 
13
  DATABASE_KEY = 'ut_activity'
14
  SYSTEM_KIND = 'activity'
15
+ KINDS = frozenset(('record', 'field', 'cell', 'file', 'app', 'view', 'view_as'))
16
  OBJECT_TYPES = frozenset(('database', 'workbook', 'file', 'app'))
17
  CHANGES_MAX_BYTES = 4096
18
  RETENTION_MONTHS = 13
 
242
  def append(rt, *, kind, object, by, via=None, on_behalf_of=None, changes=None, _delivery=None):
243
  """Append one C5 event and remove at most PRUNE_BATCH expired events in the same transaction."""
244
  if kind not in KINDS:
245
+ raise ValueError('kind must be record, field, cell, file, app, view or view_as.')
246
  object_type, object_key, object_id = _object(object)
247
  by = _text(by, 'by')
248
  via = _text(via, 'via', optional=True)
 
522
  needed = {f'{self.name}_{suffix}' for suffix in (
523
  'delivery', 'time', 'object_time', 'object_id_time', 'person_time')}
524
  if (set(_COLUMNS) | {'delivery_key', 'delivery_version'} <= {r[0] for r in columns}
525
+ and needed <= {r[0] for r in indexes} and constraint
526
+ and "'view_as'" in constraint[0]):
527
  return
528
  con.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))', (self.table,))
529
  con.execute(f'''CREATE TABLE IF NOT EXISTS {self.table} (
530
  id BIGSERIAL PRIMARY KEY, occurred_at TIMESTAMPTZ NOT NULL,
531
+ kind TEXT NOT NULL CHECK (kind IN ('record','field','cell','file','app','view','view_as')),
532
  object_type TEXT NOT NULL, object_key TEXT NOT NULL, object_id TEXT NOT NULL,
533
  actor TEXT NOT NULL, via TEXT, on_behalf_of TEXT,
534
  changes JSONB NOT NULL, changes_truncated BOOLEAN NOT NULL,
 
542
  'SELECT pg_get_constraintdef(oid) FROM pg_constraint '
543
  'WHERE conrelid = %s::regclass AND conname = %s',
544
  (self.table, constraint)).fetchone()
545
+ if prior is None or "'view_as'" not in prior[0]:
546
  con.execute(f'ALTER TABLE {self.table} DROP CONSTRAINT IF EXISTS "{constraint}"')
547
  # The former constraint already admitted every existing event kind.
548
  con.execute(f'ALTER TABLE {self.table} ADD CONSTRAINT "{constraint}" '
549
+ "CHECK (kind IN ('record','field','cell','file','app','view','view_as')) NOT VALID")
550
  for suffix, columns in (
551
  ('time', 'occurred_at DESC, id DESC'),
552
  ('object_time', 'object_type, object_key, occurred_at DESC, id DESC'),
platform/core/amazon_project_results.py CHANGED
@@ -1,4 +1,4 @@
1
- """Typed project result configurations and tenant-day completion windows."""
2
 
3
  from datetime import date, timedelta
4
  import hashlib
@@ -13,6 +13,121 @@ METRICS = ('sales', 'units', 'returns', 'return_rate', 'refund_rate', 'roas')
13
  GRAINS = ('sku', 'asin', 'parent_asin')
14
  MAX_CONFIGS = 20
15
  BOUNDARY = '[completedOn, endExclusive) in the tenant timezone. Today is excluded.'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
 
18
  def configurations(values):
 
1
+ """Typed project results and daily series with independent source reconciliation."""
2
 
3
  from datetime import date, timedelta
4
  import hashlib
 
13
  GRAINS = ('sku', 'asin', 'parent_asin')
14
  MAX_CONFIGS = 20
15
  BOUNDARY = '[completedOn, endExclusive) in the tenant timezone. Today is excluded.'
16
+ SERIES_METRICS = (
17
+ ('sales', 'Sales', 'currency'), ('units', 'Units', 'units'),
18
+ ('margin', 'Margin', 'percent'), ('returns', 'Returns', 'units'),
19
+ ('returns_pct', 'Returns %', 'percent'), ('refunds_pct', 'Refunds %', 'percent'),
20
+ ('roas', 'ROAS', 'ratio'), ('sales_yoy', 'Sales YoY', 'percent'),
21
+ )
22
+ SERIES_BOUNDARY = 'Inclusive requested dates in the tenant timezone. Today is incomplete and excluded from totals.'
23
+
24
+
25
+ class ValidationFailed(RuntimeError):
26
+ """A daily project series does not reconcile to its independent source aggregate."""
27
+
28
+
29
+ def series_scope(value='all'):
30
+ """Parse the modal filter without allowing it to widen project membership."""
31
+ if value == 'all':
32
+ return None
33
+ if not isinstance(value, str) or ':' not in value:
34
+ raise ValueError('scope must be all, sku:<keys>, asin:<key> or parent:<key>.')
35
+ grain, text = value.split(':', 1)
36
+ keys = [key.strip() for key in text.split(',')]
37
+ if (grain not in ('sku', 'asin', 'parent') or not all(keys)
38
+ or (grain != 'sku' and len(keys) != 1)):
39
+ raise ValueError('scope must be all, sku:<keys>, asin:<key> or parent:<key>.')
40
+ return {'grain': grain, 'keys': list(dict.fromkeys(keys))}
41
+
42
+
43
+ def series_window(project, start=None, end=None, *, today=None):
44
+ """Default to thirty days before start through today and aggregate only completed tenant days."""
45
+ today = today or periods.today()
46
+
47
+ def parsed(value):
48
+ if not isinstance(value, str) or not re.fullmatch(r'\d{4}-\d{2}-\d{2}', value):
49
+ raise ValueError('Series dates must use YYYY-MM-DD.')
50
+ return date.fromisoformat(value)
51
+
52
+ first = (parsed(start) if start is not None else
53
+ parsed(project.get('start_on') or today.isoformat()) - timedelta(days=30))
54
+ last = parsed(end) if end is not None else today
55
+ if first > last or last > today:
56
+ raise ValueError('Series dates must be ordered and cannot end after today.')
57
+ stop = min(last + timedelta(days=1), today)
58
+ lo, hi, rule = periods.yoy_window(first.isoformat(), stop.isoformat(), 'day')
59
+ return {'from': first.isoformat(), 'to': last.isoformat(), 'days': (stop - first).days,
60
+ 'current': (first, stop), 'comparison': (date.fromisoformat(lo), date.fromisoformat(hi)),
61
+ 'compare_rule': rule}
62
+
63
+
64
+ def _finite(value):
65
+ return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
66
+
67
+
68
+ def validate(answer, *, independent):
69
+ """Requery independent totals and return failing checks for any changed or unproved series."""
70
+ checks = []
71
+ first, last = date.fromisoformat(answer['from']), date.fromisoformat(answer['to'])
72
+ dates = [(first + timedelta(days=i)).isoformat() for i in range((last - first).days + 1)]
73
+ expected_keys = [key for key, _label, _unit in SERIES_METRICS]
74
+ metrics = answer['metrics']
75
+ if [metric['key'] for metric in metrics] != expected_keys:
76
+ return [{'check': 'series total ties to economics', 'ok': False,
77
+ 'reason': 'Series metrics do not follow the project result contract.'}]
78
+ for metric in metrics:
79
+ ours = metric.get('total')
80
+ source = independent(metric['key'])
81
+ expected, source_reason = source['value'], source.get('reason')
82
+ points = metric.get('points', [])
83
+ ok = [point.get('date') for point in points] == dates
84
+ if ours is None:
85
+ ok = ok and bool(metric.get('reason')) and all(point.get('value') is None for point in points)
86
+ else:
87
+ ok = (ok and _finite(ours) and _finite(expected)
88
+ and math.isclose(ours, expected, rel_tol=1e-9, abs_tol=1e-6)
89
+ and all(point.get('value') is None or _finite(point['value']) for point in points))
90
+ if metric['key'] in ('sales', 'units', 'returns'):
91
+ values = [point['value'] for point in points if point.get('value') is not None]
92
+ ok = ok and bool(values) and math.isclose(math.fsum(values), ours, rel_tol=1e-9, abs_tol=1e-6)
93
+ tie = metric.get('tie', {})
94
+ if tie.get('ok') or tie.get('ours') is not None or tie.get('independent') is not None:
95
+ ok = (ok and ours is not None and tie.get('ok') is True
96
+ and _finite(tie.get('ours')) and _finite(tie.get('independent'))
97
+ and math.isclose(tie['ours'], ours, rel_tol=1e-9, abs_tol=1e-6)
98
+ and _finite(expected)
99
+ and math.isclose(tie['independent'], expected, rel_tol=1e-9, abs_tol=1e-6))
100
+ checks.append({'check': 'series total ties to economics', 'metric': metric['key'], 'ok': bool(ok),
101
+ 'ours': ours, 'independent': expected,
102
+ 'reason': metric.get('reason') or source_reason})
103
+ return checks
104
+
105
+
106
+ def series(project, *, bounds, read, independent):
107
+ """Publish the fixed chart order and project markers after independent economics reconciliation."""
108
+ first, last = date.fromisoformat(bounds['from']), date.fromisoformat(bounds['to'])
109
+ dates = [(first + timedelta(days=i)).isoformat() for i in range((last - first).days + 1)]
110
+ answer = {'projectId': project['id'], 'from': bounds['from'], 'to': bounds['to'],
111
+ 'timezone': timezone_name(), 'boundary': SERIES_BOUNDARY,
112
+ 'markers': {'start': project.get('start_on'), 'target': project.get('target_on'),
113
+ 'completed': project.get('completed_on')}, 'metrics': []}
114
+ for key, label, unit in SERIES_METRICS:
115
+ measured = read(key)
116
+ total, reason = measured.get('total'), measured.get('reason')
117
+ if total is None and not reason:
118
+ reason = 'An independent daily source for this metric is unavailable.'
119
+ values = {point['date']: point['value'] for point in measured.get('points', [])} if total is not None else {}
120
+ answer['metrics'].append({'key': key, 'label': label, 'unit': measured.get('unit', unit),
121
+ 'points': [{'date': day, 'value': values.get(day)} for day in dates],
122
+ 'total': total, 'reason': reason,
123
+ 'tie': {'ok': False, 'ours': None, 'independent': None}})
124
+ checks = validate(answer, independent=independent)
125
+ if any(not check['ok'] for check in checks):
126
+ raise ValidationFailed('A series total does not tie to its independent Amazon economics aggregate.')
127
+ for metric, check in zip(answer['metrics'], checks):
128
+ if metric['total'] is not None:
129
+ metric['tie'] = {'ok': True, 'ours': check['ours'], 'independent': check['independent']}
130
+ return answer
131
 
132
 
133
  def configurations(values):
platform/core/amazon_projects.py CHANGED
@@ -250,10 +250,11 @@ def health(project, today):
250
 
251
 
252
  def summary(projects):
253
- """R19 and R67: average progress and "N of M completed", with Canceled left out of M."""
254
  counted = [p for p in projects if p.get('status') != CANCELED]
255
  total = len(counted)
256
- return {'average': round(sum(p['progress'] for p in counted) / total) if total else None,
 
257
  'completed': sum(1 for p in counted if p.get('status') == COMPLETED), 'total': total}
258
 
259
 
 
250
 
251
 
252
  def summary(projects):
253
+ """Average progress with Completed at 100 and Canceled excluded."""
254
  counted = [p for p in projects if p.get('status') != CANCELED]
255
  total = len(counted)
256
+ return {'average': round(sum(100 if p.get('status') == COMPLETED else p['progress']
257
+ for p in counted) / total) if total else None,
258
  'completed': sum(1 for p in counted if p.get('status') == COMPLETED), 'total': total}
259
 
260
 
platform/core/connector_sync.py CHANGED
@@ -162,6 +162,23 @@ def sync_not_before(provider, tenant):
162
  return _SYNC_NOT_BEFORE.get((provider, tenant))
163
 
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  def register_request_listener(fn):
166
  """Call fn(receipt, connection_id) after each accepted ordinary request, outside the desk lock."""
167
  if fn not in _REQUEST_LISTENERS:
@@ -221,7 +238,13 @@ def _request_sync(provider, tenant, *, requested_by="", now=None, not_before=Non
221
  _wake_sync_requests(stamp)
222
  return {**result, "accepted": accepted_id == identity, "duplicate": accepted_id != identity}
223
  with _SYNC_REQUEST_LOCK:
 
 
 
224
  previous = _SYNC_REQUESTS.get((provider, tenant))
 
 
 
225
  if previous and previous["state"] in ("queued", "running"):
226
  if not (previous["state"] == "queued" and previous.get("requestedBy") == "first load"
227
  and requested_by != "first load"):
@@ -230,7 +253,7 @@ def _request_sync(provider, tenant, *, requested_by="", now=None, not_before=Non
230
  "requestedAt": time.time() if now is None else now,
231
  "notBefore": max(float(not_before or 0), _SYNC_NOT_BEFORE.get((provider, tenant), 0),
232
  float(previous.get("notBefore") or 0) if previous
233
- and previous["state"] == "queued" else 0) or None,
234
  "startedAt": None, "finishedAt": None, "detail": "",
235
  "requestedBy": str(requested_by or ""), "accepted": True}
236
  if requested_by == "first load" and previous and previous.get("firstLoad"):
@@ -471,6 +494,9 @@ def poll_remote_requests(rt, *, now=None):
471
  if stamp - float(entry.get("finishedAt") or 0) > REMOTE_KEEP_SECONDS:
472
  pruned.append(identity)
473
  continue
 
 
 
474
  receipt = _SYNC_REQUESTS.get(key)
475
  # Promote under the same lock as claims, so a continuation cannot win a manual hand-off race.
476
  if (receipt and receipt["state"] == "queued" and receipt.get("requestedBy") == "first load"
@@ -478,7 +504,7 @@ def poll_remote_requests(rt, *, now=None):
478
  receipt.update(id=identity, requestedBy=entry.get("requestedBy") or "",
479
  requestedAt=entry["requestedAt"],
480
  connectionId=entry.get("connectionId"),
481
- notBefore=max(float(receipt.get("notBefore") or 0),
482
  float(entry.get("notBefore") or 0),
483
  _SYNC_NOT_BEFORE.get(key, 0)) or None)
484
  admitted.append(deepcopy(entry))
 
162
  return _SYNC_NOT_BEFORE.get((provider, tenant))
163
 
164
 
165
+ def _clear_sync_defer(provider, tenant):
166
+ """Remove one obsolete provider hold under the desk lock, retaining explicit continuation waits."""
167
+ key = (provider, tenant)
168
+ held = _SYNC_NOT_BEFORE.pop(key, None)
169
+ receipt = _SYNC_REQUESTS.get(key)
170
+ if (held is not None and receipt and receipt["state"] == "queued"
171
+ and receipt.get("requestedBy") != "first load" and receipt.get("notBefore") == held):
172
+ receipt["notBefore"] = None
173
+
174
+
175
+ def clear_sync_defer(provider, tenant):
176
+ """Release one provider hold after its producer adopts partition-specific waits."""
177
+ with _SYNC_REQUEST_LOCK:
178
+ _clear_sync_defer(provider, tenant)
179
+ _wake_sync_requests(time.time())
180
+
181
+
182
  def register_request_listener(fn):
183
  """Call fn(receipt, connection_id) after each accepted ordinary request, outside the desk lock."""
184
  if fn not in _REQUEST_LISTENERS:
 
238
  _wake_sync_requests(stamp)
239
  return {**result, "accepted": accepted_id == identity, "duplicate": accepted_id != identity}
240
  with _SYNC_REQUEST_LOCK:
241
+ if provider == "amazon_sp" and requested_by != "first load":
242
+ # Amazon's history floor belongs to owed tables, never a loaded-table refresh.
243
+ _clear_sync_defer(provider, tenant)
244
  previous = _SYNC_REQUESTS.get((provider, tenant))
245
+ replaces_amazon_history = (provider == "amazon_sp" and previous
246
+ and previous.get("requestedBy") == "first load"
247
+ and requested_by != "first load")
248
  if previous and previous["state"] in ("queued", "running"):
249
  if not (previous["state"] == "queued" and previous.get("requestedBy") == "first load"
250
  and requested_by != "first load"):
 
253
  "requestedAt": time.time() if now is None else now,
254
  "notBefore": max(float(not_before or 0), _SYNC_NOT_BEFORE.get((provider, tenant), 0),
255
  float(previous.get("notBefore") or 0) if previous
256
+ and previous["state"] == "queued" and not replaces_amazon_history else 0) or None,
257
  "startedAt": None, "finishedAt": None, "detail": "",
258
  "requestedBy": str(requested_by or ""), "accepted": True}
259
  if requested_by == "first load" and previous and previous.get("firstLoad"):
 
494
  if stamp - float(entry.get("finishedAt") or 0) > REMOTE_KEEP_SECONDS:
495
  pruned.append(identity)
496
  continue
497
+ manual_amazon = key[0] == "amazon_sp" and entry.get("requestedBy") != "first load"
498
+ if manual_amazon:
499
+ _clear_sync_defer(*key)
500
  receipt = _SYNC_REQUESTS.get(key)
501
  # Promote under the same lock as claims, so a continuation cannot win a manual hand-off race.
502
  if (receipt and receipt["state"] == "queued" and receipt.get("requestedBy") == "first load"
 
504
  receipt.update(id=identity, requestedBy=entry.get("requestedBy") or "",
505
  requestedAt=entry["requestedAt"],
506
  connectionId=entry.get("connectionId"),
507
+ notBefore=max(0 if manual_amazon else float(receipt.get("notBefore") or 0),
508
  float(entry.get("notBefore") or 0),
509
  _SYNC_NOT_BEFORE.get(key, 0)) or None)
510
  admitted.append(deepcopy(entry))
platform/core/external_access.py CHANGED
@@ -11,6 +11,8 @@ from dataclasses import dataclass
11
  from datetime import datetime, timezone
12
  import hashlib
13
  import hmac
 
 
14
  import secrets
15
  import time
16
 
@@ -18,6 +20,7 @@ import core.users as users
18
  from harness import runtime
19
 
20
 
 
21
  BUCKET = "external_access"
22
  ACCESS_READ = "read"
23
  ACCESS_READWRITE = "readwrite"
@@ -57,6 +60,10 @@ class ExternalAccessError(RuntimeError):
57
  class ExternalAuthError(ExternalAccessError):
58
  """A uniform authentication refusal that reveals no credential or tenant detail."""
59
 
 
 
 
 
60
 
61
  def normalise_access(value, *, allow_refresh=False) -> str:
62
  if value is None or value == "":
@@ -139,6 +146,8 @@ class VerifiedCredential:
139
  access: str = ACCESS_READWRITE
140
  databases: object = ALL_DATABASES
141
  workbooks: tuple = ()
 
 
142
 
143
 
144
  def _now() -> str:
@@ -329,6 +338,7 @@ def mint(st, username: str, tenant: str, label: str, access=None, databases=None
329
  "rotated_at": None,
330
  "expires_at": expiry,
331
  "test_connection": test_connection,
 
332
  }
333
  book[key_id] = record
334
  minted["record"] = record
@@ -424,6 +434,7 @@ def rotate(st, username: str, tenant: str, key_id: str) -> tuple[dict, str]:
424
  record.update({
425
  "salt": salt.hex(), "hash": _digest(salt, secret), "algo": ALGORITHM,
426
  "rotated_at": _now(), "last_used": None,
 
427
  })
428
  book[target] = record
429
  rotated["record"] = record
@@ -514,31 +525,63 @@ def _comparison_material(record) -> tuple[bool, bytes, str]:
514
  return False, _DUMMY_SALT, _DUMMY_HASH
515
 
516
 
517
- def verify(token: str) -> VerifiedCredential:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  """Verify a bearer token from fresh state and resolve its ordinary public-user principal."""
519
  try:
520
  tenant_key, key_id, secret = _parse_token(token)
521
- try:
522
- st = runtime.get_runtime(tenant_key)
523
- except KeyError as exc:
524
- raise ExternalAuthError(AUTH_ERROR) from exc
 
 
 
 
 
525
 
526
  book = _book(st)
527
  record = book.get(key_id)
528
- material_ok, salt, expected = _comparison_material(record)
529
- matched = hmac.compare_digest(_digest(salt, secret), expected)
530
- record_ok = (
531
- material_ok
532
- and isinstance(record, dict)
533
- and record.get("id") == key_id
534
- and record.get("tenant") == tenant_key
535
- and bool(record.get("username"))
536
- and not record.get("revoked_at")
537
- and (_epoch(record.get("expires_at")) is None
538
- if not record.get("expires_at") else _epoch(record.get("expires_at")) > time.time())
539
- )
540
- if not record_ok or not matched:
541
- raise ExternalAuthError(AUTH_ERROR)
542
 
543
  owner = str(record["username"]).strip().lower()
544
  try:
@@ -549,7 +592,8 @@ def verify(token: str) -> VerifiedCredential:
549
  access, databases = scope_of(record)
550
  if not _stamp_is_due(record.get("last_used"), time.time()):
551
  return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st,
552
- access=access, databases=databases, workbooks=workbooks_of(record))
 
553
 
554
  used_at = _now()
555
 
@@ -557,26 +601,32 @@ def verify(token: str) -> VerifiedCredential:
557
  if not isinstance(value, dict):
558
  raise ExternalAuthError(AUTH_ERROR)
559
  current = value.get(key_id)
560
- if (not isinstance(current, dict) or current.get("revoked_at")
561
- or current.get("username") != owner
562
- or current.get("tenant") != tenant_key
563
- or current.get("hash") != record.get("hash")):
564
- raise ExternalAuthError(AUTH_ERROR)
565
  book_now = dict(value)
566
  stamped = dict(current)
567
  stamped["last_used"] = used_at
568
  book_now[key_id] = stamped
569
  return book_now
570
 
 
571
  try:
572
  st.update(BUCKET, _stamp, flush="sync")
573
  except ExternalAuthError:
574
  raise
575
  except Exception as exc:
576
- raise ExternalAuthError(AUTH_ERROR) from exc
 
 
577
  return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st,
578
- access=access, databases=databases, workbooks=workbooks_of(record))
 
579
  except ExternalAuthError:
580
  raise
 
 
 
 
581
  except Exception as exc:
582
  raise ExternalAuthError(AUTH_ERROR) from exc
 
11
  from datetime import datetime, timezone
12
  import hashlib
13
  import hmac
14
+ import logging
15
+ import os
16
  import secrets
17
  import time
18
 
 
20
  from harness import runtime
21
 
22
 
23
+ _log = logging.getLogger(__name__)
24
  BUCKET = "external_access"
25
  ACCESS_READ = "read"
26
  ACCESS_READWRITE = "readwrite"
 
60
  class ExternalAuthError(ExternalAccessError):
61
  """A uniform authentication refusal that reveals no credential or tenant detail."""
62
 
63
+ def __init__(self, code: str, *, cause: str = "unknown_key"):
64
+ super().__init__(code)
65
+ self.cause = cause
66
+
67
 
68
  def normalise_access(value, *, allow_refresh=False) -> str:
69
  if value is None or value == "":
 
146
  access: str = ACCESS_READWRITE
147
  databases: object = ALL_DATABASES
148
  workbooks: tuple = ()
149
+ warning: str | None = None
150
+ origin_recorded: bool = False
151
 
152
 
153
  def _now() -> str:
 
338
  "rotated_at": None,
339
  "expires_at": expiry,
340
  "test_connection": test_connection,
341
+ "minted_space": os.environ.get("SPACE_ID") or None,
342
  }
343
  book[key_id] = record
344
  minted["record"] = record
 
434
  record.update({
435
  "salt": salt.hex(), "hash": _digest(salt, secret), "algo": ALGORITHM,
436
  "rotated_at": _now(), "last_used": None,
437
+ "minted_space": os.environ.get("SPACE_ID") or None,
438
  })
439
  book[target] = record
440
  rotated["record"] = record
 
525
  return False, _DUMMY_SALT, _DUMMY_HASH
526
 
527
 
528
+ def _recorded_space(record: dict) -> str | None:
529
+ """Treat missing or malformed origin metadata as unproved."""
530
+ value = record.get("minted_space")
531
+ return value if isinstance(value, str) and value.strip() else None
532
+
533
+
534
+ def _credential_cause(record, tenant: str, key_id: str, secret: str, *, owner=None):
535
+ """Inspect status only after the secret and optional session owner match the stored record."""
536
+ material_ok, salt, expected = _comparison_material(record)
537
+ matched = hmac.compare_digest(_digest(salt, secret), expected)
538
+ if (not material_ok or not matched or record.get("id") != key_id
539
+ or record.get("tenant") != tenant or not record.get("username")
540
+ or (owner is not None and record.get("username") != owner)):
541
+ return "unknown_key"
542
+ origin, current = _recorded_space(record), os.environ.get("SPACE_ID")
543
+ if origin and current and origin != current:
544
+ return "minted_elsewhere"
545
+ if record.get("revoked_at"):
546
+ return "revoked"
547
+ if record.get("expires_at"):
548
+ expiry = _epoch(record["expires_at"])
549
+ if expiry is None or expiry <= time.time():
550
+ return "expired"
551
+ return None
552
+
553
+
554
+ def verify_for_owner(st, username: str, tenant: str, token: str) -> dict:
555
+ """Diagnose only a session owner's secret inside that session's tenant runtime."""
556
+ owner, tenant_key = _normalise_owner(username, tenant)
557
+ _assert_bound_runtime(st, tenant_key)
558
+ try:
559
+ verified = verify(token, owner_runtime=st, owner=owner)
560
+ except ExternalAuthError as exc:
561
+ return {"valid": False, "cause": exc.cause, "warning": None, "origin_recorded": None}
562
+ return {"valid": True, "cause": None, "warning": verified.warning,
563
+ "origin_recorded": verified.origin_recorded, "key_id": verified.key_id}
564
+
565
+
566
+ def verify(token: str, *, owner_runtime=None, owner=None) -> VerifiedCredential:
567
  """Verify a bearer token from fresh state and resolve its ordinary public-user principal."""
568
  try:
569
  tenant_key, key_id, secret = _parse_token(token)
570
+ if owner_runtime is not None:
571
+ if not owner or getattr(owner_runtime, "key", None) != tenant_key:
572
+ raise ExternalAuthError(AUTH_ERROR)
573
+ st = owner_runtime
574
+ else:
575
+ try:
576
+ st = runtime.get_runtime(tenant_key)
577
+ except KeyError as exc:
578
+ raise ExternalAuthError(AUTH_ERROR) from exc
579
 
580
  book = _book(st)
581
  record = book.get(key_id)
582
+ cause = _credential_cause(record, tenant_key, key_id, secret, owner=owner)
583
+ if cause:
584
+ raise ExternalAuthError(AUTH_ERROR, cause=cause)
 
 
 
 
 
 
 
 
 
 
 
585
 
586
  owner = str(record["username"]).strip().lower()
587
  try:
 
592
  access, databases = scope_of(record)
593
  if not _stamp_is_due(record.get("last_used"), time.time()):
594
  return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st,
595
+ access=access, databases=databases, workbooks=workbooks_of(record),
596
+ origin_recorded=bool(_recorded_space(record)))
597
 
598
  used_at = _now()
599
 
 
601
  if not isinstance(value, dict):
602
  raise ExternalAuthError(AUTH_ERROR)
603
  current = value.get(key_id)
604
+ cause = _credential_cause(current, tenant_key, key_id, secret, owner=owner)
605
+ if cause:
606
+ raise ExternalAuthError(AUTH_ERROR, cause=cause)
 
 
607
  book_now = dict(value)
608
  stamped = dict(current)
609
  stamped["last_used"] = used_at
610
  book_now[key_id] = stamped
611
  return book_now
612
 
613
+ warning = None
614
  try:
615
  st.update(BUCKET, _stamp, flush="sync")
616
  except ExternalAuthError:
617
  raise
618
  except Exception as exc:
619
+ # Store diagnostics must not include credential material or exception text.
620
+ _log.warning("external_access last_used stamp failed: %s", type(exc).__name__)
621
+ warning = "store_write_refused"
622
  return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st,
623
+ access=access, databases=databases, workbooks=workbooks_of(record),
624
+ warning=warning, origin_recorded=bool(_recorded_space(record)))
625
  except ExternalAuthError:
626
  raise
627
+ except ExternalAccessError as exc:
628
+ if owner_runtime is not None:
629
+ raise
630
+ raise ExternalAuthError(AUTH_ERROR) from exc
631
  except Exception as exc:
632
  raise ExternalAuthError(AUTH_ERROR) from exc
platform/core/table_store.py CHANGED
@@ -105,10 +105,9 @@ TRASH_RETENTION_SECONDS = TRASH_RETENTION_DAYS * 24 * 60 * 60
105
  # browser re-reads only after some writes. So "stamp differs" alone refuses one tab's own second
106
  # write, and the outbox then resends that refused write forever.
107
  #
108
- # The record below tells the two apart. Each view write appends the view's member digests as
109
- # they stood BEFORE it, tagged with the revision it landed as. For a stamp `R`, the earliest
110
- # entry after `R` is the view as that sender saw it. A write is refused only when a member
111
- # changed after `R` and this write would set it to something else.
112
  #
113
  # ⛔ The record lives in the owner's own stratum beside `views`, so it rides the same update and
114
  # the same settings row. `workspace()` never projects it.
@@ -121,7 +120,7 @@ TRASH_RETENTION_SECONDS = TRASH_RETENTION_DAYS * 24 * 60 * 60
121
  VIEW_WRITES_KEY = 'viewWrites'
122
  # Folder ids are surface-scoped and keep history separate from view ids.
123
  FOLDER_WRITES_KEY = 'folderWrites'
124
- #: Writes kept per view. A sender older than the oldest kept write cannot be judged and is refused.
125
  VIEW_WRITES_KEEP = 8
126
  #: Views kept per stratum. The least recently written record goes first.
127
  VIEW_WRITES_VIEWS = 400
@@ -150,7 +149,7 @@ def view_members(view):
150
  return out
151
 
152
 
153
- def _note_write(ws, view_id, at, kind, before, book_key):
154
  """Append one write to the selected bounded history in this owner's stratum."""
155
  if at is None or not view_id or not isinstance(ws, dict):
156
  return
@@ -159,11 +158,21 @@ def _note_write(ws, view_id, at, kind, before, book_key):
159
  book = ws[book_key] = {}
160
  entry = book.get(view_id) if isinstance(book.get(view_id), dict) else {}
161
  log = [row for row in entry.get('log') or () if isinstance(row, dict)]
162
- log.append({'rev': int(at), 'kind': kind, 'before': before})
 
163
  since = int(entry.get('since') or 0)
 
 
164
  while len(log) > VIEW_WRITES_KEEP:
165
- since = max(since, int(log.pop(0).get('rev') or 0))
 
 
 
 
 
166
  book[view_id] = {'since': since, 'log': log}
 
 
167
  if len(book) > VIEW_WRITES_VIEWS:
168
  # jsonb keeps no key order, so recency is read off each record's newest revision.
169
  def newest(vid):
@@ -174,9 +183,9 @@ def _note_write(ws, view_id, at, kind, before, book_key):
174
  book.pop(vid, None)
175
 
176
 
177
- def note_view_write(ws, view_id, at, kind, before):
178
  """Append one view, gone or place write to this stratum's record."""
179
- _note_write(ws, view_id, at, kind, before, VIEW_WRITES_KEY)
180
 
181
 
182
  def _later_writes(ws, view_id, sent, kinds, *, book_key=VIEW_WRITES_KEY):
@@ -191,25 +200,31 @@ def _later_writes(ws, view_id, sent, kinds, *, book_key=VIEW_WRITES_KEY):
191
  return later, int(sent) >= int(entry.get('since') or 0)
192
 
193
 
194
- def stale_view_conflict(ws, view_id, mine, sent):
195
  """Whether saving `mine` over `view_id` undoes a change made after revision `sent`."""
196
  later, judgeable = _later_writes(ws, view_id, sent, ('view', 'gone'))
197
- if not later:
198
- return False
199
  if not judgeable:
200
- return True
201
- if later[0].get('kind') == 'view' and later[0].get('before') is None:
202
- # Created after `sent`, so the sender is the tab that created it. Judge only later writes.
203
- later = later[1:]
204
- if not later:
205
- return False
206
  current = ((ws.get('views') or {}).get(view_id))
207
- if not isinstance(current, dict):
208
- return True
209
- base = later[0].get('before') or {}
210
- now, want = view_members(current), view_members(mine) or {}
211
- return any(now.get(member) != base.get(member) and want.get(member) != now.get(member)
212
- for member in set(base) | set(now))
 
 
 
 
 
 
 
213
 
214
 
215
  def stale_place_conflict(ws, view_id, folder, sent):
@@ -1391,14 +1406,15 @@ class TableStore:
1391
  for folder_id in sorted(set(before_parents) | set(parents)):
1392
  if before_parents.get(folder_id) != parents.get(folder_id):
1393
  _note_write(ws, folder_id, landing.get('at'), 'parent',
1394
- {'parent': before_parents.get(folder_id)}, FOLDER_WRITES_KEY)
 
1395
  ws['folders'] = tree
1396
  ws['itemFolders'] = placed
1397
  now = placed.get('views') or {}
1398
  for view_id in sorted(set(was) | {str(k) for k in now}):
1399
  if was.get(view_id) != now.get(view_id):
1400
  note_view_write(ws, view_id, landing.get('at'), 'place',
1401
- {'folderId': was.get(view_id)})
1402
 
1403
  def _audit(ws, record):
1404
  now = (ws.get('itemFolders') or {}).get('views') or {}
@@ -1994,7 +2010,8 @@ class TableStore:
1994
  prior = (ws.get('views') or {}).get(view_id)
1995
  activity_before['value'] = view_audit.summary(prior)
1996
  ws.setdefault('views', {})[view_id] = payload
1997
- note_view_write(ws, view_id, landing.get('at'), 'view', view_members(prior))
 
1998
  audit(action=action or view_audit.derive_action(prior, payload),
1999
  owner=username, view_id=view_id, actor=actor, origin=origin,
2000
  before=view_audit.summary(prior), after=view_audit.summary(payload))
@@ -2010,7 +2027,8 @@ class TableStore:
2010
  if rev is None or not rebase:
2011
  raise
2012
  self._land_stale(username, rev, stale,
2013
- lambda ws, sent: stale_view_conflict(ws, view_id, requested, sent),
 
2014
  _write)
2015
  self.log_view_activity(view_id, 'saved', by=actor or username,
2016
  before=activity_before.get('value'),
@@ -2055,7 +2073,8 @@ class TableStore:
2055
  removed.clear()
2056
  if isinstance(before, dict):
2057
  removed['value'] = view_audit.summary(before)
2058
- note_view_write(data.setdefault(owner, {}), vid, at, 'gone', view_members(before))
 
2059
  views.pop(vid, None)
2060
  audit(action='delete' if isinstance(before, dict) else None,
2061
  owner=owner, view_id=vid, actor=actor, origin=origin,
 
105
  # browser re-reads only after some writes. So "stamp differs" alone refuses one tab's own second
106
  # write, and the outbox then resends that refused write forever.
107
  #
108
+ # Each view write records its author, landed revision and the member digests from before it.
109
+ # The next before-image isolates each write's changes. Known requester writes are skipped.
110
+ # A foreign or unknown writer's changed member stays protected when this write would replace it.
 
111
  #
112
  # ⛔ The record lives in the owner's own stratum beside `views`, so it rides the same update and
113
  # the same settings row. `workspace()` never projects it.
 
120
  VIEW_WRITES_KEY = 'viewWrites'
121
  # Folder ids are surface-scoped and keep history separate from view ids.
122
  FOLDER_WRITES_KEY = 'folderWrites'
123
+ #: Writes kept per view. Older senders need a matching trimmed-author boundary.
124
  VIEW_WRITES_KEEP = 8
125
  #: Views kept per stratum. The least recently written record goes first.
126
  VIEW_WRITES_VIEWS = 400
 
149
  return out
150
 
151
 
152
+ def _note_write(ws, view_id, at, kind, before, book_key, *, author=None):
153
  """Append one write to the selected bounded history in this owner's stratum."""
154
  if at is None or not view_id or not isinstance(ws, dict):
155
  return
 
158
  book = ws[book_key] = {}
159
  entry = book.get(view_id) if isinstance(book.get(view_id), dict) else {}
160
  log = [row for row in entry.get('log') or () if isinstance(row, dict)]
161
+ author = str(author or '').strip().lower() or None
162
+ log.append({'rev': int(at), 'kind': kind, 'before': before, 'author': author})
163
  since = int(entry.get('since') or 0)
164
+ trimmed = entry.get('trimmed')
165
+ trimmed = dict(trimmed) if isinstance(trimmed, dict) else {}
166
  while len(log) > VIEW_WRITES_KEEP:
167
+ removed = log.pop(0)
168
+ removed_author = removed.get('author')
169
+ # Only a known, uninterrupted author run can excuse writes outside the ring.
170
+ if not removed_author or removed_author != trimmed.get('author'):
171
+ trimmed = {'author': removed_author, 'after': since}
172
+ since = max(since, int(removed.get('rev') or 0))
173
  book[view_id] = {'since': since, 'log': log}
174
+ if trimmed:
175
+ book[view_id]['trimmed'] = trimmed
176
  if len(book) > VIEW_WRITES_VIEWS:
177
  # jsonb keeps no key order, so recency is read off each record's newest revision.
178
  def newest(vid):
 
183
  book.pop(vid, None)
184
 
185
 
186
+ def note_view_write(ws, view_id, at, kind, before, *, author=None):
187
  """Append one view, gone or place write to this stratum's record."""
188
+ _note_write(ws, view_id, at, kind, before, VIEW_WRITES_KEY, author=author)
189
 
190
 
191
  def _later_writes(ws, view_id, sent, kinds, *, book_key=VIEW_WRITES_KEY):
 
200
  return later, int(sent) >= int(entry.get('since') or 0)
201
 
202
 
203
+ def stale_view_conflict(ws, view_id, mine, sent, *, requester=None):
204
  """Whether saving `mine` over `view_id` undoes a change made after revision `sent`."""
205
  later, judgeable = _later_writes(ws, view_id, sent, ('view', 'gone'))
206
+ requester = str(requester or '').strip().lower() or None
 
207
  if not judgeable:
208
+ entry = (ws.get(VIEW_WRITES_KEY) or {}).get(view_id) or {}
209
+ trimmed = entry.get('trimmed')
210
+ trimmed = trimmed if isinstance(trimmed, dict) else {}
211
+ if not (requester and trimmed.get('author') == requester
212
+ and int(sent) >= int(trimmed.get('after', entry.get('since') or 0))):
213
+ return True
214
  current = ((ws.get('views') or {}).get(view_id))
215
+ now, want = view_members(current) or {}, view_members(mine) or {}
216
+ for index, row in enumerate(later):
217
+ if requester and row.get('author') == requester:
218
+ continue
219
+ if not isinstance(current, dict):
220
+ return True
221
+ before = row.get('before') or {}
222
+ after = (later[index + 1].get('before') or {}) if index + 1 < len(later) else now
223
+ # The next before-image isolates this writer's changes from the requester's later edits.
224
+ if any(before.get(member) != after.get(member) and want.get(member) != now.get(member)
225
+ for member in set(before) | set(after)):
226
+ return True
227
+ return False
228
 
229
 
230
  def stale_place_conflict(ws, view_id, folder, sent):
 
1406
  for folder_id in sorted(set(before_parents) | set(parents)):
1407
  if before_parents.get(folder_id) != parents.get(folder_id):
1408
  _note_write(ws, folder_id, landing.get('at'), 'parent',
1409
+ {'parent': before_parents.get(folder_id)}, FOLDER_WRITES_KEY,
1410
+ author=actor or username)
1411
  ws['folders'] = tree
1412
  ws['itemFolders'] = placed
1413
  now = placed.get('views') or {}
1414
  for view_id in sorted(set(was) | {str(k) for k in now}):
1415
  if was.get(view_id) != now.get(view_id):
1416
  note_view_write(ws, view_id, landing.get('at'), 'place',
1417
+ {'folderId': was.get(view_id)}, author=actor or username)
1418
 
1419
  def _audit(ws, record):
1420
  now = (ws.get('itemFolders') or {}).get('views') or {}
 
2010
  prior = (ws.get('views') or {}).get(view_id)
2011
  activity_before['value'] = view_audit.summary(prior)
2012
  ws.setdefault('views', {})[view_id] = payload
2013
+ note_view_write(ws, view_id, landing.get('at'), 'view', view_members(prior),
2014
+ author=actor or username)
2015
  audit(action=action or view_audit.derive_action(prior, payload),
2016
  owner=username, view_id=view_id, actor=actor, origin=origin,
2017
  before=view_audit.summary(prior), after=view_audit.summary(payload))
 
2027
  if rev is None or not rebase:
2028
  raise
2029
  self._land_stale(username, rev, stale,
2030
+ lambda ws, sent: stale_view_conflict(
2031
+ ws, view_id, requested, sent, requester=actor or username),
2032
  _write)
2033
  self.log_view_activity(view_id, 'saved', by=actor or username,
2034
  before=activity_before.get('value'),
 
2073
  removed.clear()
2074
  if isinstance(before, dict):
2075
  removed['value'] = view_audit.summary(before)
2076
+ note_view_write(data.setdefault(owner, {}), vid, at, 'gone', view_members(before),
2077
+ author=who)
2078
  views.pop(vid, None)
2079
  audit(action='delete' if isinstance(before, dict) else None,
2080
  owner=owner, view_id=vid, actor=actor, origin=origin,
platform/core/user_tables.py CHANGED
@@ -529,6 +529,8 @@ def oversize_of(table_key, st=None, defn=None):
529
  defn = get_definition(str(table_key or '').strip(), st) or {}
530
  except Exception: # noqa: BLE001
531
  defn = {}
 
 
532
  stamp = (defn or {}).get(OVERSIZE_FIELD)
533
  return stamp if isinstance(stamp, dict) else None
534
 
 
529
  defn = get_definition(str(table_key or '').strip(), st) or {}
530
  except Exception: # noqa: BLE001
531
  defn = {}
532
+ if not materialises(table_key, defn=defn):
533
+ return None
534
  stamp = (defn or {}).get(OVERSIZE_FIELD)
535
  return stamp if isinstance(stamp, dict) else None
536
 
platform/core/users.py CHANGED
@@ -1176,8 +1176,6 @@ def set_whatsapp(username, value):
1176
  u = reg.get(username)
1177
  if not isinstance(u, dict):
1178
  raise ValueError('This account is not available.')
1179
- # R42: a test account has no outbound channel, so no writer may add one.
1180
- refuse_test_account_change(u, whatsapp=value)
1181
  if value is None:
1182
  u.pop('whatsapp', None)
1183
  else:
@@ -1200,110 +1198,9 @@ def set_whatsapp_opt_out(username, on):
1200
  return account_rows.update(username, _set, st=store)
1201
 
1202
 
1203
- # ------------------------------------------------------------------ test accounts (S79-T63, R42)
1204
- #: The record key that marks a copied test account: `{copied_from, created_by, created_at}`.
1205
- TEST_ACCOUNT_KEY = 'test_account'
1206
-
1207
-
1208
- class TestAccountRefused(ValueError):
1209
- """A writer refused to give a test account an outbound channel or the admin role."""
1210
-
1211
- def __init__(self, code, message):
1212
- self.code = code
1213
- super().__init__(message)
1214
-
1215
-
1216
- class TestAccountExists(ValueError):
1217
- """The chosen test username is already a key in the registry."""
1218
-
1219
-
1220
  def is_test_account(record):
1221
- """Whether this stored record is a copied test account."""
1222
- return isinstance(record, dict) and isinstance(record.get(TEST_ACCOUNT_KEY), dict)
1223
-
1224
-
1225
- def refuse_test_account_change(record, *, email=None, whatsapp=None, role=None):
1226
- """Raise `TestAccountRefused` if a change would give a test account email, WhatsApp or admin."""
1227
- if not is_test_account(record):
1228
- return
1229
- if email:
1230
- raise TestAccountRefused('test_account_no_email',
1231
- 'A test account cannot have an email address.')
1232
- if whatsapp:
1233
- raise TestAccountRefused('test_account_no_whatsapp',
1234
- 'A test account cannot connect WhatsApp.')
1235
- if role is not None and str(role).strip().lower() != 'user':
1236
- raise TestAccountRefused('test_account_not_admin',
1237
- 'A test account cannot be an administrator.')
1238
-
1239
-
1240
- def create_test_account(username, pw, *, name, access, marker, tenant):
1241
- """Write one test account in one update: `access` fields verbatim, no email, no WhatsApp.
1242
-
1243
- Raises `TestAccountExists` when the key is taken by any record other than this exact write.
1244
- """
1245
- import copy
1246
- freeze_notice.check_write()
1247
- original_username = (username or '').strip()
1248
- username = original_username.lower()
1249
- failures = password_failures(pw, username=username, name=name)
1250
- if failures:
1251
- raise PasswordPolicyError(failures)
1252
- marker = dict(marker)
1253
- salt = secrets.token_hex(16)
1254
- digest = _hash(pw, salt)
1255
-
1256
- def _add(reg):
1257
- prior = reg.get(username)
1258
- if prior is not None:
1259
- # A replayed closure finds its own landed write; anything else is a real collision.
1260
- if isinstance(prior, dict) and prior.get(TEST_ACCOUNT_KEY) == marker:
1261
- return reg
1262
- raise TestAccountExists(username)
1263
- rec = {'salt': salt, 'hash': digest, 'name': name, 'role': 'user',
1264
- 'password_changed_at': _now_iso(),
1265
- 'bus': copy.deepcopy(access.get('bus', 'all')), 'active': True,
1266
- 'modules': copy.deepcopy(access.get('modules', 'all')),
1267
- 'agent': access.get('agent') or None, 'email': None,
1268
- 'tenant': str(tenant or 'royal-imports').strip().lower(),
1269
- 'username_original': original_username, TEST_ACCOUNT_KEY: dict(marker)}
1270
- # The permission block and its marker travel together or not at all (see `_record`).
1271
- for key in ('perms', 'perms_v'):
1272
- if key in access:
1273
- rec[key] = copy.deepcopy(access[key])
1274
- reg[username] = rec
1275
- return reg
1276
-
1277
- account_rows.update(username, _add, st=store)
1278
-
1279
-
1280
- def delete_test_account(username, *, tenant, marker=None):
1281
- """Delete one test account record. Returns deleted, missing or not_test; never a real account.
1282
-
1283
- `marker`, when given, must equal the stored marker, so a rollback never removes a copy
1284
- another administrator made under the same name.
1285
- """
1286
- freeze_notice.check_write()
1287
- username = (username or '').strip().lower()
1288
- tenant = str(tenant or '').strip().lower()
1289
- outcome = ['missing']
1290
-
1291
- def _del(reg):
1292
- outcome[0] = 'missing'
1293
- rec = reg.get(username)
1294
- if (not isinstance(rec, dict)
1295
- or str(rec.get('tenant') or 'royal-imports').strip().lower() != tenant):
1296
- return reg
1297
- if not is_test_account(rec) or (marker is not None
1298
- and rec.get(TEST_ACCOUNT_KEY) != dict(marker)):
1299
- outcome[0] = 'not_test'
1300
- return reg
1301
- reg.pop(username, None)
1302
- outcome[0] = 'deleted'
1303
- return reg
1304
-
1305
- account_rows.update(username, _del, st=store)
1306
- return outcome[0]
1307
 
1308
 
1309
  #: The keys of one database entry that `set_database_access` writes.
@@ -1422,7 +1319,6 @@ def set_access(username, role=None, bus=None, modules=None, agent=None, email=No
1422
  def _set(reg):
1423
  u = reg.get(username)
1424
  if u:
1425
- refuse_test_account_change(u, email=email, role=role)
1426
  if name is not None:
1427
  u['name'] = name
1428
  if role is not None:
@@ -1465,12 +1361,6 @@ def update_access_with_active_admin_guard(username, role=None, bus=None, modules
1465
  if not isinstance(current, dict):
1466
  outcome[0] = 'missing'
1467
  return reg
1468
- try:
1469
- refuse_test_account_change(current, email=email, role=role)
1470
- except TestAccountRefused as exc:
1471
- outcome[0] = exc.code
1472
- return reg
1473
-
1474
  # Build the whole candidate before touching reg. On a refused transition the original
1475
  # record remains byte-for-byte intact, including its epoch.
1476
  candidate = dict(current)
 
1176
  u = reg.get(username)
1177
  if not isinstance(u, dict):
1178
  raise ValueError('This account is not available.')
 
 
1179
  if value is None:
1180
  u.pop('whatsapp', None)
1181
  else:
 
1198
  return account_rows.update(username, _set, st=store)
1199
 
1200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1201
  def is_test_account(record):
1202
+ """Read the legacy marker until release confirms live has no copied accounts."""
1203
+ return isinstance(record, dict) and isinstance(record.get('test_account'), dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1204
 
1205
 
1206
  #: The keys of one database entry that `set_database_access` writes.
 
1319
  def _set(reg):
1320
  u = reg.get(username)
1321
  if u:
 
1322
  if name is not None:
1323
  u['name'] = name
1324
  if role is not None:
 
1361
  if not isinstance(current, dict):
1362
  outcome[0] = 'missing'
1363
  return reg
 
 
 
 
 
 
1364
  # Build the whole candidate before touching reg. On a refused transition the original
1365
  # record remains byte-for-byte intact, including its epoch.
1366
  candidate = dict(current)
platform/core/windows.py CHANGED
@@ -286,7 +286,10 @@ def ending_bounds(kind, mode, value, today, step="month", offset=0, n=0, unit="m
286
  """Keep the kind's own start. An ending before it refuses instead of reversing bounds."""
287
  if kind not in TO_DATE_KINDS:
288
  return None
289
- end = resolve_ending(mode, value, today, step, offset, n, unit)
 
 
 
290
  if end is None:
291
  return None
292
  start = resolve({"kind": kind}, today)[0]
 
286
  """Keep the kind's own start. An ending before it refuses instead of reversing bounds."""
287
  if kind not in TO_DATE_KINDS:
288
  return None
289
+ ending_today = today
290
+ if kind == "ytd_last_year" and mode not in ("fixed", "cell"):
291
+ ending_today = resolve({"kind": kind}, today)[1]
292
+ end = resolve_ending(mode, value, ending_today, step, offset, n, unit)
293
  if end is None:
294
  return None
295
  start = resolve({"kind": kind}, today)[0]
platform/harness/amazon_client.py CHANGED
@@ -416,7 +416,7 @@ REPORT_TYPES = {
416
  },
417
  "storage_fees": {
418
  "report": "GET_FBA_STORAGE_FEE_CHARGES_DATA", "monthly": True,
419
- "min_interval_s": 30 * 86400,
420
  "validates_against": "Amazon document data-row count vs stored rows for its latest calendar month",
421
  "role": "Amazon Fulfillment",
422
  },
@@ -458,6 +458,7 @@ GROUP_A_TABLES = {
458
  "report": "GET_FBA_REIMBURSEMENTS_DATA",
459
  "role": "Amazon Fulfillment",
460
  "min_interval_s": 14400,
 
461
  "cadence_note": "Date windows; refresh no sooner than 14400 seconds.",
462
  "validates_against": "Amazon document or API data-row count vs stored rows for the latest period",
463
  },
@@ -466,6 +467,7 @@ GROUP_A_TABLES = {
466
  "report": "GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA",
467
  "role": "Amazon Fulfillment",
468
  "min_interval_s": 14400,
 
469
  "cadence_note": "Date windows; refresh no sooner than 14400 seconds.",
470
  "validates_against": "Amazon document or API data-row count vs stored rows for the latest period",
471
  },
@@ -676,8 +678,8 @@ GROUP_A_TABLES = {
676
  "label": "Amazon account health",
677
  "report": "GET_V2_SELLER_PERFORMANCE_REPORT",
678
  "role": "Selling Partner Insights",
679
- "min_interval_s": 86400,
680
- "cadence_note": "Current snapshot; refresh no sooner than 86400 seconds.",
681
  "snapshot": True,
682
  "json_root": "performanceMetrics",
683
  "json_schema": "accountHealthReport-2020-11-18.json",
@@ -734,6 +736,7 @@ GROUP_A_TABLES = {
734
  "report": "GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE",
735
  "role": "Inventory and Order Tracking",
736
  "min_interval_s": 14400,
 
737
  "cadence_note": "Date windows; refresh no sooner than 14400 seconds.",
738
  "max_window_days": 60,
739
  "validates_against": "Amazon document or API data-row count vs stored rows for the latest period",
@@ -1015,6 +1018,40 @@ GROUP_A = {
1015
  GROUP_A.update({key: {"key": key, **spec} for key, spec in DECLARED_DATASETS.items()})
1016
 
1017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1018
  # Inventory section 2.5: immutable refusal even if a future report roster admits one.
1019
  EXCLUDED_REPORT_TYPES = frozenset({
1020
  "GET_ORDER_REPORT_DATA_SHIPPING", "GET_ORDER_REPORT_DATA_TAX", "GET_ORDER_REPORT_DATA_ALL",
@@ -3381,7 +3418,8 @@ def report_document(fields, report_type, start=None, end=None, options=None,
3381
  # a role-shaped refusal on screen and left the window to retry forever. An empty
3382
  # document lets the window complete and the connector detail name the empty period.
3383
  say(f" {report_type}: Amazon cancelled report {rid}; no data for the requested period")
3384
- return "", {"reportId": rid, "reportDocumentId": None, "cancelled": True,
 
3385
  "dataStartTime": got.get("dataStartTime"),
3386
  "dataEndTime": got.get("dataEndTime"),
3387
  "createdTime": got.get("createdTime"), "compression": None,
@@ -3419,7 +3457,7 @@ def report_document(fields, report_type, start=None, end=None, options=None,
3419
  raise AmazonError(f"the {report_type} report document carried no download URL")
3420
  text = _download_document(url, str(meta.get("compressionAlgorithm") or ""), spool=True)
3421
  say(f" {report_type}: {len(text):,} chars")
3422
- return text, {"reportId": rid, "reportDocumentId": doc_id,
3423
  "dataStartTime": got.get("dataStartTime"),
3424
  "dataEndTime": got.get("dataEndTime"), "createdTime": got.get("createdTime"),
3425
  "compression": meta.get("compressionAlgorithm"), "marketplaceIds": got.get("marketplaceIds")}
 
416
  },
417
  "storage_fees": {
418
  "report": "GET_FBA_STORAGE_FEE_CHARGES_DATA", "monthly": True,
419
+ "min_interval_s": 86400,
420
  "validates_against": "Amazon document data-row count vs stored rows for its latest calendar month",
421
  "role": "Amazon Fulfillment",
422
  },
 
458
  "report": "GET_FBA_REIMBURSEMENTS_DATA",
459
  "role": "Amazon Fulfillment",
460
  "min_interval_s": 14400,
461
+ "restatement_keys": ("reimbursement_id",),
462
  "cadence_note": "Date windows; refresh no sooner than 14400 seconds.",
463
  "validates_against": "Amazon document or API data-row count vs stored rows for the latest period",
464
  },
 
467
  "report": "GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA",
468
  "role": "Amazon Fulfillment",
469
  "min_interval_s": 14400,
470
+ "restatement_keys": ("order_id", "sku", "fnsku", "asin", "return_date"),
471
  "cadence_note": "Date windows; refresh no sooner than 14400 seconds.",
472
  "validates_against": "Amazon document or API data-row count vs stored rows for the latest period",
473
  },
 
678
  "label": "Amazon account health",
679
  "report": "GET_V2_SELLER_PERFORMANCE_REPORT",
680
  "role": "Selling Partner Insights",
681
+ "min_interval_s": 43200,
682
+ "cadence_note": "Current snapshot; refresh no sooner than 43200 seconds.",
683
  "snapshot": True,
684
  "json_root": "performanceMetrics",
685
  "json_schema": "accountHealthReport-2020-11-18.json",
 
736
  "report": "GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE",
737
  "role": "Inventory and Order Tracking",
738
  "min_interval_s": 14400,
739
+ "restatement_keys": ("order_id", "sku", "return_request_date"),
740
  "cadence_note": "Date windows; refresh no sooner than 14400 seconds.",
741
  "max_window_days": 60,
742
  "validates_against": "Amazon document or API data-row count vs stored rows for the latest period",
 
1018
  GROUP_A.update({key: {"key": key, **spec} for key, spec in DECLARED_DATASETS.items()})
1019
 
1020
 
1021
+ # Source: AutoWiki/research/datadoe-sync-cadence.md. Unlisted tables keep the daily default.
1022
+ CADENCE = {
1023
+ table: {"perDay": 1, "lookbackDays": 1, "intradayLookbackDays": 1, "period": "day"}
1024
+ for table in GROUP_A
1025
+ }
1026
+ for _table, _spec in REPORT_TYPES.items():
1027
+ if _spec.get("monthly") or _spec.get("period") == "MONTH":
1028
+ CADENCE[_table].update(period="month", lookbackDays=60, intradayLookbackDays=60)
1029
+ elif _spec.get("weekly") or _spec.get("min_interval_s", 0) >= 7 * 86400:
1030
+ CADENCE[_table].update(period="week", lookbackDays=14, intradayLookbackDays=14)
1031
+ for _tables, _passes, _daily, _intraday in (
1032
+ (("orders", "order_items"), 6, 8, 1),
1033
+ (("settlements", "transactions"), 3, 8, 2),
1034
+ (("listings", "inventory_planning"), 4, 1, 1),
1035
+ (("account_health", "manage_inventory", "restock_recommendations"), 2, 1, 1),
1036
+ (("pricing", "fba_fee_estimates", "referral_fees"), 1, 4, 4),
1037
+ (("reimbursements", "long_term_storage_fees"), 1, 30, 30),
1038
+ (("fba_customer_returns", "mfn_returns"), 1, 60, 60),
1039
+ (("storage_fees",), 1, 1, 1),
1040
+ ):
1041
+ for _table in _tables:
1042
+ CADENCE[_table].update(perDay=_passes, lookbackDays=_daily,
1043
+ intradayLookbackDays=_intraday, period="day")
1044
+
1045
+ # Ads uses namespaced mirror marker names, so its tables cannot collide with SP-API tables.
1046
+ from harness import amazon_ads_schema as _ads_schema
1047
+
1048
+ CADENCE.update({
1049
+ "ads_" + table: {"perDay": 4, "lookbackDays": 21, "intradayLookbackDays": 5, "period": "day"}
1050
+ for table in _ads_schema.TABLE_SPECS
1051
+ })
1052
+ del _table, _tables, _spec, _passes, _daily, _intraday, _ads_schema
1053
+
1054
+
1055
  # Inventory section 2.5: immutable refusal even if a future report roster admits one.
1056
  EXCLUDED_REPORT_TYPES = frozenset({
1057
  "GET_ORDER_REPORT_DATA_SHIPPING", "GET_ORDER_REPORT_DATA_TAX", "GET_ORDER_REPORT_DATA_ALL",
 
3418
  # a role-shaped refusal on screen and left the window to retry forever. An empty
3419
  # document lets the window complete and the connector detail name the empty period.
3420
  say(f" {report_type}: Amazon cancelled report {rid}; no data for the requested period")
3421
+ return "", {"reportId": rid, "reportDocumentId": None, "processingStatus": state,
3422
+ "cancelled": True,
3423
  "dataStartTime": got.get("dataStartTime"),
3424
  "dataEndTime": got.get("dataEndTime"),
3425
  "createdTime": got.get("createdTime"), "compression": None,
 
3457
  raise AmazonError(f"the {report_type} report document carried no download URL")
3458
  text = _download_document(url, str(meta.get("compressionAlgorithm") or ""), spool=True)
3459
  say(f" {report_type}: {len(text):,} chars")
3460
+ return text, {"reportId": rid, "reportDocumentId": doc_id, "processingStatus": state,
3461
  "dataStartTime": got.get("dataStartTime"),
3462
  "dataEndTime": got.get("dataEndTime"), "createdTime": got.get("createdTime"),
3463
  "compression": meta.get("compressionAlgorithm"), "marketplaceIds": got.get("marketplaceIds")}
platform/harness/amazon_store.py CHANGED
@@ -463,27 +463,48 @@ def read_database_enablement(rt):
463
  return {"available": True, "databases": {key: stored.get(key, True) for key in keys}}
464
 
465
 
466
- #: ⛔⛔ HOTFIX 2026-09-29 (owner): *"We should only sync daily at the fastest."* A scheduled pass
467
- #: refreshes each table at most once a day. The two hours of grace keep a table stamped late in
468
- #: yesterday's pass from sliding to 48 hours because today's pass reached it a little earlier.
469
- #: An operator's Sync now keeps only Amazon's own report floors (owner ruling, same day).
470
  SCHEDULED_FLOOR_SECONDS = 86400
471
- SCHEDULED_FLOOR_GRACE_SECONDS = 2 * 3600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472
 
473
 
474
  def sync_plan(states, tables=None, *, now=None, slot_pass=False):
475
- """Pure cadence decision: EVERY due table, oldest first, with no stagger sleep.
476
-
477
- `slot_pass` is the tenant's daily slot pass: it IS the once-a-day sync, so only Amazon's own
478
- report floors defer a table. Any other scheduled pass keeps the daily floor.
479
-
480
- Attempt markers survive in the tenant's saved mirror. Failed tables rotate behind peers.
481
- Settlement LIST discovery runs separately on every resync and consumes no report slot.
482
- The client still owns pacing/retries within a table.
483
- ⛔ HOTFIX 2026-09-29: this picked ONE table per tick. At a once-a-day cadence that is one
484
- table a day, about seven weeks between two refreshes of the same table, so a scheduled pass
485
- now takes every table whose daily floor (and Amazon's own floor) has passed.
486
- """
487
  from harness.amazon_client import REPORT_STAGGER_SECONDS
488
  now = _utc(now)
489
  want = list(TABLES if tables is None else tables)
@@ -491,10 +512,8 @@ def sync_plan(states, tables=None, *, now=None, slot_pass=False):
491
  if state and table != "settlements"}
492
  deferred = {}
493
  due = []
494
- floor = 0 if slot_pass else SCHEDULED_FLOOR_SECONDS - SCHEDULED_FLOOR_GRACE_SECONDS
495
  for table in want:
496
- if table == "settlements":
497
- continue
498
  if (reason := _interval_reason(table, states.get(table), now, floor_s=floor)):
499
  deferred[table] = reason
500
  else:
@@ -508,8 +527,37 @@ def sync_plan(states, tables=None, *, now=None, slot_pass=False):
508
  for table in due:
509
  deferred[table] = ("table stagger: waiting for a later cadence tick; "
510
  f"minimum spacing {REPORT_STAGGER_SECONDS:g}s")
511
- return {"tables": selected + (["settlements"] if "settlements" in want else []),
512
- "deferred": deferred}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
 
514
 
515
  def _readable_duration(seconds):
@@ -525,10 +573,12 @@ def _readable_duration(seconds):
525
 
526
 
527
  def _interval_wait(table, state, now, floor_s=None):
528
- """Seconds until `table` may be requested again (0 when due) and which floor decides it.
529
-
530
- `floor_s` is the scheduled daily floor; Amazon's own `min_interval_s` always applies too.
531
- """
 
 
532
  vendor = (_client().REPORT_TYPES.get(table) or {}).get("min_interval_s") or 0
533
  seconds = max(int(vendor), int(floor_s or 0))
534
  if seconds and state and state["state"] == "done":
@@ -540,11 +590,13 @@ def _interval_wait(table, state, now, floor_s=None):
540
 
541
 
542
  def _interval_reason(table, state, now, floor_s=None):
543
- wait, due, seconds, daily = _interval_wait(table, state, now, floor_s=floor_s)
544
  if wait <= 0:
545
  return None
546
- if daily:
547
- return (f"Scheduled syncs refresh each Amazon table at most once a day; "
 
 
548
  f"next due {_iso(due)}")
549
  return (f"Amazon allows this report at most once every {_readable_duration(seconds)}; "
550
  f"next due {_iso(due)}")
@@ -3964,7 +4016,7 @@ def _restored_state_rows(rows, tables, refused):
3964
  return rows
3965
  refused = set(refused)
3966
  names = sorted(set(tables) | {table_name(t) for t in TABLES}, key=lambda name: (-len(name), name))
3967
- families = ("", WATERMARK_PREFIX, BACKFILL_FLOOR_PREFIX, "sync_tick_", "reconcile_", "limit_", "refused_",
3968
  RESYNC_PREFIX)
3969
  shared = {entity_key(key) for key in (FIRST_LOAD_END_KEY, FIRST_LOAD_STATUS_KEY, FIRST_LOAD_DUE_KEY)}
3970
 
@@ -4735,7 +4787,7 @@ def _record_state(con, key, phase, rows=0, detail=None):
4735
  con.execute(
4736
  "INSERT OR REPLACE INTO _sync_state "
4737
  "(entity, phase, last_id, cursor_wd, rows, updated_at) VALUES (?,?,?,?,?,?)",
4738
- [str(key), str(phase), None, (str(detail) if payload_key.startswith(("amzn_reconcile_", "amzn_limit_", "amzn_refused_", "amzn_pending_report_",
4739
  "amzn_resync_", "amzn_waiting_"))
4740
  else str(detail)[:300]) if detail else None, int(rows or 0),
4741
  _iso(datetime.now(timezone.utc))])
@@ -4807,31 +4859,25 @@ def _first_load_due(con):
4807
 
4808
 
4809
  def _restore_first_load_wait(tenant_key, con):
 
4810
  from core import connector_sync
4811
  due = _first_load_due(con)
4812
- if _account_scope() is not None:
4813
- return due
4814
- connector_sync.defer_sync("amazon_sp", tenant_key, due.timestamp() if due else None)
4815
  return due
4816
 
4817
 
4818
  def _loaded_refresh_hold(con, table, now, *, slot_pass=False):
4819
- """`(next due, reason)` while a loaded table's own floor holds it during a first load, else None.
4820
-
4821
- A weekly table is due when a new week has closed after its watermark (D-11328). Every other
4822
- table, settlement discovery included, keeps the daily floor and Amazon's own floor.
4823
- """
4824
  now = _utc(now)
4825
  tick = _read_state(con, entity_key("sync_tick_" + table))
4826
  # A queued resync is a repair: only Amazon's own floor holds it.
4827
  repair = queued_resync(con, table) is not None
4828
- floor = 0 if slot_pass or repair else SCHEDULED_FLOOR_SECONDS - SCHEDULED_FLOOR_GRACE_SECONDS
4829
- wait, due, _seconds, daily = _interval_wait(table, tick, now, floor_s=floor)
4830
  reason = None
4831
  if wait > 0:
4832
  # The card parses "next due <iso>" out of a reason, so every hold keeps that phrase.
4833
- reason = (f"Each loaded Amazon table refreshes at most once a day while the first load continues. "
4834
- f"It is next due {_iso(due)}" if daily else _interval_reason(table, tick, now, floor_s=floor))
4835
  mark = read_watermark(con, table)
4836
  if _client().REPORT_TYPES.get(table, {}).get("weekly") and mark and not repair:
4837
  week = _week_start(mark) + timedelta(days=7)
@@ -5129,6 +5175,8 @@ def first_load_status(tenant_key, rt=None):
5129
  remaining = int(values["remaining"])
5130
  # S82-T14: one pass sees only its own tables. The job's store record counts the whole load.
5131
  job = first_load_job_summary(rt) if _store_documents(rt) else None
 
 
5132
  if job and isinstance(job.get("remaining"), int) and state["state"] != "done":
5133
  remaining = job["remaining"]
5134
  out = {"state": state["state"], **first_load_payload(markers, remaining,
@@ -5413,7 +5461,7 @@ def _sync_writer(tenant_key, rt, log):
5413
  def _sync_delegate(tenant_key, rt, log):
5414
  """Every table but the job's own takes one bounded first-load pass: settlements, period, Group A, SQP.
5415
 
5416
- The same pass refreshes each loaded table whose daily floor is due, so loaded data keeps moving.
5417
  """
5418
  def delegate(exclude, budget):
5419
  tables = None if exclude is None else [t for t in TABLES if t not in set(exclude)]
@@ -5473,7 +5521,91 @@ def _status_stamp(status, connector):
5473
  return max(stamps, key=lambda item: item[0])[1] if stamps else None
5474
 
5475
 
5476
- def first_load_table_status(tasks, finished, failed, counts):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5477
  """D-2643, D-2642, D-2649: a row count or a named reason per table, and why orders are behind."""
5478
  out = {}
5479
  for table in dict.fromkeys(EMPTY_REASON_TABLES + BEHIND_TABLES):
@@ -5487,17 +5619,25 @@ def first_load_table_status(tasks, finished, failed, counts):
5487
  f"The first load is still loading this table. {len(left)} report windows remain."
5488
  if left else "Amazon returned no rows for this seller in the loaded period."
5489
  if own else "This table holds no rows and the first load owes it no windows.")
 
 
 
5490
  if table in BEHIND_TABLES and left:
5491
  done = [t for t in own if t["id"] in finished or t.get("landed")]
5492
  oldest = min((t["start"] for t in done if t.get("start")), default=None)
5493
  row["behind"] = (f"The first load is still loading this table. {len(left)} of {len(own)} report "
5494
  "windows remain" + (f", and history before {oldest[:10]} is not loaded yet." if oldest
5495
  else ", starting with the most recent month."))
 
 
 
 
 
5496
  out[table] = row
5497
  return out
5498
 
5499
 
5500
- def first_load_job_summary(docs):
5501
  """The latest generation record and the cursor's remaining count, read from the store."""
5502
  try:
5503
  record = _doc(docs, FIRST_LOAD_GENERATIONS_KEY)
@@ -5511,7 +5651,7 @@ def first_load_job_summary(docs):
5511
  "remaining": record.get("remaining", last.get("remainingAfter")),
5512
  "tablesPublished": list(last.get("tablesPublished") or []), "end": last.get("end"),
5513
  "operation": last.get("operation"), "resume_at": last.get("resume_at"),
5514
- "tables": last.get("tables") or {}}
5515
 
5516
 
5517
  def _record_generation(docs, row):
@@ -5787,7 +5927,15 @@ def run_first_load_generation(tenant_key, rt=None, *, generation=1, budget_secon
5787
  row_counts = counts(list(dict.fromkeys(EMPTY_REASON_TABLES + BEHIND_TABLES)))
5788
  except Exception: # noqa: BLE001
5789
  row_counts = {}
5790
- record["tables"] = first_load_table_status(after, finished, failed, row_counts)
 
 
 
 
 
 
 
 
5791
  record["state"] = "done" if not record["remainingAfter"] else "partial"
5792
  status_after = read_connector_status(docs)
5793
  record["odooLastGoodSyncAt"] = {"start": _status_stamp(status_before, "odoo"),
@@ -5812,7 +5960,7 @@ def run_first_load_generation(tenant_key, rt=None, *, generation=1, budget_secon
5812
  pass
5813
 
5814
 
5815
- def sync_window(table, mark, *, now, since=None, backfill_days=None):
5816
  """Pure request decision for one table: start, end, report, incremental and cause."""
5817
  end = _utc(now)
5818
  days = DEFAULT_BACKFILL_DAYS if backfill_days is None else backfill_days
@@ -5830,6 +5978,11 @@ def sync_window(table, mark, *, now, since=None, backfill_days=None):
5830
  start, incremental = end - timedelta(days=days), False
5831
  cause = f"first full load: {days} days of backfill"
5832
  spec = _client().REPORT_TYPES.get(table) or {}
 
 
 
 
 
5833
  rtype = spec.get("report_incremental") if incremental and spec.get("report_incremental") else spec.get("report")
5834
  rtype = rtype or spec.get("api")
5835
  if table in ("economics", "order_metrics"):
@@ -5858,7 +6011,22 @@ def sync_window(table, mark, *, now, since=None, backfill_days=None):
5858
  # The watermark filters issued reports by creation time; it never requests one.
5859
  rtype = spec.get("report_discover")
5860
  return {"start": start, "end": end, "report": rtype,
5861
- "incremental": incremental, "cause": cause}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5862
 
5863
 
5864
  def _sync_event(kind, tenant, table, detail, rt, report, log):
@@ -5955,10 +6123,21 @@ def _pending_report_key(table, report_type, **kwargs):
5955
  return entity_key("pending_report_" + hashlib.sha256(identity.encode()).hexdigest())
5956
 
5957
 
 
 
 
 
 
 
 
 
 
 
 
5958
  @contextmanager
5959
  def _report_resume(con, table, report_type, **kwargs):
5960
  """Resume the same report/window for up to 24 hours instead of paying createReport again."""
5961
- from harness.amazon_client import AmazonReportError, AmazonReportPending
5962
  window = {key: kwargs.get(key) for key in ("start", "end", "options")}
5963
  key = _pending_report_key(table, report_type, **kwargs)
5964
  state = _read_state(con, key)
@@ -5973,7 +6152,15 @@ def _report_resume(con, table, report_type, **kwargs):
5973
  yield kwargs
5974
  except AmazonReportPending as exc:
5975
  _record_state(con, key, "pending", detail=json.dumps({
5976
- "reportId": exc.report_id, "createdAt": saved.get("createdAt", stamp), "window": window}))
 
 
 
 
 
 
 
 
5977
  raise
5978
  except AmazonReportError:
5979
  con.execute("DELETE FROM _sync_state WHERE entity=?", [key])
@@ -6012,6 +6199,21 @@ def _record_reconciliation(con, table, marker):
6012
  _record_state(con, key, "done", detail=json.dumps(marker, separators=(",", ":")))
6013
 
6014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6015
  def _replace_period(con, table, rows, columns, column, start, end=None, marker=None, *, snapshot=False):
6016
  """Atomic range/date replacement and proof; an empty restatement removes old rows too.
6017
 
@@ -6041,10 +6243,44 @@ def _replace_period(con, table, rows, columns, column, start, end=None, marker=N
6041
  if column != "_snapshot_date" or end:
6042
  raise ValueError("a snapshot replacement is keyed by one _snapshot_date")
6043
  predicate, params = "TRUE", []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6044
  con.execute("BEGIN TRANSACTION")
6045
  try:
6046
  con.execute(f'DELETE FROM {mirror} WHERE {predicate}', params)
6047
- count = _replace(con, mirror, columns, types, rows)
 
 
 
 
 
 
 
6048
  if marker is not None:
6049
  _record_reconciliation(con, table, marker)
6050
  if table == "listing_items":
@@ -6054,6 +6290,9 @@ def _replace_period(con, table, rows, columns, column, start, end=None, marker=N
6054
  except Exception:
6055
  con.execute("ROLLBACK")
6056
  raise
 
 
 
6057
 
6058
 
6059
  def document_row_count(text, roots=()):
@@ -6196,6 +6435,7 @@ def _replace_legacy_snapshot(con, table, columns, rows, meta, source_count):
6196
  dates = sorted(counts)
6197
  marker = {"start": dates[0], "end": dates[-1], "column": "_snapshot_date",
6198
  "count": source_count, "dates": counts, "document": meta.get("reportDocumentId")}
 
6199
  # Every earlier date and any undated pre-migration copy goes; AWD rows are another leg.
6200
  predicate = "COALESCE(inventory_source, 'FBA') <> 'AWD'" if table == "inventory" else "TRUE"
6201
  con.execute("BEGIN TRANSACTION")
@@ -7470,7 +7710,8 @@ def _sync_accounts_candidate(tenant_key="royal-imports", tables=None, since=None
7470
  for entry in entries:
7471
  cid = entry["connection_id"]
7472
  try:
7473
- with _account_workspace(con, tenant_key, rt, entry) as scope:
 
7474
  _sync_credential(tenant_key, rt, connection_id=cid)
7475
  result = _sync_candidate(tenant_key, tables=tables, since=since, log=log, rt=rt,
7476
  max_wait=max_wait, scheduled=scheduled, first_load=first_load,
@@ -7641,7 +7882,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7641
  ticks.update({name: None for name in (table, pairs.get(table)) if name in want})
7642
  for table in want:
7643
  tick = ticks.get(table)
7644
- # A queued resync is a repair: only Amazon's own floor and the stagger hold it, never the daily floor.
7645
  if (tick and tick["state"] == "done" and queued_resync(con, table) is not None
7646
  and read_watermark(con, table) and not _interval_reason(table, tick, end)):
7647
  ticks[table] = {**tick, "state": "resync"}
@@ -7775,6 +8016,15 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7775
  else:
7776
  for table in pending:
7777
  mark = read_watermark(con, table)
 
 
 
 
 
 
 
 
 
7778
  if not mark and since is None and table != "settlements" and table in completed:
7779
  # A local fallback cannot reopen old daily history or invent a stored watermark.
7780
  recent = end - timedelta(days=FIRST_LOAD_RECENT_DAYS)
@@ -7784,7 +8034,8 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7784
  mark = recent
7785
  if not recent <= mark <= end:
7786
  mark = recent
7787
- plan = plans[table] = sync_window(table, mark, now=end, since=since)
 
7788
  if table == SQP_TABLE:
7789
  months = _month_windows(plan["start"], plan["end"])
7790
  if not plan["incremental"]:
@@ -7798,6 +8049,16 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7798
  else:
7799
  work.append((table, None, None, None))
7800
 
 
 
 
 
 
 
 
 
 
 
7801
  if sqp_limit is not None:
7802
  def continuation_first(task):
7803
  _table, w0, w1, batch = task
@@ -7834,6 +8095,14 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7834
  and (task_end is not None or table == "settlements"
7835
  or table in PERIOD_TABLES or table in LEGACY_SNAPSHOT_TABLES)):
7836
  break
 
 
 
 
 
 
 
 
7837
  if table == SQP_TABLE and task_start is not None and _sqp_period(task_start, task_end) == "WEEK":
7838
  weekly_limit = {**sqp_limit, "refusedAsins": {}, "errors": []}
7839
  causes = []
@@ -7856,6 +8125,16 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7856
  report["nextDueSeconds"] = min(report.get("nextDueSeconds") or 900, 900)
7857
  _record_state(con, entity_key(table), "pending", 0, weekly_pending["reason"])
7858
  except Exception as exc:
 
 
 
 
 
 
 
 
 
 
7859
  causes.append(str(exc))
7860
  if isinstance(exc, cx.AmazonError) and not isinstance(exc, (cx.AmazonReportPending, cx.AmazonThrottled)):
7861
  marker = _sqp_month(con, task_start, task_end)
@@ -7872,7 +8151,6 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7872
  continue
7873
  if first_load and (table in GROUP_A_TABLES or (table in prior_refusals
7874
  and cx.REPORT_TYPES[table].get("min_interval_s"))):
7875
- tick = _read_state(con, entity_key("sync_tick_" + table))
7876
  reason = _interval_reason(table, tick, datetime.now(timezone.utc))
7877
  if reason:
7878
  report["tables"][table] = {"state": "deferred", "reason": reason}
@@ -7932,6 +8210,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7932
  proof_key = entity_key("reconcile_" + table)
7933
  previous_proof = _read_state(con, proof_key) if table in invalid_proofs else None
7934
  resync, resync_count = [], 0
 
7935
  try:
7936
  if previous_proof:
7937
  # The recorder accepts an empty object; only a parsed document can replace it with proof.
@@ -7941,9 +8220,9 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7941
  _record_state(con, entity_key("sync_tick_" + table), "attempted")
7942
  if sibling and not first_load:
7943
  _record_state(con, entity_key("sync_tick_" + sibling), "attempted")
7944
- sibling_written, sibling_cleared = 0, False
7945
- written, cols, cleared = 0, [], False
7946
- landed_window = False
7947
  pricing_asins = []
7948
  operations = {}
7949
  if first_load:
@@ -7993,12 +8272,17 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
7993
  "state": "done", "reports": len(discovered),
7994
  "downloaded": downloaded, "skipped": skipped}
7995
  if not discovered:
 
7996
  report["tables"][table] = {"rows": 0, "operations": operations,
7997
  "state": "empty", "reason": "No issued settlement reports found."}
7998
  landed.add((table, task_start, task_end, task_batch))
7999
  if first_load:
8000
  _record_state(con, key, "done", 0, "No issued settlement reports found.")
8001
- # Empty discovery earns no cursor, attempt stamp or document marker.
 
 
 
 
8002
  continue
8003
  if first_load and settlement_remaining:
8004
  break
@@ -8047,7 +8331,8 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8047
  plan["cause"] = (f"resync of {len(margin_windows)} stored windows "
8048
  f"from {_iso(margin_windows[0][0])}")
8049
  log(f"{table}: {plan['cause']}")
8050
- elif table in GROUP_A_TABLES and not first_load and len(margin_windows) > 1:
 
8051
  # One due window per table tick respects the vendor floor. The watermark
8052
  # advances only to that window's end, so the following tick loses no history.
8053
  remaining_windows = len(margin_windows) - 1
@@ -8063,6 +8348,9 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8063
  if not reason.startswith("Amazon keeps this report back to "):
8064
  _record_state(con, entity_key("limit_" + table), "done", detail="{}")
8065
  for w0, w1 in margin_windows:
 
 
 
8066
  # One detail window is one budget task, including its reconciliation query.
8067
  probe = _DocumentProbe(table, spec.get("report") or operation)
8068
  documents += 1
@@ -8078,6 +8366,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8078
  spend_rows(count)
8079
  written += count
8080
  landed_window = True
 
8081
  if table in LEGACY_SNAPSHOT_TABLES:
8082
  # The dated replacement already landed; AWD must not clear that
8083
  # document or historical FBA/AWD rows on an explicit first load.
@@ -8101,6 +8390,9 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8101
  plan["end"] = windows[0][1]
8102
  weekly = bool(table != "settlements" and cx.REPORT_TYPES[table].get("weekly"))
8103
  for w0, w1 in (() if table == "settlements" else windows):
 
 
 
8104
  # W59-T43 / C2 has no amazon.access kind. Record each credential-backed
8105
  # vendor attempt as a sync ACCESS phase, distinct from the completion event.
8106
  # Lane F/combine own the vocabulary decision; no credential or row enters C2.
@@ -8179,6 +8471,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8179
  con.execute(f"DELETE FROM {table_name(empty_table)} WHERE 1=1")
8180
  cleared = True
8181
  else:
 
8182
  continue
8183
  if sibling:
8184
  sibling_mirror = table_name(sibling)
@@ -8219,6 +8512,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8219
  _clear_week_wait(con, table, w0)
8220
  probe.finish(len(rows) if cols else 0)
8221
  spend_rows(len(rows) if cols else 0)
 
8222
  if first_load:
8223
  for reached in (table, sibling):
8224
  if reached:
@@ -8268,6 +8562,8 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8268
  log(f" pricing: getCompetitiveSummary {response_rows:,} ASIN responses, "
8269
  f"{updated:,} mirror rows enriched")
8270
  except Exception as e: # noqa: BLE001
 
 
8271
  problem = _sync_problem(e, table, "getCompetitiveSummary", tenant_key,
8272
  rt, report, log)
8273
  _record_state(con, state_key, problem["state"], 0, problem["reason"])
@@ -8307,6 +8603,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8307
  report["tables"][table]["nextWeek"] = _iso(period_repair_next)
8308
  _record_state(con, key, "done", total_rows)
8309
  _record_state(con, entity_key("sync_tick_" + table), "done", total_rows)
 
8310
  if weekly and landed_window:
8311
  # A landed week closes any earlier refusal of this table.
8312
  con.execute("DELETE FROM _sync_state WHERE entity=?", [entity_key("refused_" + table)])
@@ -8322,6 +8619,10 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8322
  or landed_window or (first_load and floors[table] is not None))
8323
  if mark is not None and has_window:
8324
  record_watermark(con, table, mark, total_rows)
 
 
 
 
8325
  _sync_event("amazon.sync", tenant_key, table,
8326
  {"state": "done", "rows": total_rows,
8327
  "operations_ok": all(v["state"] == "done" for v in operations.values())},
@@ -8341,7 +8642,10 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8341
  "incremental": sibling_plan["incremental"], "operations": {}}
8342
  _record_state(con, entity_key(sibling), "done", sibling_total)
8343
  _record_state(con, entity_key("sync_tick_" + sibling), "done", sibling_total)
 
8344
  record_watermark(con, sibling, sibling_plan["end"], sibling_total)
 
 
8345
  _sync_event("amazon.sync", tenant_key, sibling,
8346
  {"state": "done", "rows": sibling_total, "operations_ok": True},
8347
  rt, report, log)
@@ -8363,9 +8667,17 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8363
  # A failed pass retains its previous watermark so its window will be retried.
8364
  # ⚠ ONE TABLE'S REFUSAL IS NOT THE SYNC'S. A 403 on brand_analytics is R5's UNPROVEN
8365
  # ticket answering itself, and it must not cost the six tables that did work.
8366
- problem = _sync_problem(e, table, operation, tenant_key, rt, report, log)
8367
  failed.add(table)
8368
  report["tables"][table] = {"rows": 0, **problem}
 
 
 
 
 
 
 
 
8369
  if problem["state"] == "pending":
8370
  pending_reports.add(table)
8371
  report["nextDueSeconds"] = min(report.get("nextDueSeconds") or 900, 900)
@@ -8383,8 +8695,13 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
8383
  "reason": problem["reason"][:500], "amazonStatus": problem["amazonStatus"],
8384
  "diagnostic": problem.get("diagnostic", "")[:500],
8385
  "error": problem.get("error", ""), "attempts": attempts}))
8386
- _record_state(con, entity_key("sync_tick_" + refused),
8387
- "attempted" if problem["state"] == "pending" else "done", 0)
 
 
 
 
 
8388
  except Exception: # noqa: BLE001
8389
  pass
8390
  _sync_event("amazon.sync", tenant_key, table,
@@ -10612,8 +10929,8 @@ def group_a_selftest(log=print, check=None):
10612
  clock[0] += timedelta(seconds=cx.REPORT_TYPES["long_term_storage_fees"]["min_interval_s"] + 60)
10613
  idle = sync(rt.key, tables=("long_term_storage_fees",), rt=rt, log=lambda *_: None,
10614
  scheduled=True)["tables"]["long_term_storage_fees"]
10615
- ck("NC: with the queue empty the daily floor defers the table again",
10616
- idle.get("state") == "deferred" and "once a day" in idle.get("reason", ""))
10617
  finally:
10618
  con.close()
10619
 
 
463
  return {"available": True, "databases": {key: stored.get(key, True) for key in keys}}
464
 
465
 
466
+ # Day length for per-table cadence. Manual sync keeps the report's own refresh floor.
 
 
 
467
  SCHEDULED_FLOOR_SECONDS = 86400
468
+ SCHEDULED_FLOOR_GRACE_SECONDS = 0
469
+
470
+
471
+ def _cadence_seconds(table, state=None):
472
+ """Return the table's scheduled interval, using calendar months for monthly reports."""
473
+ cadence = _client().CADENCE[table]
474
+ if cadence["period"] == "week":
475
+ if state:
476
+ stamp = _utc(state["updated_at"])
477
+ return int((_week_start(stamp) + timedelta(days=7) - stamp).total_seconds())
478
+ return 7 * SCHEDULED_FLOOR_SECONDS
479
+ if cadence["period"] == "month":
480
+ stamp = _utc(state["updated_at"]) if state else None
481
+ if stamp is None:
482
+ return 30 * SCHEDULED_FLOOR_SECONDS
483
+ year, month = stamp.year + (stamp.month == 12), stamp.month % 12 + 1
484
+ due = stamp.replace(year=year, month=month, day=1,
485
+ hour=0, minute=0, second=0, microsecond=0)
486
+ return int((due - stamp).total_seconds())
487
+ return SCHEDULED_FLOOR_SECONDS // cadence["perDay"]
488
+
489
+
490
+ def sync_schedule(states, now=None):
491
+ """Return every SP-API table's last sync, next sync and configured passes per day."""
492
+ now = _utc(now)
493
+ schedule = []
494
+ for table in TABLES:
495
+ state = states.get(table)
496
+ seconds = _cadence_seconds(table, state)
497
+ _wait, due, _seconds, _scheduled = _interval_wait(table, state, now, floor_s=seconds)
498
+ if due is None and state and state.get("state") == "done":
499
+ vendor = _client().REPORT_TYPES[table].get("min_interval_s", 0)
500
+ due = _utc(state["updated_at"]) + timedelta(seconds=max(vendor, seconds))
501
+ schedule.append({"table": table, "lastSyncAt": state.get("updated_at") if state else None,
502
+ "nextSyncAt": _iso(due or now), "perDay": _client().CADENCE[table]["perDay"]})
503
+ return schedule
504
 
505
 
506
  def sync_plan(states, tables=None, *, now=None, slot_pass=False):
507
+ """Plan every due table, oldest first, with the same per-table cadence inside and outside the daily slot."""
 
 
 
 
 
 
 
 
 
 
 
508
  from harness.amazon_client import REPORT_STAGGER_SECONDS
509
  now = _utc(now)
510
  want = list(TABLES if tables is None else tables)
 
512
  if state and table != "settlements"}
513
  deferred = {}
514
  due = []
 
515
  for table in want:
516
+ floor = _cadence_seconds(table, states.get(table))
 
517
  if (reason := _interval_reason(table, states.get(table), now, floor_s=floor)):
518
  deferred[table] = reason
519
  else:
 
527
  for table in due:
528
  deferred[table] = ("table stagger: waiting for a later cadence tick; "
529
  f"minimum spacing {REPORT_STAGGER_SECONDS:g}s")
530
+ return {"tables": selected, "deferred": deferred}
531
+
532
+
533
+ def scheduled_sync_plan(tenant_key, *, rt, now=None):
534
+ """Combine enabled tables' due plans from each connection's own resident cadence ticks."""
535
+ path = _tenant_mirror_path(tenant_key, rt)
536
+ if path.resolve() != Path(datastore.DB_PATH).resolve():
537
+ raise RuntimeError("Amazon scheduling requires the tenant's resident mirror")
538
+ enabled = read_database_enablement(rt)
539
+ tables = [table for table in TABLES
540
+ if enabled["databases"].get(amazon_schema.DATABASE_PREFIX + table)]
541
+ if not tables:
542
+ return {"tables": [], "deferred": {}}
543
+ now = _utc(now)
544
+ con, _path = _connect_tenant_mirror(tenant_key, rt)
545
+ try:
546
+ due, deferred = {}, {}
547
+ for entry in account_entries(tenant_key, rt):
548
+ prefix = _account_prefix(entry["connection_id"]) + entity_key("sync_tick_")
549
+ ticks = {entity[len(prefix):]: {"state": phase, "rows": rows, "updated_at": at, "detail": detail}
550
+ for entity, phase, rows, at, detail in con.execute(
551
+ "SELECT entity, phase, rows, updated_at, cursor_wd FROM _sync_state "
552
+ "WHERE starts_with(entity, ?)", [prefix]).fetchall()}
553
+ plan = sync_plan(ticks, tables, now=now)
554
+ due.update(dict.fromkeys(plan["tables"]))
555
+ for table, reason in plan["deferred"].items():
556
+ deferred.setdefault(table, []).append(reason)
557
+ return {"tables": list(due), "deferred": {
558
+ table: "; ".join(dict.fromkeys(reasons)) for table, reasons in deferred.items() if table not in due}}
559
+ finally:
560
+ con.close()
561
 
562
 
563
  def _readable_duration(seconds):
 
573
 
574
 
575
  def _interval_wait(table, state, now, floor_s=None):
576
+ """Return the wait and deciding refresh floor, preserving the vendor floor on manual sync."""
577
+ if state and state.get("state") == "throttled" and state.get("detail"):
578
+ due = _utc(state["detail"])
579
+ wait = (due - _utc(now)).total_seconds()
580
+ if wait > 0:
581
+ return wait, due, int(wait), False
582
  vendor = (_client().REPORT_TYPES.get(table) or {}).get("min_interval_s") or 0
583
  seconds = max(int(vendor), int(floor_s or 0))
584
  if seconds and state and state["state"] == "done":
 
590
 
591
 
592
  def _interval_reason(table, state, now, floor_s=None):
593
+ wait, due, seconds, scheduled = _interval_wait(table, state, now, floor_s=floor_s)
594
  if wait <= 0:
595
  return None
596
+ if state and state.get("state") == "throttled":
597
+ return f"Amazon is limiting requests; next retry {_iso(due)}"
598
+ if scheduled:
599
+ return (f"This Amazon table refreshes every {_readable_duration(seconds)}; "
600
  f"next due {_iso(due)}")
601
  return (f"Amazon allows this report at most once every {_readable_duration(seconds)}; "
602
  f"next due {_iso(due)}")
 
4016
  return rows
4017
  refused = set(refused)
4018
  names = sorted(set(tables) | {table_name(t) for t in TABLES}, key=lambda name: (-len(name), name))
4019
+ families = ("", WATERMARK_PREFIX, BACKFILL_FLOOR_PREFIX, "sync_tick_", "resume_", "reconcile_", "limit_", "refused_",
4020
  RESYNC_PREFIX)
4021
  shared = {entity_key(key) for key in (FIRST_LOAD_END_KEY, FIRST_LOAD_STATUS_KEY, FIRST_LOAD_DUE_KEY)}
4022
 
 
4787
  con.execute(
4788
  "INSERT OR REPLACE INTO _sync_state "
4789
  "(entity, phase, last_id, cursor_wd, rows, updated_at) VALUES (?,?,?,?,?,?)",
4790
+ [str(key), str(phase), None, (str(detail) if payload_key.startswith(("amzn_reconcile_", "amzn_limit_", "amzn_refused_", "amzn_pending_report_", "amzn_resume_",
4791
  "amzn_resync_", "amzn_waiting_"))
4792
  else str(detail)[:300]) if detail else None, int(rows or 0),
4793
  _iso(datetime.now(timezone.utc))])
 
4859
 
4860
 
4861
  def _restore_first_load_wait(tenant_key, con):
4862
+ """Keep the history floor in the producer and release legacy connector-wide desk holds."""
4863
  from core import connector_sync
4864
  due = _first_load_due(con)
4865
+ connector_sync.clear_sync_defer("amazon_sp", tenant_key)
 
 
4866
  return due
4867
 
4868
 
4869
  def _loaded_refresh_hold(con, table, now, *, slot_pass=False):
4870
+ """Return a loaded table's cadence hold while its first load continues, or None when due."""
 
 
 
 
4871
  now = _utc(now)
4872
  tick = _read_state(con, entity_key("sync_tick_" + table))
4873
  # A queued resync is a repair: only Amazon's own floor holds it.
4874
  repair = queued_resync(con, table) is not None
4875
+ floor = 0 if repair else _cadence_seconds(table, tick)
4876
+ wait, due, _seconds, _scheduled = _interval_wait(table, tick, now, floor_s=floor)
4877
  reason = None
4878
  if wait > 0:
4879
  # The card parses "next due <iso>" out of a reason, so every hold keeps that phrase.
4880
+ reason = _interval_reason(table, tick, now, floor_s=floor)
 
4881
  mark = read_watermark(con, table)
4882
  if _client().REPORT_TYPES.get(table, {}).get("weekly") and mark and not repair:
4883
  week = _week_start(mark) + timedelta(days=7)
 
5175
  remaining = int(values["remaining"])
5176
  # S82-T14: one pass sees only its own tables. The job's store record counts the whole load.
5177
  job = first_load_job_summary(rt) if _store_documents(rt) else None
5178
+ if job:
5179
+ job["tables"]["inventory"] = inventory_table_status(con)
5180
  if job and isinstance(job.get("remaining"), int) and state["state"] != "done":
5181
  remaining = job["remaining"]
5182
  out = {"state": state["state"], **first_load_payload(markers, remaining,
 
5461
  def _sync_delegate(tenant_key, rt, log):
5462
  """Every table but the job's own takes one bounded first-load pass: settlements, period, Group A, SQP.
5463
 
5464
+ The same pass refreshes each loaded table whose cadence is due, so loaded data keeps moving.
5465
  """
5466
  def delegate(exclude, budget):
5467
  tables = None if exclude is None else [t for t in TABLES if t not in set(exclude)]
 
5521
  return max(stamps, key=lambda item: item[0])[1] if stamps else None
5522
 
5523
 
5524
+ def inventory_table_status(con):
5525
+ """Read inventory counts and account-scoped source proof without changing the mirror or requesting reports."""
5526
+ rows = (int(con.execute("SELECT COUNT(*) FROM amzn_inventory").fetchone()[0])
5527
+ if _columns_of(con, "amzn_inventory") else 0)
5528
+ out = {"rows": rows, "empty": rows == 0}
5529
+ if rows:
5530
+ return out
5531
+ keys = ("amzn_inventory", "amzn_reconcile_inventory", "amzn_refused_inventory", "amzn_inventory_awd")
5532
+ clauses = " OR ".join("entity=? OR ends_with(entity, ?)" for _ in keys)
5533
+ saved = con.execute(
5534
+ "SELECT entity, phase, cursor_wd, rows, updated_at FROM _sync_state WHERE " + clauses
5535
+ + " OR contains(entity, 'amzn_pending_report_')",
5536
+ [value for key in keys for value in (key, "__" + key)]).fetchall()
5537
+ accounts, pending, unknown_request = {}, [], False
5538
+ for entity, phase, detail, count, stamp in saved:
5539
+ prefix, key = (str(entity).split("__", 1) if str(entity).startswith("amzn_account_")
5540
+ and "__" in str(entity) else ("", str(entity)))
5541
+ if key.startswith("amzn_pending_report_"):
5542
+ try:
5543
+ request = json.loads(detail or "{}")
5544
+ except (ValueError, TypeError):
5545
+ request = {}
5546
+ if not isinstance(request, dict):
5547
+ request = {}
5548
+ if request.get("table") == "inventory":
5549
+ pending.append({"type": request.get("reportType"), "state": phase,
5550
+ "reportId": request.get("reportId"),
5551
+ "processingStatus": request.get("processingStatus"), "updatedAt": stamp})
5552
+ elif not request.get("table"):
5553
+ unknown_request = True
5554
+ continue
5555
+ accounts.setdefault(prefix, {})[key] = {"state": phase, "detail": detail,
5556
+ "rows": count, "updated_at": stamp}
5557
+ if pending:
5558
+ return {**out, "code": "first_load_pending", "reason":
5559
+ "Amazon is preparing the inventory report, so keep sync enabled and retry after it completes.",
5560
+ "reports": pending}
5561
+ # Account markers own their proof once account scoping exists.
5562
+ scoped = {key: state for key, state in accounts.items() if key}
5563
+ accounts = scoped or accounts
5564
+ reports = []
5565
+ for states in accounts.values():
5566
+ marker = states.get("amzn_inventory") or {}
5567
+ refused = refused_reports(states)
5568
+ if refused:
5569
+ return {**out, "code": "refused_by_amazon", "reason": refused[0]["reason"]}
5570
+ proof = states.get("amzn_reconcile_inventory") or {}
5571
+ try:
5572
+ detail = json.loads(proof.get("detail") or "{}")
5573
+ except (ValueError, TypeError):
5574
+ detail = {}
5575
+ if not isinstance(detail, dict):
5576
+ detail = {}
5577
+ dates = detail.get("dates")
5578
+ valid_dates = (isinstance(dates, dict) and bool(dates)
5579
+ and all(isinstance(day, str) and re.fullmatch(r"\d{4}-\d{2}-\d{2}", day)
5580
+ and type(count) is int and count == 0 for day, count in dates.items()))
5581
+ awd = states.get("amzn_inventory_awd") or {}
5582
+ if (marker.get("state") != "done" or marker.get("rows") != 0 or proof.get("state") != "done"
5583
+ or type(detail.get("count")) is not int or detail["count"] != 0
5584
+ or not isinstance(detail.get("document"), str) or not detail["document"].strip()
5585
+ or detail.get("column") != "_snapshot_date" or not valid_dates
5586
+ or detail.get("start") != min(dates) or detail.get("end") != max(dates)
5587
+ or awd.get("state") != "done" or awd.get("rows") != 0):
5588
+ return {**out, "code": "first_load_pending", "reason":
5589
+ "Inventory source checks are incomplete, so retry Amazon sync to finish the FBA and AWD inventory checks."}
5590
+ reports.append({"type": _client().REPORT_TYPES["inventory"]["report"], "state": proof["state"],
5591
+ "reportId": detail.get("reportId"), "processingStatus": detail.get("processingStatus"),
5592
+ "documentId": detail["document"], "start": detail["start"], "end": detail["end"],
5593
+ "rows": detail["count"], "updatedAt": proof.get("updated_at")})
5594
+ if not reports:
5595
+ if unknown_request:
5596
+ return {**out, "code": "first_load_pending", "reason":
5597
+ "A saved Amazon request has no table identity, so retry sync to refresh the inventory report status."}
5598
+ return {**out, "code": "not_requested", "reason":
5599
+ "No inventory report request is saved, so start Amazon sync to request the FBA ledger."}
5600
+ periods = {(report["start"], report["end"]) for report in reports}
5601
+ start, end = next(iter(periods))
5602
+ period = (start if start == end else f"{start} to {end}") if len(periods) == 1 else "the loaded dates"
5603
+ return {**out, "code": "no_source_rows", "reason":
5604
+ f"Amazon returned no FBA ledger rows for {period} and no AWD inventory, so check seller stock and retry Amazon sync.",
5605
+ "reports": reports}
5606
+
5607
+
5608
+ def first_load_table_status(tasks, finished, failed, counts, *, table_status=None):
5609
  """D-2643, D-2642, D-2649: a row count or a named reason per table, and why orders are behind."""
5610
  out = {}
5611
  for table in dict.fromkeys(EMPTY_REASON_TABLES + BEHIND_TABLES):
 
5619
  f"The first load is still loading this table. {len(left)} report windows remain."
5620
  if left else "Amazon returned no rows for this seller in the loaded period."
5621
  if own else "This table holds no rows and the first load owes it no windows.")
5622
+ if table == "inventory":
5623
+ row.update(empty=True, code="first_load_pending", reason=
5624
+ "Inventory source proof is not available in this job record, so retry Amazon sync to refresh its status.")
5625
  if table in BEHIND_TABLES and left:
5626
  done = [t for t in own if t["id"] in finished or t.get("landed")]
5627
  oldest = min((t["start"] for t in done if t.get("start")), default=None)
5628
  row["behind"] = (f"The first load is still loading this table. {len(left)} of {len(own)} report "
5629
  "windows remain" + (f", and history before {oldest[:10]} is not loaded yet." if oldest
5630
  else ", starting with the most recent month."))
5631
+ if table_status and table in table_status:
5632
+ row.update(table_status[table])
5633
+ if not row.get("empty"):
5634
+ row.pop("reason", None)
5635
+ row.pop("code", None)
5636
  out[table] = row
5637
  return out
5638
 
5639
 
5640
+ def first_load_job_summary(docs, *, table_status=None):
5641
  """The latest generation record and the cursor's remaining count, read from the store."""
5642
  try:
5643
  record = _doc(docs, FIRST_LOAD_GENERATIONS_KEY)
 
5651
  "remaining": record.get("remaining", last.get("remainingAfter")),
5652
  "tablesPublished": list(last.get("tablesPublished") or []), "end": last.get("end"),
5653
  "operation": last.get("operation"), "resume_at": last.get("resume_at"),
5654
+ "tables": {**(last.get("tables") or {}), **(table_status or {})}}
5655
 
5656
 
5657
  def _record_generation(docs, row):
 
5927
  row_counts = counts(list(dict.fromkeys(EMPTY_REASON_TABLES + BEHIND_TABLES)))
5928
  except Exception: # noqa: BLE001
5929
  row_counts = {}
5930
+ table_status = None
5931
+ if plan is None:
5932
+ con, _path = _connect_tenant_mirror(key, rt)
5933
+ try:
5934
+ table_status = {"inventory": inventory_table_status(con)}
5935
+ finally:
5936
+ con.close()
5937
+ record["tables"] = first_load_table_status(after, finished, failed, row_counts,
5938
+ table_status=table_status)
5939
  record["state"] = "done" if not record["remainingAfter"] else "partial"
5940
  status_after = read_connector_status(docs)
5941
  record["odooLastGoodSyncAt"] = {"start": _status_stamp(status_before, "odoo"),
 
5960
  pass
5961
 
5962
 
5963
+ def sync_window(table, mark, *, now, since=None, backfill_days=None, intraday=False):
5964
  """Pure request decision for one table: start, end, report, incremental and cause."""
5965
  end = _utc(now)
5966
  days = DEFAULT_BACKFILL_DAYS if backfill_days is None else backfill_days
 
5978
  start, incremental = end - timedelta(days=days), False
5979
  cause = f"first full load: {days} days of backfill"
5980
  spec = _client().REPORT_TYPES.get(table) or {}
5981
+ if incremental and not spec.get("snapshot") and not _periodic(spec):
5982
+ cadence = _client().CADENCE[table]
5983
+ lookback = cadence["intradayLookbackDays" if intraday else "lookbackDays"]
5984
+ start = min(start, end - timedelta(days=lookback))
5985
+ cause += f"; re-read {lookback} days ({'intraday' if intraday else 'daily'})"
5986
  rtype = spec.get("report_incremental") if incremental and spec.get("report_incremental") else spec.get("report")
5987
  rtype = rtype or spec.get("api")
5988
  if table in ("economics", "order_metrics"):
 
6011
  # The watermark filters issued reports by creation time; it never requests one.
6012
  rtype = spec.get("report_discover")
6013
  return {"start": start, "end": end, "report": rtype,
6014
+ "incremental": incremental, "intraday": intraday, "cause": cause}
6015
+
6016
+
6017
+ def _lookback_intraday(con, table, now, *, slot_pass=False):
6018
+ """Use the daily window until this account has completed it, including after a failed attempt."""
6019
+ day = _read_state(con, entity_key("lookback_day_" + table))
6020
+ return bool(not slot_pass and day and day.get("state") == "done"
6021
+ and day.get("detail") == now.date().isoformat())
6022
+
6023
+
6024
+ def _record_lookback_day(con, table, plan, now):
6025
+ """Only a completed incremental daily request earns this account's intraday window."""
6026
+ if (plan["incremental"] and not plan.get("intraday")
6027
+ and plan["end"] >= now - timedelta(minutes=2)
6028
+ and not _lookback_intraday(con, table, now)):
6029
+ _record_state(con, entity_key("lookback_day_" + table), "done", detail=now.date().isoformat())
6030
 
6031
 
6032
  def _sync_event(kind, tenant, table, detail, rt, report, log):
 
6123
  return entity_key("pending_report_" + hashlib.sha256(identity.encode()).hexdigest())
6124
 
6125
 
6126
+ def _save_throttled_resume(con, table, exc, plan, completed, *, first_load, since):
6127
+ """Keep the continuation beside the account's markers and leave the retry tick as an ISO deadline."""
6128
+ continuation = {**exc.as_resume(), "plan": {**plan, "start": _iso(plan["start"]), "end": _iso(plan["end"])},
6129
+ "completed": completed, "first_load": first_load,
6130
+ "since": _iso(_utc(since)) if since is not None else None}
6131
+ _record_state(con, entity_key("resume_" + table), "throttled", detail=json.dumps(continuation))
6132
+ due = datetime.now(timezone.utc) + timedelta(seconds=max(60.0, float(exc.retry_after or 60)))
6133
+ _record_state(con, entity_key("sync_tick_" + table), "throttled", detail=_iso(due))
6134
+ return max(0, (due - datetime.now(timezone.utc)).total_seconds())
6135
+
6136
+
6137
  @contextmanager
6138
  def _report_resume(con, table, report_type, **kwargs):
6139
  """Resume the same report/window for up to 24 hours instead of paying createReport again."""
6140
+ from harness.amazon_client import AmazonReportError, AmazonReportPending, AmazonThrottled
6141
  window = {key: kwargs.get(key) for key in ("start", "end", "options")}
6142
  key = _pending_report_key(table, report_type, **kwargs)
6143
  state = _read_state(con, key)
 
6152
  yield kwargs
6153
  except AmazonReportPending as exc:
6154
  _record_state(con, key, "pending", detail=json.dumps({
6155
+ "reportId": exc.report_id, "createdAt": saved.get("createdAt", stamp), "window": window,
6156
+ "table": table, "reportType": report_type, "processingStatus": exc.status}))
6157
+ raise
6158
+ except AmazonThrottled as exc:
6159
+ if exc.report_id is not None:
6160
+ _record_state(con, key, "throttled", detail=json.dumps({
6161
+ **exc.as_resume(), "reportId": str(exc.report_id),
6162
+ "createdAt": saved.get("createdAt", stamp), "window": window,
6163
+ "table": table, "reportType": report_type}))
6164
  raise
6165
  except AmazonReportError:
6166
  con.execute("DELETE FROM _sync_state WHERE entity=?", [key])
 
6199
  _record_state(con, key, "done", detail=json.dumps(marker, separators=(",", ":")))
6200
 
6201
 
6202
+ def _restatement_may_overlap(con, mirror, predicate, params, start, end, *, unsafe="TRUE"):
6203
+ """Treat absent or invalid legacy period metadata as unknown when checking retained rows."""
6204
+ columns = set(_columns_of(con, mirror))
6205
+ def period(name):
6206
+ return f'TRY_CAST("{name}" AS TIMESTAMPTZ)' if name in columns else "NULL::TIMESTAMPTZ"
6207
+ old_start, old_end = period("_period_start"), period("_period_end")
6208
+ return bool(con.execute(
6209
+ f'SELECT 1 FROM {mirror} WHERE NOT COALESCE(({predicate}), FALSE) AND ({unsafe}) '
6210
+ f'AND ({old_start} IS NULL OR TRY_CAST(? AS TIMESTAMPTZ) IS NULL '
6211
+ f'OR {old_start} < TRY_CAST(? AS TIMESTAMPTZ)) '
6212
+ f'AND ({old_end} IS NULL OR {old_start} IS NULL OR {old_end} <= {old_start} '
6213
+ f'OR {old_end} > TRY_CAST(? AS TIMESTAMPTZ)) LIMIT 1',
6214
+ [*params, end, end, start]).fetchone())
6215
+
6216
+
6217
  def _replace_period(con, table, rows, columns, column, start, end=None, marker=None, *, snapshot=False):
6218
  """Atomic range/date replacement and proof; an empty restatement removes old rows too.
6219
 
 
6243
  if column != "_snapshot_date" or end:
6244
  raise ValueError("a snapshot replacement is keyed by one _snapshot_date")
6245
  predicate, params = "TRUE", []
6246
+ source_keys = _client().REPORT_TYPES[table].get("restatement_keys", ())
6247
+ staged = None
6248
+ if source_keys:
6249
+ overlap_end = (marker or {}).get("end") or end
6250
+ missing = set(source_keys) - set(columns)
6251
+ if missing:
6252
+ # A same-window replay remains safe without source keys; a moving overlap must refuse.
6253
+ if _restatement_may_overlap(con, mirror, predicate, params, start, overlap_end):
6254
+ raise ValueError(f"{table}: overlapping restatement omitted source keys: "
6255
+ + ", ".join(sorted(missing)) + "; request the complete source report")
6256
+ elif rows:
6257
+ staged = "amazon_restatement_" + uuid.uuid4().hex
6258
+ names = ", ".join(_quoted_identifier(c) for c in columns)
6259
+ con.execute(f"CREATE TEMP TABLE {staged} AS SELECT {names} FROM {mirror} WHERE FALSE")
6260
+ try:
6261
+ _replace(con, staged, columns, types, rows, source_table=mirror)
6262
+ required = [c for c in source_keys if c not in ("fnsku", "asin")]
6263
+ invalid = " OR ".join(f'NULLIF(TRIM(CAST({_quoted_identifier(c)} AS VARCHAR)), \'\') IS NULL'
6264
+ for c in required)
6265
+ if con.execute(f"SELECT 1 FROM {staged} WHERE {invalid} LIMIT 1").fetchone():
6266
+ raise ValueError(f"{table}: restatement omitted a source key value; request the complete source report")
6267
+ if _restatement_may_overlap(con, mirror, predicate, params, start, overlap_end, unsafe=invalid):
6268
+ raise ValueError(f"{table}: overlapping legacy rows omitted a source key value; "
6269
+ "restore the complete source report before restating")
6270
+ except BaseException:
6271
+ con.execute(f"DROP TABLE {staged}")
6272
+ raise
6273
  con.execute("BEGIN TRANSACTION")
6274
  try:
6275
  con.execute(f'DELETE FROM {mirror} WHERE {predicate}', params)
6276
+ if staged:
6277
+ matches = " AND ".join(f'{mirror}.{_quoted_identifier(c)} IS NOT DISTINCT FROM '
6278
+ f'k.{_quoted_identifier(c)}' for c in source_keys)
6279
+ con.execute(f'DELETE FROM {mirror} WHERE EXISTS (SELECT 1 FROM {staged} k WHERE {matches})')
6280
+ con.execute(f"INSERT INTO {mirror} ({names}) SELECT {names} FROM {staged}")
6281
+ count = con.execute(f"SELECT COUNT(*) FROM {staged}").fetchone()[0]
6282
+ else:
6283
+ count = _replace(con, mirror, columns, types, rows)
6284
  if marker is not None:
6285
  _record_reconciliation(con, table, marker)
6286
  if table == "listing_items":
 
6290
  except Exception:
6291
  con.execute("ROLLBACK")
6292
  raise
6293
+ finally:
6294
+ if staged:
6295
+ con.execute(f"DROP TABLE {staged}")
6296
 
6297
 
6298
  def document_row_count(text, roots=()):
 
6435
  dates = sorted(counts)
6436
  marker = {"start": dates[0], "end": dates[-1], "column": "_snapshot_date",
6437
  "count": source_count, "dates": counts, "document": meta.get("reportDocumentId")}
6438
+ marker.update({key: meta[key] for key in ("reportId", "processingStatus") if meta.get(key)})
6439
  # Every earlier date and any undated pre-migration copy goes; AWD rows are another leg.
6440
  predicate = "COALESCE(inventory_source, 'FBA') <> 'AWD'" if table == "inventory" else "TRUE"
6441
  con.execute("BEGIN TRANSACTION")
 
7710
  for entry in entries:
7711
  cid = entry["connection_id"]
7712
  try:
7713
+ with _account_workspace(con, tenant_key, rt, entry) as scope, \
7714
+ _client().rate_limit_scope(tenant_key, cid):
7715
  _sync_credential(tenant_key, rt, connection_id=cid)
7716
  result = _sync_candidate(tenant_key, tables=tables, since=since, log=log, rt=rt,
7717
  max_wait=max_wait, scheduled=scheduled, first_load=first_load,
 
7882
  ticks.update({name: None for name in (table, pairs.get(table)) if name in want})
7883
  for table in want:
7884
  tick = ticks.get(table)
7885
+ # A queued resync is a repair: only Amazon's own floor and the stagger hold it.
7886
  if (tick and tick["state"] == "done" and queued_resync(con, table) is not None
7887
  and read_watermark(con, table) and not _interval_reason(table, tick, end)):
7888
  ticks[table] = {**tick, "state": "resync"}
 
8016
  else:
8017
  for table in pending:
8018
  mark = read_watermark(con, table)
8019
+ if table == "settlements" and since is None:
8020
+ # Empty discovery earns a request cutoff, never a stored source watermark.
8021
+ tick = _read_state(con, entity_key("sync_tick_" + table))
8022
+ if tick and tick.get("state") == "done":
8023
+ mark = _utc(tick["updated_at"])
8024
+ elif not mark:
8025
+ day = _read_state(con, entity_key("lookback_day_" + table))
8026
+ if day and day.get("state") == "done":
8027
+ mark = _utc(day["updated_at"])
8028
  if not mark and since is None and table != "settlements" and table in completed:
8029
  # A local fallback cannot reopen old daily history or invent a stored watermark.
8030
  recent = end - timedelta(days=FIRST_LOAD_RECENT_DAYS)
 
8034
  mark = recent
8035
  if not recent <= mark <= end:
8036
  mark = recent
8037
+ plan = plans[table] = sync_window(table, mark, now=end, since=since,
8038
+ intraday=_lookback_intraday(con, table, end, slot_pass=slot_pass))
8039
  if table == SQP_TABLE:
8040
  months = _month_windows(plan["start"], plan["end"])
8041
  if not plan["incremental"]:
 
8049
  else:
8050
  work.append((table, None, None, None))
8051
 
8052
+ resumes = {}
8053
+ for table in pending:
8054
+ saved = _read_state(con, entity_key("resume_" + table))
8055
+ continuation = json.loads(saved["detail"]) if saved and saved.get("detail") else {}
8056
+ if (continuation and continuation.get("first_load") == first_load
8057
+ and continuation.get("since") == (_iso(_utc(since)) if since is not None else None)):
8058
+ resumes[table] = continuation
8059
+ plans[table] = {**continuation["plan"], "start": _utc(continuation["plan"]["start"]),
8060
+ "end": _utc(continuation["plan"]["end"])}
8061
+
8062
  if sqp_limit is not None:
8063
  def continuation_first(task):
8064
  _table, w0, w1, batch = task
 
8095
  and (task_end is not None or table == "settlements"
8096
  or table in PERIOD_TABLES or table in LEGACY_SNAPSHOT_TABLES)):
8097
  break
8098
+ tick = _read_state(con, entity_key("sync_tick_" + table))
8099
+ if first_load and (tick or {}).get("state") == "throttled":
8100
+ reason = _interval_reason(table, tick, datetime.now(timezone.utc))
8101
+ if reason:
8102
+ report["tables"][table] = {"state": "deferred", "reason": reason}
8103
+ wait = _interval_wait(table, tick, datetime.now(timezone.utc))[0]
8104
+ report["nextDueSeconds"] = min(report.get("nextDueSeconds") or wait, wait)
8105
+ continue
8106
  if table == SQP_TABLE and task_start is not None and _sqp_period(task_start, task_end) == "WEEK":
8107
  weekly_limit = {**sqp_limit, "refusedAsins": {}, "errors": []}
8108
  causes = []
 
8125
  report["nextDueSeconds"] = min(report.get("nextDueSeconds") or 900, 900)
8126
  _record_state(con, entity_key(table), "pending", 0, weekly_pending["reason"])
8127
  except Exception as exc:
8128
+ if isinstance(exc, cx.AmazonThrottled):
8129
+ problem = _sync_problem(exc, table, exc.operation or "report_document", tenant_key, rt, report, log)
8130
+ report["tables"][table] = problem
8131
+ failed.add(table)
8132
+ pending_reports.add(table)
8133
+ wait = _save_throttled_resume(con, table, exc, plans[table], [],
8134
+ first_load=first_load, since=since)
8135
+ report["nextDueSeconds"] = min(report.get("nextDueSeconds") or wait, wait)
8136
+ _record_state(con, entity_key(table), "throttled", detail=problem["reason"])
8137
+ continue
8138
  causes.append(str(exc))
8139
  if isinstance(exc, cx.AmazonError) and not isinstance(exc, (cx.AmazonReportPending, cx.AmazonThrottled)):
8140
  marker = _sqp_month(con, task_start, task_end)
 
8151
  continue
8152
  if first_load and (table in GROUP_A_TABLES or (table in prior_refusals
8153
  and cx.REPORT_TYPES[table].get("min_interval_s"))):
 
8154
  reason = _interval_reason(table, tick, datetime.now(timezone.utc))
8155
  if reason:
8156
  report["tables"][table] = {"state": "deferred", "reason": reason}
 
8210
  proof_key = entity_key("reconcile_" + table)
8211
  previous_proof = _read_state(con, proof_key) if table in invalid_proofs else None
8212
  resync, resync_count = [], 0
8213
+ completed_windows = list(resumes.get(table, {}).get("completed", []))
8214
  try:
8215
  if previous_proof:
8216
  # The recorder accepts an empty object; only a parsed document can replace it with proof.
 
8220
  _record_state(con, entity_key("sync_tick_" + table), "attempted")
8221
  if sibling and not first_load:
8222
  _record_state(con, entity_key("sync_tick_" + sibling), "attempted")
8223
+ sibling_written, sibling_cleared = 0, bool(completed_windows)
8224
+ written, cols, cleared = 0, [], bool(completed_windows)
8225
+ landed_window = bool(completed_windows)
8226
  pricing_asins = []
8227
  operations = {}
8228
  if first_load:
 
8272
  "state": "done", "reports": len(discovered),
8273
  "downloaded": downloaded, "skipped": skipped}
8274
  if not discovered:
8275
+ con.execute("DELETE FROM _sync_state WHERE entity=?", [entity_key("resume_" + table)])
8276
  report["tables"][table] = {"rows": 0, "operations": operations,
8277
  "state": "empty", "reason": "No issued settlement reports found."}
8278
  landed.add((table, task_start, task_end, task_batch))
8279
  if first_load:
8280
  _record_state(con, key, "done", 0, "No issued settlement reports found.")
8281
+ # A successful empty scheduled discovery earns cadence, but no source cursor or document marker.
8282
+ if scheduled and not first_load:
8283
+ _record_state(con, entity_key("sync_tick_" + table), "done", 0)
8284
+ if not first_load and since is None:
8285
+ _record_lookback_day(con, table, {**plan, "incremental": True}, end)
8286
  continue
8287
  if first_load and settlement_remaining:
8288
  break
 
8331
  plan["cause"] = (f"resync of {len(margin_windows)} stored windows "
8332
  f"from {_iso(margin_windows[0][0])}")
8333
  log(f"{table}: {plan['cause']}")
8334
+ elif (table in GROUP_A_TABLES and not first_load and len(margin_windows) > 1
8335
+ and not spec.get("restatement_keys")):
8336
  # One due window per table tick respects the vendor floor. The watermark
8337
  # advances only to that window's end, so the following tick loses no history.
8338
  remaining_windows = len(margin_windows) - 1
 
8348
  if not reason.startswith("Amazon keeps this report back to "):
8349
  _record_state(con, entity_key("limit_" + table), "done", detail="{}")
8350
  for w0, w1 in margin_windows:
8351
+ window = [_iso(w0) if w0 is not None else None, _iso(w1) if w1 is not None else None]
8352
+ if window in completed_windows:
8353
+ continue
8354
  # One detail window is one budget task, including its reconciliation query.
8355
  probe = _DocumentProbe(table, spec.get("report") or operation)
8356
  documents += 1
 
8366
  spend_rows(count)
8367
  written += count
8368
  landed_window = True
8369
+ completed_windows.append(window)
8370
  if table in LEGACY_SNAPSHOT_TABLES:
8371
  # The dated replacement already landed; AWD must not clear that
8372
  # document or historical FBA/AWD rows on an explicit first load.
 
8390
  plan["end"] = windows[0][1]
8391
  weekly = bool(table != "settlements" and cx.REPORT_TYPES[table].get("weekly"))
8392
  for w0, w1 in (() if table == "settlements" else windows):
8393
+ window = [_iso(w0), _iso(w1)]
8394
+ if window in completed_windows:
8395
+ continue
8396
  # W59-T43 / C2 has no amazon.access kind. Record each credential-backed
8397
  # vendor attempt as a sync ACCESS phase, distinct from the completion event.
8398
  # Lane F/combine own the vocabulary decision; no credential or row enters C2.
 
8471
  con.execute(f"DELETE FROM {table_name(empty_table)} WHERE 1=1")
8472
  cleared = True
8473
  else:
8474
+ completed_windows.append(window)
8475
  continue
8476
  if sibling:
8477
  sibling_mirror = table_name(sibling)
 
8512
  _clear_week_wait(con, table, w0)
8513
  probe.finish(len(rows) if cols else 0)
8514
  spend_rows(len(rows) if cols else 0)
8515
+ completed_windows.append(window)
8516
  if first_load:
8517
  for reached in (table, sibling):
8518
  if reached:
 
8562
  log(f" pricing: getCompetitiveSummary {response_rows:,} ASIN responses, "
8563
  f"{updated:,} mirror rows enriched")
8564
  except Exception as e: # noqa: BLE001
8565
+ if isinstance(e, cx.AmazonThrottled):
8566
+ raise
8567
  problem = _sync_problem(e, table, "getCompetitiveSummary", tenant_key,
8568
  rt, report, log)
8569
  _record_state(con, state_key, problem["state"], 0, problem["reason"])
 
8603
  report["tables"][table]["nextWeek"] = _iso(period_repair_next)
8604
  _record_state(con, key, "done", total_rows)
8605
  _record_state(con, entity_key("sync_tick_" + table), "done", total_rows)
8606
+ con.execute("DELETE FROM _sync_state WHERE entity=?", [entity_key("resume_" + table)])
8607
  if weekly and landed_window:
8608
  # A landed week closes any earlier refusal of this table.
8609
  con.execute("DELETE FROM _sync_state WHERE entity=?", [entity_key("refused_" + table)])
 
8619
  or landed_window or (first_load and floors[table] is not None))
8620
  if mark is not None and has_window:
8621
  record_watermark(con, table, mark, total_rows)
8622
+ if not first_load and not resync:
8623
+ completed_plan = ({**plan, "incremental": True}
8624
+ if table == "settlements" and since is None else plan)
8625
+ _record_lookback_day(con, table, completed_plan, end)
8626
  _sync_event("amazon.sync", tenant_key, table,
8627
  {"state": "done", "rows": total_rows,
8628
  "operations_ok": all(v["state"] == "done" for v in operations.values())},
 
8642
  "incremental": sibling_plan["incremental"], "operations": {}}
8643
  _record_state(con, entity_key(sibling), "done", sibling_total)
8644
  _record_state(con, entity_key("sync_tick_" + sibling), "done", sibling_total)
8645
+ con.execute("DELETE FROM _sync_state WHERE entity=?", [entity_key("resume_" + sibling)])
8646
  record_watermark(con, sibling, sibling_plan["end"], sibling_total)
8647
+ if not first_load:
8648
+ _record_lookback_day(con, sibling, sibling_plan, end)
8649
  _sync_event("amazon.sync", tenant_key, sibling,
8650
  {"state": "done", "rows": sibling_total, "operations_ok": True},
8651
  rt, report, log)
 
8667
  # A failed pass retains its previous watermark so its window will be retried.
8668
  # ⚠ ONE TABLE'S REFUSAL IS NOT THE SYNC'S. A 403 on brand_analytics is R5's UNPROVEN
8669
  # ticket answering itself, and it must not cost the six tables that did work.
8670
+ problem = _sync_problem(e, table, getattr(e, "operation", None) or operation, tenant_key, rt, report, log)
8671
  failed.add(table)
8672
  report["tables"][table] = {"rows": 0, **problem}
8673
+ if problem["state"] == "throttled":
8674
+ landed.discard((table, task_start, task_end, task_batch))
8675
+ pending_reports.add(table)
8676
+ if _columns_of(con, mirror):
8677
+ report["tables"][table]["rows"] = con.execute(f"SELECT COUNT(*) FROM {mirror}").fetchone()[0]
8678
+ wait = _save_throttled_resume(con, table, e, plan, completed_windows,
8679
+ first_load=first_load, since=since)
8680
+ report["nextDueSeconds"] = min(report.get("nextDueSeconds") or wait, wait)
8681
  if problem["state"] == "pending":
8682
  pending_reports.add(table)
8683
  report["nextDueSeconds"] = min(report.get("nextDueSeconds") or 900, 900)
 
8695
  "reason": problem["reason"][:500], "amazonStatus": problem["amazonStatus"],
8696
  "diagnostic": problem.get("diagnostic", "")[:500],
8697
  "error": problem.get("error", ""), "attempts": attempts}))
8698
+ if problem["state"] == "throttled":
8699
+ if refused != table:
8700
+ _save_throttled_resume(con, refused, e, sibling_plan, completed_windows,
8701
+ first_load=first_load, since=since)
8702
+ else:
8703
+ _record_state(con, entity_key("sync_tick_" + refused),
8704
+ "attempted" if problem["state"] == "pending" else "done")
8705
  except Exception: # noqa: BLE001
8706
  pass
8707
  _sync_event("amazon.sync", tenant_key, table,
 
10929
  clock[0] += timedelta(seconds=cx.REPORT_TYPES["long_term_storage_fees"]["min_interval_s"] + 60)
10930
  idle = sync(rt.key, tables=("long_term_storage_fees",), rt=rt, log=lambda *_: None,
10931
  scheduled=True)["tables"]["long_term_storage_fees"]
10932
+ ck("NC: with the queue empty the table cadence defers it again",
10933
+ idle.get("state") == "deferred" and "next due " in idle.get("reason", ""))
10934
  finally:
10935
  con.close()
10936
 
platform/modules/account_health.py CHANGED
@@ -24,6 +24,7 @@ import hashlib
24
  import json
25
  import math
26
  from pathlib import Path
 
27
 
28
  APP_LABEL = "Account health"
29
  SOURCE_KIND = "account_health"
@@ -55,6 +56,11 @@ SETTLEMENT_TABLE = "amzn_settlements"
55
  COMPLAINT_METRICS = ("received_intellectual_property_complaints", "suspected_intellectual_property_violations",
56
  "product_authenticity_customer_complaints")
57
  SOURCE = "ut_amzn_account_health"
 
 
 
 
 
58
  NOTIFICATION_KIND = "account_health_new_items"
59
  _sync_delivery = None
60
 
@@ -463,7 +469,7 @@ def _page_bounds(offset, limit, total):
463
  return start, size
464
 
465
 
466
- def summary_page(tenant_key, *, search="", offset=0, limit=PAGE_SIZE, metric=None, st=None):
467
  """Read one metric page or one exact metric without loading the complete snapshot."""
468
  st = _rt(tenant_key, st)
469
  distance = settings(tenant_key, st=st)["alertDistancePct"]
@@ -495,6 +501,8 @@ def summary_page(tenant_key, *, search="", offset=0, limit=PAGE_SIZE, metric=Non
495
  "ELSE CASE WHEN CONTAINS(UPPER(condition), 'EQUAL') OR target = 0 THEN 'At most' ELSE 'Below' END END) "
496
  "|| ' ' || CASE WHEN unit = 'percent' THEN printf('%.2f%%', target) ELSE CAST(target AS VARCHAR) END END, "
497
  "condition, window_from, window_to)), ?)", [str(search or "").strip().lower()])
 
 
498
  folded = ", ".join(f"'{s}'" for s in FOLDED)
499
  total, folded_count = cur.execute(
500
  sql + f" SELECT COUNT(*), COUNT(*) FILTER (WHERE status IN ({folded})) FROM metrics WHERE {where}",
@@ -530,6 +538,110 @@ def summary_page(tenant_key, *, search="", offset=0, limit=PAGE_SIZE, metric=Non
530
  "cursor": str(start + size) if start + size < out["metricTotal"] else None}
531
 
532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533
  # --------------------------------------------------------------------------- settings and owner
534
 
535
  #: Accounts that sign in for a process, never a person who can own an alert (same list as amazon_profit).
@@ -919,7 +1031,7 @@ def _title(row):
919
  return f"{row['label']} is {'at risk' if row['status'] == 'At risk' else 'near target'}"
920
 
921
 
922
- def _alert_source(tenant_key, st, row, snapshot_date):
923
  """Bind an alert to the actual mirror row supplying its metric value."""
924
  from harness.amazon_schema import IDENTITY_PID
925
  column = row["key"] + ("_defects_count" if row["key"] in dict(COUNT_METRICS) else "_rate")
@@ -930,6 +1042,7 @@ def _alert_source(tenant_key, st, row, snapshot_date):
930
  return {"source": None, "fields": fields}
931
  fields += [name for name in (row["key"] + "_target_value", row["key"] + "_target_condition")
932
  if name in cols]
 
933
  order = ' ORDER BY "_report_row_number"' if "_report_row_number" in cols else ""
934
  row_ids = []
935
  for field in (name for name in fields if name not in {"_snapshot_date", "marketplace_id"}):
@@ -945,6 +1058,14 @@ def _alert_source(tenant_key, st, row, snapshot_date):
945
  "fields": fields}
946
 
947
 
 
 
 
 
 
 
 
 
948
  def raise_alerts(tenant_key, view=None, *, st=None, now=None):
949
  """Raise one owned alert per metric that is near or past its target, never a second while one is open."""
950
  st = _rt(tenant_key, st)
@@ -1731,7 +1852,7 @@ def validate_dashboard(tenant_key, figures=None, *, start=None, end=None, st=Non
1731
  return {"ok": bool(detail) and all(check["ok"] for check in detail), "detail": detail}
1732
 
1733
  def validate(tenant_key, *, st=None, **period):
1734
- """Tie the rating and each rate in `summary` to the raw `ut_amzn_account_health` snapshot."""
1735
  st = _rt(tenant_key, st)
1736
  view = summary(tenant_key, st=st)
1737
  with _reader(tenant_key, st) as cur:
@@ -1742,17 +1863,33 @@ def validate(tenant_key, *, st=None, **period):
1742
  date = cur.execute(f'SELECT MAX(CAST("_snapshot_date" AS VARCHAR)) FROM {AH_TABLE}').fetchone()[0] \
1743
  if "_snapshot_date" in cols else None
1744
  wanted = [("rating", "account_health_rating_ahr_score", 1.0)] + \
1745
- [(p, f"{p}_rate", None) for p, _l, _d in RATE_METRICS]
 
1746
  for key, col, _s in wanted:
1747
  if col not in cols:
1748
  continue
1749
  where = ' WHERE CAST("_snapshot_date" AS VARCHAR) = ?' if date is not None else ""
1750
  order = ' ORDER BY "_report_row_number"' if "_report_row_number" in cols else ""
1751
- q = (f'SELECT CAST("marketplace_id" AS VARCHAR), FIRST(TRY_CAST("{col}" AS DOUBLE){order}) '
 
1752
  f'FILTER (WHERE "{col}" IS NOT NULL) '
1753
  f'FROM {AH_TABLE}{where} GROUP BY 1')
1754
  for market, value in cur.execute(q, [date] if date is not None else []).fetchall():
1755
  independent[(key, str(market or ""))] = value
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1756
  app = {}
1757
  for m in view["marketplaces"]:
1758
  app[("rating", m["marketplaceId"] or "")] = m["rating"]
@@ -1761,6 +1898,8 @@ def validate(tenant_key, *, st=None, **period):
1761
  # The shown percent ties back through its scale; a no orders row ties its raw rate (Amazon's 0).
1762
  raw = row["value"] / row.get("scale", 100.0) if row["value"] is not None else row.get("raw")
1763
  app[(row["key"], row["marketplaceId"] or "")] = raw
 
 
1764
  detail, app_total, ind_total = [], 0.0, 0.0
1765
  for key in sorted(set(app) | set(independent)):
1766
  a, b = app.get(key), independent.get(key)
@@ -1770,9 +1909,24 @@ def validate(tenant_key, *, st=None, **period):
1770
  detail.append({"metric": key[0], "marketplaceId": key[1] or None, "app": a, "independent": b, "ok": tied})
1771
  ok = bool(detail) and all(d["ok"] for d in detail) and str(view.get("snapshotDate")) == str(date)
1772
  groups = _validate_groups(tenant_key, st, view)
1773
- return {"ok": ok and reserves["ok"] and groups["ok"], "app_total": round(app_total, 9),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1774
  "independent_total": round(ind_total, 9), "detail": detail, "snapshotDate": date,
1775
- "message": None if detail else EMPTY, "reserves": reserves, "groups": groups}
1776
 
1777
 
1778
  def _validate_groups(tenant_key, st, view):
 
24
  import json
25
  import math
26
  from pathlib import Path
27
+ from urllib.parse import urlencode
28
 
29
  APP_LABEL = "Account health"
30
  SOURCE_KIND = "account_health"
 
56
  COMPLAINT_METRICS = ("received_intellectual_property_complaints", "suspected_intellectual_property_violations",
57
  "product_authenticity_customer_complaints")
58
  SOURCE = "ut_amzn_account_health"
59
+ LISTING_SOURCE = "ut_amzn_listing_items"
60
+ CATEGORY_SOURCE_NOTE = (
61
+ "Amazon's account health report provides category totals, targets and reporting windows, but no violation records. "
62
+ "Related listings show listing issues. Amazon does not link them to this category."
63
+ )
64
  NOTIFICATION_KIND = "account_health_new_items"
65
  _sync_delivery = None
66
 
 
469
  return start, size
470
 
471
 
472
+ def summary_page(tenant_key, *, search="", offset=0, limit=PAGE_SIZE, metric=None, category=None, st=None):
473
  """Read one metric page or one exact metric without loading the complete snapshot."""
474
  st = _rt(tenant_key, st)
475
  distance = settings(tenant_key, st=st)["alertDistancePct"]
 
501
  "ELSE CASE WHEN CONTAINS(UPPER(condition), 'EQUAL') OR target = 0 THEN 'At most' ELSE 'Below' END END) "
502
  "|| ' ' || CASE WHEN unit = 'percent' THEN printf('%.2f%%', target) ELSE CAST(target AS VARCHAR) END END, "
503
  "condition, window_from, window_to)), ?)", [str(search or "").strip().lower()])
504
+ if category is not None:
505
+ where, filters = f"({where}) AND key = ?", [*filters, str(category)]
506
  folded = ", ".join(f"'{s}'" for s in FOLDED)
507
  total, folded_count = cur.execute(
508
  sql + f" SELECT COUNT(*), COUNT(*) FILTER (WHERE status IN ({folded})) FROM metrics WHERE {where}",
 
538
  "cursor": str(start + size) if start + size < out["metricTotal"] else None}
539
 
540
 
541
+ def _listing_array(value):
542
+ """Decode a connector JSON array without inventing missing entries."""
543
+ if isinstance(value, str):
544
+ try:
545
+ value = json.loads(value)
546
+ except (TypeError, ValueError):
547
+ return None
548
+ return value if isinstance(value, list) else None
549
+
550
+
551
+ def _related_listings(tenant_key, st, user, market):
552
+ """Read all permitted listing issue pages through the listing source's own wall."""
553
+ from core import perm_scope
554
+ st = _rt(tenant_key, st)
555
+ if user is None:
556
+ return [], "Related listings need access to the listing source."
557
+ related, cursor, incomplete = [], None, False
558
+ try:
559
+ while True:
560
+ page = perm_scope.scoped_table_page(user, LISTING_SOURCE, st=st, cursor=cursor, limit=1024)
561
+ if "issues" not in {field.get("key") for field in page["fields"]}:
562
+ return [], "Listing issues are unavailable with your current source access."
563
+ for row in page["rows"]:
564
+ if market and row.get("marketplace_id") and str(row["marketplace_id"]) != market:
565
+ continue
566
+ issues = _listing_array(row.get("issues"))
567
+ if issues is None:
568
+ incomplete = True
569
+ continue
570
+ summaries = _listing_array(row.get("summaries")) or []
571
+ summaries = [item for item in summaries if isinstance(item, dict)]
572
+ matching = [item for item in summaries if not market or item.get("marketplaceId") == market]
573
+ if market and summaries and not matching:
574
+ continue
575
+ asin = row.get("asin") or next((item.get("asin") for item in matching if item.get("asin")), None)
576
+ for issue in issues:
577
+ if not isinstance(issue, dict) or not (issue.get("message") or issue.get("code")):
578
+ incomplete = True
579
+ continue
580
+ related.append({"asin": asin, "sku": row.get("sku"),
581
+ "issue": issue.get("message") or issue["code"],
582
+ "severity": issue.get("severity")})
583
+ after = page["nextCursor"]
584
+ if after is None:
585
+ break
586
+ if cursor is not None and int(after) <= int(cursor):
587
+ raise RuntimeError("The listing source cursor did not advance.")
588
+ cursor = after
589
+ except perm_scope.Denied:
590
+ return [], "Related listings are unavailable with your current source access."
591
+ except (perm_scope.UnknownTable, perm_scope.Unresolvable):
592
+ return [], "Related listing data is unavailable."
593
+ return related, "Some listing issues are missing or unreadable." if incomplete else None
594
+
595
+
596
+ def category_detail(tenant_key, key, *, user=None, allowed=None, st=None):
597
+ """Serve one permitted category and separate listing evidence without claiming violation records."""
598
+ st = _rt(tenant_key, st)
599
+ category, _sep, _market = str(key).partition("@")
600
+ labels = dict(COUNT_METRICS)
601
+ if category not in labels:
602
+ raise KeyError(key)
603
+ kept, offset, total = [], 0, 0
604
+ while True:
605
+ page = summary_page(tenant_key, category=category, metric=key if _sep else None,
606
+ offset=offset, limit=PAGE_SIZE, st=st)
607
+ total = page["metricTotal"]
608
+ kept.extend(row for row in page["metrics"] if allowed is None or allowed(row, page["snapshotDate"]))
609
+ if len(kept) > 1:
610
+ raise ValueError("Choose a marketplace with category@marketplace.")
611
+ if page["cursor"] is None:
612
+ break
613
+ offset = int(page["cursor"])
614
+ if not kept and (total or allowed is not None or _sep):
615
+ raise KeyError(key)
616
+ row = kept[0] if kept else {}
617
+ note = CATEGORY_SOURCE_NOTE
618
+ if not row:
619
+ note += " This category is missing from the saved report."
620
+ if row and row.get("value") is None:
621
+ note += " The category count is missing."
622
+ if row and row.get("target") is None:
623
+ note += " Amazon did not provide a target."
624
+ rule = str(row.get("targetCondition") or "").upper()
625
+ known_rule = "LESS" in rule or "GREATER" in rule
626
+ if row.get("target") is not None and not known_rule:
627
+ note += " The target rule is missing or unsupported."
628
+ if row and (row.get("windowFrom") is None or row.get("windowTo") is None):
629
+ note += " The reporting window is missing or incomplete."
630
+ related, listing_note = _related_listings(tenant_key, st, user, row.get("marketplaceId")) if row else ([], None)
631
+ if listing_note:
632
+ note += " " + listing_note
633
+ query = urlencode({"marketplaceID": row["marketplaceId"]}) if row.get("marketplaceId") else ""
634
+ status = row.get("status", "No data")
635
+ if row.get("value") is not None and row.get("target") is not None and not known_rule:
636
+ status = "No target"
637
+ return {"key": category, "label": labels[category], "count": row.get("value"),
638
+ "status": status, "target": row.get("condition") if known_rule else None,
639
+ "window": {"from": row.get("windowFrom"), "to": row.get("windowTo")},
640
+ "recordList": False, "sourceNote": note,
641
+ "sellerCentralUrl": "https://sellercentral.amazon.com/performance/dashboard" + ("?" + query if query else ""),
642
+ "related": related}
643
+
644
+
645
  # --------------------------------------------------------------------------- settings and owner
646
 
647
  #: Accounts that sign in for a process, never a person who can own an alert (same list as amazon_profit).
 
1031
  return f"{row['label']} is {'at risk' if row['status'] == 'At risk' else 'near target'}"
1032
 
1033
 
1034
+ def _alert_source(tenant_key, st, row, snapshot_date, *, extra_fields=()):
1035
  """Bind an alert to the actual mirror row supplying its metric value."""
1036
  from harness.amazon_schema import IDENTITY_PID
1037
  column = row["key"] + ("_defects_count" if row["key"] in dict(COUNT_METRICS) else "_rate")
 
1042
  return {"source": None, "fields": fields}
1043
  fields += [name for name in (row["key"] + "_target_value", row["key"] + "_target_condition")
1044
  if name in cols]
1045
+ fields += [name for name in extra_fields if name in cols and name not in fields]
1046
  order = ' ORDER BY "_report_row_number"' if "_report_row_number" in cols else ""
1047
  row_ids = []
1048
  for field in (name for name in fields if name not in {"_snapshot_date", "marketplace_id"}):
 
1058
  "fields": fields}
1059
 
1060
 
1061
+ def category_source(tenant_key, st, row, snapshot_date):
1062
+ """Bind every category detail field to the report rows that supplied it."""
1063
+ key = row["key"]
1064
+ return _alert_source(tenant_key, st, row, snapshot_date, extra_fields=(
1065
+ key + "_status", key + "_reporting_date_range_reporting_date_from",
1066
+ key + "_reporting_date_range_reporting_date_to"))
1067
+
1068
+
1069
  def raise_alerts(tenant_key, view=None, *, st=None, now=None):
1070
  """Raise one owned alert per metric that is near or past its target, never a second while one is open."""
1071
  st = _rt(tenant_key, st)
 
1852
  return {"ok": bool(detail) and all(check["ok"] for check in detail), "detail": detail}
1853
 
1854
  def validate(tenant_key, *, st=None, **period):
1855
+ """Tie the rating, rates and category detail counts to the raw account health snapshot."""
1856
  st = _rt(tenant_key, st)
1857
  view = summary(tenant_key, st=st)
1858
  with _reader(tenant_key, st) as cur:
 
1863
  date = cur.execute(f'SELECT MAX(CAST("_snapshot_date" AS VARCHAR)) FROM {AH_TABLE}').fetchone()[0] \
1864
  if "_snapshot_date" in cols else None
1865
  wanted = [("rating", "account_health_rating_ahr_score", 1.0)] + \
1866
+ [(p, f"{p}_rate", None) for p, _l, _d in RATE_METRICS] + \
1867
+ [(p, f"{p}_defects_count", 1.0) for p, _l in COUNT_METRICS] + [(WARNINGS[0], WARNINGS[2], 1.0)]
1868
  for key, col, _s in wanted:
1869
  if col not in cols:
1870
  continue
1871
  where = ' WHERE CAST("_snapshot_date" AS VARCHAR) = ?' if date is not None else ""
1872
  order = ' ORDER BY "_report_row_number"' if "_report_row_number" in cols else ""
1873
+ q = (f'SELECT CAST("marketplace_id" AS VARCHAR), '
1874
+ f'FIRST(TRY_CAST(REPLACE(TRIM(CAST("{col}" AS VARCHAR)), \',\', \'\') AS DOUBLE){order}) '
1875
  f'FILTER (WHERE "{col}" IS NOT NULL) '
1876
  f'FROM {AH_TABLE}{where} GROUP BY 1')
1877
  for market, value in cur.execute(q, [date] if date is not None else []).fetchall():
1878
  independent[(key, str(market or ""))] = value
1879
+ inbound_cols = _columns(cur, INBOUND_TABLE)
1880
+ if inbound_cols:
1881
+ column = next((name for name in ("issue_reported_date", "_period_start") if name in inbound_cols), None)
1882
+ where, args = "", []
1883
+ if column:
1884
+ try:
1885
+ end = dt.date.fromisoformat(str(date)[:10])
1886
+ except (TypeError, ValueError):
1887
+ end = _now().date()
1888
+ where = (f' WHERE TRY_CAST(SUBSTR(CAST("{column}" AS VARCHAR), 1, 10) AS DATE) '
1889
+ f'> ? AND TRY_CAST(SUBSTR(CAST("{column}" AS VARCHAR), 1, 10) AS DATE) <= ?')
1890
+ args = [end - dt.timedelta(days=INBOUND_DAYS), end]
1891
+ independent[(INBOUND[0], "")] = cur.execute(
1892
+ f"SELECT COUNT(*) FROM {INBOUND_TABLE}{where}", args).fetchone()[0]
1893
  app = {}
1894
  for m in view["marketplaces"]:
1895
  app[("rating", m["marketplaceId"] or "")] = m["rating"]
 
1898
  # The shown percent ties back through its scale; a no orders row ties its raw rate (Amazon's 0).
1899
  raw = row["value"] / row.get("scale", 100.0) if row["value"] is not None else row.get("raw")
1900
  app[(row["key"], row["marketplaceId"] or "")] = raw
1901
+ else:
1902
+ app[(row["key"], row["marketplaceId"] or "")] = row["value"]
1903
  detail, app_total, ind_total = [], 0.0, 0.0
1904
  for key in sorted(set(app) | set(independent)):
1905
  a, b = app.get(key), independent.get(key)
 
1909
  detail.append({"metric": key[0], "marketplaceId": key[1] or None, "app": a, "independent": b, "ok": tied})
1910
  ok = bool(detail) and all(d["ok"] for d in detail) and str(view.get("snapshotDate")) == str(date)
1911
  groups = _validate_groups(tenant_key, st, view)
1912
+ categories = []
1913
+ for key, market in sorted(set(app) | set(independent)):
1914
+ if key not in dict(COUNT_METRICS):
1915
+ continue
1916
+ try:
1917
+ shown = category_detail(tenant_key, key + ("@" + market if market else ""), st=st)["count"]
1918
+ source = independent.get((key, market))
1919
+ tied = (shown is None and source is None) or (shown is not None and source is not None
1920
+ and abs(shown - source) <= 1e-7)
1921
+ except KeyError:
1922
+ shown, source = None, independent.get((key, market))
1923
+ tied = (key, market) not in app and source is None
1924
+ categories.append({"metric": key, "marketplaceId": market or None,
1925
+ "app": shown, "independent": source, "ok": tied})
1926
+ category_check = {"ok": all(row["ok"] for row in categories), "detail": categories}
1927
+ return {"ok": ok and reserves["ok"] and groups["ok"] and category_check["ok"], "app_total": round(app_total, 9),
1928
  "independent_total": round(ind_total, 9), "detail": detail, "snapshotDate": date,
1929
+ "message": None if detail else EMPTY, "reserves": reserves, "groups": groups, "categories": category_check}
1930
 
1931
 
1932
  def _validate_groups(tenant_key, st, view):
platform/modules/amazon_profit.py CHANGED
@@ -10,9 +10,9 @@ survive every period switch.
10
  `ut_amazon_asin_profit` and `ut_amazon_parent_profit` roll the SKU rows up on every refresh.
11
  Projects retain their own verified period totals and Impact in the tenant projects store.
12
 
13
- `validate()` ties a month's net proceeds to Amazon settlements within 2% over the days its issued
14
- settlements cover, excluding loan repayments and withheld tax, and proves a negative control that
15
- drops fees goes red. A refresh runs it for the last complete month and writes no figures when it
16
  fails. A passing refresh stores the verified range and the day the rest is due to settle.
17
 
18
  Owned items (R10, R31): a refresh opens one item on a Losing or Thin SKU with no status, for the
@@ -615,11 +615,16 @@ def reported_net_proceeds(con, start, end):
615
  def economics_currency(con):
616
  if "currency_code" not in _columns(con, ECONOMICS):
617
  return None
618
- return con.execute(f"SELECT any_value(currency_code) FROM {ECONOMICS}").fetchone()[0]
 
 
 
 
 
619
 
620
 
621
  def settlements_total(con, start, end, currency=None):
622
- """Settlements posted in the window, excluding loan repayment and withheld tax.
623
 
624
  `currency` keeps settlements whose header row names it: Amazon.ca and Amazon.com.mx settle
625
  in their own currency, outside the economics marketplace. Their reports write DD.MM.YYYY.
@@ -629,18 +634,188 @@ def settlements_total(con, start, end, currency=None):
629
  stream = ("JOIN (SELECT settlement_id FROM {t} WHERE currency IS NOT NULL GROUP BY settlement_id "
630
  "HAVING any_value(currency) = ?) h USING (settlement_id)").format(t=SETTLEMENTS) if scoped else ""
631
  total, rows, undated = con.execute(
632
- "SELECT COALESCE(SUM(amount) FILTER (WHERE d BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)), 0), "
 
633
  "count(*) FILTER (WHERE d BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)), "
634
  "count(*) FILTER (WHERE d IS NULL AND posted_date IS NOT NULL) "
635
  "FROM (SELECT amount, posted_date, COALESCE(TRY_CAST(left(posted_date, 10) AS DATE), "
636
  "CAST(try_strptime(left(posted_date, 10), '%d.%m.%Y') AS DATE)) AS d "
637
- f"FROM {SETTLEMENTS} s {stream} WHERE COALESCE(transaction_type, '') <> ? "
638
- "AND COALESCE(amount_type, '') <> ?)",
639
- [start, end, start, end] + ([currency] if scoped else []) + [LOAN_REPAYMENT, WITHHELD_TAX]).fetchone()
640
  return {"total": float(total), "rows": int(rows), "undated": int(undated),
641
  "currency": currency if scoped else None}
642
 
643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644
  _COVERAGE_REQUIRED = ("settlement_id", "total_amount", "settlement_start_date", "settlement_end_date")
645
 
646
 
@@ -1988,6 +2163,8 @@ def refusal(checks, month):
1988
  )
1989
  for index, check in enumerate(checks):
1990
  if not check.get("ok"):
 
 
1991
  if index < len(sentences):
1992
  return sentences[index](check)
1993
  return f"Not verified: the Amazon figures do not tie in {label}."
@@ -2685,7 +2862,9 @@ def _mirror_has_economics(con):
2685
  return False
2686
 
2687
 
2688
- def snapshot_cursor(tenant=TENANT_ZERO, tables=(ECONOMICS, SETTLEMENTS, LISTINGS)):
 
 
2689
  """An in-memory DuckDB over the stored mirror snapshot. Downloads only; writes nothing back."""
2690
  key = (tenant, tuple(tables))
2691
  if key in _SNAPSHOT:
@@ -2820,7 +2999,7 @@ def typed_cogs_checks(con, computed, typed, usage=None, bounds=None):
2820
 
2821
  def validate(pre=None, month=None, con=None, *, cost_usage=None, cost_history=None, typed_rows=None,
2822
  typed_values=None, typed_period=None):
2823
- """validate.py convention: net proceeds tie to settlements within 2% over the month's settled days.
2824
  Receipt COGS ties to Odoo; typed COGS (`typed_rows`) is listed apart and ties to its stored values."""
2825
  month = month or last_complete_month()
2826
  bounds = period_bounds(month)
@@ -2829,25 +3008,35 @@ def validate(pre=None, month=None, con=None, *, cost_usage=None, cost_history=No
2829
  if con is None:
2830
  con, source = validation_cursor()
2831
  checks = []
2832
- currency = economics_currency(con)
2833
- span = settlement_coverage(con, start, end, currency)
2834
- # Both sides tie the same covered days: economics by start_date, settlement lines by posted_date.
 
 
 
 
 
2835
  lo, hi = span["start"] or start, span["end"] or end
2836
  proceeds = net_proceeds_total(con, lo, hi)
2837
- settled = settlements_total(con, lo, hi, currency)
2838
  if span["start"] is None:
2839
- settled = dict(settled, total=0.0, rows=0)
2840
- ok, gap = _tie(proceeds, settled["total"])
 
 
2841
  covered = {k: span[k] for k in ("start", "end", "label", "partial", "clear", "sentence")}
2842
  checks.append({"check": f"amazon_profit {month}: net proceeds tie to settlements within 2% over "
2843
  f"{span['label'] or 'no settled days'} ({gap:.2%} apart, {source})",
2844
- "ok": ok, "a": round(proceeds, 2), "b": round(settled["total"], 2),
2845
- "gap": round(gap, 4), "covered": covered})
 
 
2846
  dropped = net_proceeds_total(con, lo, hi, drop_fees=True)
2847
- control_ok, control_gap = _tie(dropped, settled["total"])
2848
  checks.append({"check": f"amazon_profit {month}: NC net proceeds without fees miss settlements "
2849
  f"({control_gap:.2%} apart)",
2850
- "ok": not control_ok, "a": round(dropped, 2), "b": round(settled["total"], 2),
 
2851
  "gap": round(control_gap, 4)})
2852
  checks.append({"check": f"amazon_profit {month}: every {settled['currency'] or 'settlement'} "
2853
  "settlement row carries a readable date",
 
10
  `ut_amazon_asin_profit` and `ut_amazon_parent_profit` roll the SKU rows up on every refresh.
11
  Projects retain their own verified period totals and Impact in the tenant projects store.
12
 
13
+ `validate()` ties each net proceeds component to independent Amazon amounts within 2% over settled
14
+ days, with order charges on purchase dates and monthly storage in its service month.
15
+ A refresh runs it for the last complete month and writes no figures when it
16
  fails. A passing refresh stores the verified range and the day the rest is due to settle.
17
 
18
  Owned items (R10, R31): a refresh opens one item on a Losing or Thin SKU with no status, for the
 
615
  def economics_currency(con):
616
  if "currency_code" not in _columns(con, ECONOMICS):
617
  return None
618
+ currency, count, missing = con.execute(
619
+ f"SELECT max(currency_code), count(DISTINCT currency_code), "
620
+ f"count(*) FILTER (WHERE NULLIF(trim(currency_code), '') IS NULL) FROM {ECONOMICS}").fetchone()
621
+ if count > 1 or missing:
622
+ raise SourceUnavailable("Amazon economics needs one known currency.")
623
+ return currency
624
 
625
 
626
  def settlements_total(con, start, end, currency=None):
627
+ """Sum posted settlements with both collected and withheld sales tax included.
628
 
629
  `currency` keeps settlements whose header row names it: Amazon.ca and Amazon.com.mx settle
630
  in their own currency, outside the economics marketplace. Their reports write DD.MM.YYYY.
 
634
  stream = ("JOIN (SELECT settlement_id FROM {t} WHERE currency IS NOT NULL GROUP BY settlement_id "
635
  "HAVING any_value(currency) = ?) h USING (settlement_id)").format(t=SETTLEMENTS) if scoped else ""
636
  total, rows, undated = con.execute(
637
+ "SELECT COALESCE(SUM(TRY_CAST(amount AS DECIMAL(38, 2))) "
638
+ "FILTER (WHERE d BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)), 0), "
639
  "count(*) FILTER (WHERE d BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)), "
640
  "count(*) FILTER (WHERE d IS NULL AND posted_date IS NOT NULL) "
641
  "FROM (SELECT amount, posted_date, COALESCE(TRY_CAST(left(posted_date, 10) AS DATE), "
642
  "CAST(try_strptime(left(posted_date, 10), '%d.%m.%Y') AS DATE)) AS d "
643
+ f"FROM {SETTLEMENTS} s {stream} WHERE COALESCE(transaction_type, '') <> ?)",
644
+ [start, end, start, end] + ([currency] if scoped else []) + [LOAN_REPAYMENT]).fetchone()
 
645
  return {"total": float(total), "rows": int(rows), "undated": int(undated),
646
  "currency": currency if scoped else None}
647
 
648
 
649
+ def settlement_components(con, start, end, currency=None):
650
+ """Tie purchase-date order charges, service-month storage and daily ads to independent sources."""
651
+ month = dt.date.fromisoformat(start).strftime("%B %Y")
652
+ parts, problems = [], []
653
+
654
+ def problem(component, reason):
655
+ problems.append(f"Not verified: {component} in {month}: {reason}.")
656
+
657
+ def tie(component, ours, source, *, reason=None):
658
+ ours = float(ours)
659
+ source = None if source is None else float(source)
660
+ ok, gap = _tie(ours, source) if source is not None else (False, math.inf)
661
+ basis = {"product sales": "order purchase date", "order fees": "order purchase date",
662
+ "monthly storage": "storage service month", "ads": "daily ad report date"}
663
+ parts.append({"component": component, "month": start[:7], "ok": ok and not reason,
664
+ "a": ours, "b": source, "gap": gap, "tolerance": SETTLEMENT_TOLERANCE,
665
+ "dateBasis": basis.get(component, "order purchase date when present, otherwise posted date"),
666
+ "sourceTables": ([ECONOMICS] + [f"amzn_ads_{kind}_advertised_products" for kind in ("sp", "sb", "sd")]
667
+ if component == "ads" else [ECONOMICS, SETTLEMENTS, "amzn_orders"])})
668
+ if reason:
669
+ problem(component, reason)
670
+ elif not ok:
671
+ problem(component, f"independent Amazon amounts differ by {gap:.1%}")
672
+
673
+ sc = _columns(con, SETTLEMENTS)
674
+ ec = _columns(con, ECONOMICS)
675
+ required = set(_SETTLEMENT_REQUIRED) | {"settlement_id", "currency", "order_id", "amount_description"}
676
+ if not required <= sc:
677
+ problem("settlement components", "missing source columns " + ", ".join(sorted(required - sc)))
678
+ return {"total": None, "parts": parts, "reason": problems[0], "rows": 0}
679
+ if currency is None:
680
+ problem("settlement components", "economics currency unavailable")
681
+ return {"total": None, "parts": parts, "reason": problems[0], "rows": 0}
682
+ if not {"amazon_order_id", "purchase_date"} <= _columns(con, "amzn_orders"):
683
+ problem("order charges", "missing amzn_orders purchase dates")
684
+ return {"total": None, "parts": parts, "reason": problems[0], "rows": 0}
685
+ unscoped = con.execute(f"""SELECT count(*) FROM {SETTLEMENTS} s
686
+ LEFT JOIN (SELECT settlement_id FROM {SETTLEMENTS} GROUP BY settlement_id
687
+ HAVING count(DISTINCT currency) = 1) h USING (settlement_id)
688
+ WHERE h.settlement_id IS NULL AND (s.amount IS NOT NULL OR NULLIF(s.amount_type, '') IS NOT NULL)
689
+ AND (CAST({_moment_sql('posted_date')} AS DATE) BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)
690
+ OR {_moment_sql('posted_date')} IS NULL)""", [start, end]).fetchone()[0]
691
+ if unscoped:
692
+ problem("settlement currency", "missing or conflicting settlement header currency")
693
+ # Aggregate in SQL before reading: duplicate order lines must not multiply settlement amounts.
694
+ cte = f"""WITH orders AS (
695
+ SELECT amazon_order_id, min(CAST({_moment_sql('purchase_date')} AS DATE)) AS bought,
696
+ count(DISTINCT CAST({_moment_sql('purchase_date')} AS DATE)) AS dates,
697
+ count(*) FILTER (WHERE {_moment_sql('purchase_date')} IS NULL) AS bad_dates
698
+ FROM amzn_orders GROUP BY amazon_order_id),
699
+ streams AS (SELECT settlement_id FROM {SETTLEMENTS} GROUP BY settlement_id
700
+ HAVING count(DISTINCT currency) = 1 AND max(currency) = ?),
701
+ lines AS (SELECT s.*, TRY_CAST(amount AS DECIMAL(38, 2)) AS currency_amount,
702
+ CAST({_moment_sql('s.posted_date')} AS DATE) AS posted,
703
+ o.bought, o.dates, o.bad_dates,
704
+ lower(regexp_replace(COALESCE(amount_description, ''), '[^a-zA-Z0-9]', '', 'g')) AS description,
705
+ lower(regexp_replace(COALESCE(amount_type, ''), '[^a-zA-Z0-9]', '', 'g')) AS kind,
706
+ lower(regexp_replace(COALESCE(transaction_type, ''), '[^a-zA-Z0-9]', '', 'g')) AS transaction
707
+ FROM {SETTLEMENTS} s JOIN streams USING (settlement_id)
708
+ LEFT JOIN orders o ON trim(s.order_id) = o.amazon_order_id
709
+ WHERE (amount IS NOT NULL OR NULLIF(amount_type, '') IS NOT NULL)
710
+ AND COALESCE(transaction_type, '') <> ?),
711
+ classified AS (SELECT *, CASE
712
+ WHEN concat(kind, description, transaction) LIKE '%inbounddefect%' THEN 'Inbound Defect Fee'
713
+ WHEN concat(kind, description, transaction) LIKE '%mcf%' THEN 'MCF credits'
714
+ WHEN kind = 'itemwithheldtax' OR description IN ('tax', 'shippingtax', 'giftwraptax')
715
+ OR description LIKE 'marketplacefacilitatortax%' THEN 'tax'
716
+ WHEN (kind = 'itemprice' AND (description IN ('shipping', 'giftwrap')
717
+ OR description LIKE '%promotion%')) OR kind IN ('promotion', 'shipping', 'shippingfees')
718
+ THEN 'shipping and promotions'
719
+ WHEN 'costofadvertising' IN (kind, description, transaction)
720
+ OR description IN ('advertising', 'advertisingfee') THEN 'ads billing'
721
+ WHEN description IN ('storagefee', 'fbamonthlystoragefee', 'monthlystoragefee')
722
+ OR 'fbainventorystoragefee' IN (kind, description, transaction) THEN 'monthly storage'
723
+ WHEN concat(kind, description, transaction) LIKE '%reimbursement%' THEN 'reimbursements'
724
+ WHEN description IN ('subscription', 'subscriptionfee', 'fbaremovalfee', 'fbadisposalfee',
725
+ 'fbainboundtransportationfee', 'couponredemptionfee', 'dealfee')
726
+ OR kind IN ('dealfee', 'lightningdealfee', 'dealparticipationfee', 'dealperformancefee',
727
+ 'fbacustomerreturnfee', 'fbacustomerreturnsfee', 'fbacustomerreturnperunitfee',
728
+ 'fbacustomerreturnprocessingfee', 'fbainboundplacementservicefee')
729
+ OR description IN ('lightningdealfee', 'dealparticipationfee', 'dealperformancefee',
730
+ 'fbacustomerreturnfee', 'fbacustomerreturnsfee', 'fbacustomerreturnperunitfee',
731
+ 'fbacustomerreturnprocessingfee', 'fbainboundplacementservicefee')
732
+ OR transaction IN ('dealfee', 'lightningdealfee', 'dealparticipationfee', 'dealperformancefee',
733
+ 'fbacustomerreturnfee', 'fbacustomerreturnsfee', 'fbacustomerreturnperunitfee',
734
+ 'fbacustomerreturnprocessingfee', 'fbainboundplacementservicefee') THEN 'account fees'
735
+ WHEN NULLIF(trim(order_id), '') IS NOT NULL AND kind = 'itemprice'
736
+ AND description = 'principal' THEN 'product sales'
737
+ WHEN NULLIF(trim(order_id), '') IS NOT NULL AND kind IN ('itemfee', 'itemfees') THEN 'order fees'
738
+ ELSE 'unmatched component' END AS component FROM lines),
739
+ dated AS (SELECT *, CASE
740
+ WHEN component = 'monthly storage' THEN CAST(date_trunc('month', posted) - INTERVAL '1 month' AS DATE)
741
+ WHEN NULLIF(trim(order_id), '') IS NOT NULL THEN bought
742
+ ELSE posted END AS basis FROM classified)
743
+ """
744
+ groups = con.execute(cte + """SELECT component, COALESCE(SUM(currency_amount), 0), count(*),
745
+ count(*) FILTER (WHERE currency_amount IS NULL OR posted IS NULL),
746
+ count(*) FILTER (WHERE NULLIF(trim(order_id), '') IS NOT NULL
747
+ AND (bought IS NULL OR dates <> 1 OR bad_dates <> 0))
748
+ FROM dated WHERE basis BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)
749
+ OR ((basis IS NULL OR dates <> 1 OR bad_dates <> 0)
750
+ AND (posted BETWEEN CAST(? AS DATE) AND CAST(? AS DATE) OR posted IS NULL))
751
+ GROUP BY component ORDER BY component""",
752
+ [currency, LOAN_REPAYMENT, start, end, start, end]).fetchall()
753
+ amounts = {name: total for name, total, *_ in groups}
754
+ if not groups:
755
+ problem("settlement components", "no settlement source rows on the purchase or service dates")
756
+ for name, _total, _n, bad, unmatched in groups:
757
+ if bad or unmatched:
758
+ problem(name, "unmatched purchase date, unreadable posting date or unavailable amount")
759
+ if name in ("unmatched component", "Inbound Defect Fee", "MCF credits"):
760
+ problem(name, "settlement category has no economics mapping")
761
+ fee_columns = sorted(c for c in ec if c.startswith("fee_") and c.endswith("_amount") and "_charge_" not in c)
762
+ storage_columns = [c for c in fee_columns if "storage" in c]
763
+ reimbursement_columns = [c for c in fee_columns if "reimbursement" in c]
764
+ account_columns = [c for c in fee_columns if any(name in c for name in
765
+ ("subscription", "removal", "disposal", "inbound_transportation", "inbound_placement",
766
+ "coupon", "deal_", "customer_return", "return_processing", "returns_processing"))]
767
+ # Storage fee columns are accruals in economics; only monthly settlement charges use the prior month.
768
+ sums = ["sales_net_product_sales_amount", "fees_total_amount", *ads_columns(ec),
769
+ *storage_columns, *reimbursement_columns, *account_columns]
770
+ observed = con.execute("SELECT " + ", ".join(f'COALESCE(SUM(TRY_CAST("{c}" AS DECIMAL(38, 2))), 0)' for c in sums)
771
+ + ", count(*) FILTER (WHERE "
772
+ + " OR ".join(f'("{c}" IS NOT NULL AND TRY_CAST("{c}" AS DECIMAL(38, 2)) IS NULL)'
773
+ for c in sums) + ")"
774
+ + f" FROM {ECONOMICS} WHERE left(start_date, 10) BETWEEN ? AND ?",
775
+ [start, end]).fetchone()
776
+ if observed[-1]:
777
+ problem("settlement components", "unavailable economics amount")
778
+ values = dict(zip(sums, observed[:-1]))
779
+ sales, fees = values[sums[0]], values[sums[1]]
780
+ storage = sum(values[c] for c in storage_columns)
781
+ reimbursements = sum(values[c] for c in reimbursement_columns)
782
+ account_fees = sum(values[c] for c in account_columns)
783
+ ads = sum(values[c] for c in ads_columns(ec))
784
+ tie("product sales", sales, amounts.get("product sales", 0.0))
785
+ tie("order fees", fees - storage - reimbursements - account_fees, -amounts.get("order fees", 0.0))
786
+ tie("monthly storage", storage, -amounts.get("monthly storage", 0.0))
787
+ tie("reimbursements", reimbursements, -amounts.get("reimbursements", 0.0))
788
+ tie("account fees", account_fees, -amounts.get("account fees", 0.0))
789
+ tie("sales tax", 0.0, amounts.get("tax", 0.0))
790
+ # Shipping and promotions lie outside net product sales; ad invoices use a separate billing period.
791
+ if ads or "ads billing" in amounts:
792
+ ad_total = 0
793
+ for kind in ("sp", "sb", "sd"):
794
+ table = f"amzn_ads_{kind}_advertised_products"
795
+ if not {"date", "cost"} <= _columns(con, table):
796
+ problem("ads", f"missing source {table}.cost")
797
+ ad_total = None
798
+ break
799
+ total, count, present = con.execute(
800
+ f"SELECT SUM(TRY_CAST(cost AS DECIMAL(38, 2))), count(*), "
801
+ f"count(TRY_CAST(cost AS DECIMAL(38, 2))) FROM {table} "
802
+ "WHERE left(CAST(date AS VARCHAR), 10) BETWEEN ? AND ?", [start, end]).fetchone()
803
+ if not count or count != present or total is None or not math.isfinite(float(total)):
804
+ problem("ads", f"unavailable daily amounts in {table}")
805
+ ad_total = None
806
+ break
807
+ ad_total += total
808
+ tie("ads", ads, ad_total)
809
+ else:
810
+ ad_total = 0
811
+ target = (float(sum(amounts.get(name, 0) for name in
812
+ ("product sales", "order fees", "monthly storage", "reimbursements", "account fees")))
813
+ - float(ad_total)) if ad_total is not None else None
814
+ return {"total": target, "parts": parts, "reason": problems[0] if problems else None,
815
+ "rows": sum(row[2] for row in groups),
816
+ "excluded": {k: float(amounts[k]) for k in ("shipping and promotions", "ads billing") if k in amounts}}
817
+
818
+
819
  _COVERAGE_REQUIRED = ("settlement_id", "total_amount", "settlement_start_date", "settlement_end_date")
820
 
821
 
 
2163
  )
2164
  for index, check in enumerate(checks):
2165
  if not check.get("ok"):
2166
+ if index == 0 and check.get("reason"):
2167
+ return check["reason"]
2168
  if index < len(sentences):
2169
  return sentences[index](check)
2170
  return f"Not verified: the Amazon figures do not tie in {label}."
 
2862
  return False
2863
 
2864
 
2865
+ def snapshot_cursor(tenant=TENANT_ZERO, tables=(ECONOMICS, SETTLEMENTS, LISTINGS, "amzn_orders",
2866
+ "amzn_ads_sp_advertised_products", "amzn_ads_sb_advertised_products",
2867
+ "amzn_ads_sd_advertised_products")):
2868
  """An in-memory DuckDB over the stored mirror snapshot. Downloads only; writes nothing back."""
2869
  key = (tenant, tuple(tables))
2870
  if key in _SNAPSHOT:
 
2999
 
3000
  def validate(pre=None, month=None, con=None, *, cost_usage=None, cost_history=None, typed_rows=None,
3001
  typed_values=None, typed_period=None):
3002
+ """Tie net proceeds components to independent Amazon sources within 2% over settled days.
3003
  Receipt COGS ties to Odoo; typed COGS (`typed_rows`) is listed apart and ties to its stored values."""
3004
  month = month or last_complete_month()
3005
  bounds = period_bounds(month)
 
3008
  if con is None:
3009
  con, source = validation_cursor()
3010
  checks = []
3011
+ try:
3012
+ _require(con, ECONOMICS, _ECONOMICS_REQUIRED)
3013
+ currency = economics_currency(con)
3014
+ span = settlement_coverage(con, start, end, currency)
3015
+ settled = settlements_total(con, start, end, currency)
3016
+ except SourceUnavailable as exc:
3017
+ label = dt.date.fromisoformat(start).strftime("%B %Y")
3018
+ raise SourceUnavailable(f"Not verified: settlement components in {label}: {exc}") from None
3019
  lo, hi = span["start"] or start, span["end"] or end
3020
  proceeds = net_proceeds_total(con, lo, hi)
3021
+ aligned = settlement_components(con, lo, hi, currency)
3022
  if span["start"] is None:
3023
+ aligned = dict(aligned, total=None, rows=0,
3024
+ reason=f"Not verified: settlement components in {bounds['label']}: no settled days.")
3025
+ target = aligned["total"]
3026
+ ok, gap = _tie(proceeds, target) if target is not None else (False, math.inf)
3027
  covered = {k: span[k] for k in ("start", "end", "label", "partial", "clear", "sentence")}
3028
  checks.append({"check": f"amazon_profit {month}: net proceeds tie to settlements within 2% over "
3029
  f"{span['label'] or 'no settled days'} ({gap:.2%} apart, {source})",
3030
+ "ok": ok and not aligned["reason"], "a": round(proceeds, 2),
3031
+ "b": None if target is None else round(target, 2), "reason": aligned["reason"],
3032
+ "components": aligned["parts"], "excluded": aligned.get("excluded", {}),
3033
+ "gap": round(gap, 4), "covered": covered, "tolerance": SETTLEMENT_TOLERANCE})
3034
  dropped = net_proceeds_total(con, lo, hi, drop_fees=True)
3035
+ control_ok, control_gap = _tie(dropped, target) if target is not None else (False, math.inf)
3036
  checks.append({"check": f"amazon_profit {month}: NC net proceeds without fees miss settlements "
3037
  f"({control_gap:.2%} apart)",
3038
+ "ok": not control_ok, "a": round(dropped, 2),
3039
+ "b": None if target is None else round(target, 2),
3040
  "gap": round(control_gap, 4)})
3041
  checks.append({"check": f"amazon_profit {month}: every {settled['currency'] or 'settlement'} "
3042
  "settlement row carries a readable date",
platform/modules/procurement.py CHANGED
@@ -20,6 +20,7 @@ READ-ONLY on Odoo; the team's Order Qty + master-data edits persist in the platf
20
  """
21
  import sys
22
  import json
 
23
  import math
24
  import calendar
25
  import datetime as dt
@@ -153,6 +154,22 @@ def _preorders(cutoff_iso):
153
  return qty, need
154
 
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  def recommendations(t=None):
157
  """Base per-SKU table (curated master data + live Odoo demand/stock/PO + alt-vendors). The UI
158
  overlays the team's vendor/country/cost edits on top; reorder methods + CBM/container are here."""
@@ -191,13 +208,13 @@ def recommendations(t=None):
191
  try:
192
  import modules.warehouse as warehouse
193
  fba_by_pid, fba_report = warehouse.fba_qty_by_product_id(
194
- [p['id'] for p in by_code.values()])
195
  except Exception as exc:
196
  fba_by_pid, fba_report = None, {
197
  'available': False, 'cause': 'the FBA warehouse read failed: ' + type(exc).__name__}
198
  rows = []
199
  for code, p in by_code.items():
200
- pid = p['id']
201
  meta = sup.get(code, {})
202
  lead = meta.get('lead') or 0
203
  tmpl = O.m2o_id(p.get('product_tmpl_id'))
@@ -211,7 +228,8 @@ def recommendations(t=None):
211
  on_hand = p.get('qty_available') or 0.0
212
  po = inpo.get(pid, 0.0)
213
  fba = (fba_by_pid or {}).get(pid)
214
- fba_known = isinstance(fba, (int, float)) and fba_report.get('available') is not False
 
215
  avail = on_hand - fba + po if fba_known else None
216
  days_cover = ((avail / avg_day) if avg_day > 0 else
217
  (None if avail <= 0 else 9999.0)) if avail is not None else None
@@ -245,8 +263,7 @@ def recommendations(t=None):
245
  })
246
  if not fba_known:
247
  rows[-1]['_coverage_report'] = {**fba_report, 'available': False,
248
- 'cause': fba_report.get('cause')
249
- or 'FBA stock unavailable for this product'}
250
  return rows
251
 
252
 
@@ -649,13 +666,13 @@ def validate_coverage(rows, t=None, oracle=None, demand=None):
649
  unavailable, wrong_days, wrong_buy = [], [], []
650
  for r in rows:
651
  pid = r.get('_product_id')
652
- known = (pid is not None and isinstance(r.get('_fba'), (int, float))
653
- and r.get('_coverage_report', {}).get('available') is not False)
654
  expected_days, expected_buy = None, None
655
- if not known:
656
  unavailable.append({'code': r['code'],
657
- 'cause': r.get('_coverage_report', {}).get('cause')
658
- or 'FBA stock unavailable for this product'})
659
  else:
660
  effective = stock['non_fba_by_pid'].get(pid, 0.0) + float(r.get('incoming_po') or 0.0)
661
  daily = demand.get(pid, 0.0) / window_days
 
20
  """
21
  import sys
22
  import json
23
+ import hashlib
24
  import math
25
  import calendar
26
  import datetime as dt
 
154
  return qty, need
155
 
156
 
157
+ def _fba_refusal(product_id, quantity, report):
158
+ """Classify unknown FBA stock without treating missing or invalid quantities as zero."""
159
+ if not product_id:
160
+ return {'cause_code': 'no_product_id', 'cause': 'no product id for this SKU'}
161
+ if report.get('available') is False:
162
+ cause_code = ('non_numeric_fba' if report.get('cause_code') == 'non_numeric_fba'
163
+ else 'report_unavailable')
164
+ return {'cause_code': cause_code,
165
+ 'cause': report.get('cause') or 'FBA stock report unavailable'}
166
+ if (not isinstance(quantity, (int, float)) or isinstance(quantity, bool)
167
+ or not math.isfinite(quantity)):
168
+ return {'cause_code': 'non_numeric_fba',
169
+ 'cause': 'FBA quantity is missing or is not a finite number for this product'}
170
+ return None
171
+
172
+
173
  def recommendations(t=None):
174
  """Base per-SKU table (curated master data + live Odoo demand/stock/PO + alt-vendors). The UI
175
  overlays the team's vendor/country/cost edits on top; reorder methods + CBM/container are here."""
 
208
  try:
209
  import modules.warehouse as warehouse
210
  fba_by_pid, fba_report = warehouse.fba_qty_by_product_id(
211
+ [p.get('id') for p in by_code.values()])
212
  except Exception as exc:
213
  fba_by_pid, fba_report = None, {
214
  'available': False, 'cause': 'the FBA warehouse read failed: ' + type(exc).__name__}
215
  rows = []
216
  for code, p in by_code.items():
217
+ pid = p.get('id')
218
  meta = sup.get(code, {})
219
  lead = meta.get('lead') or 0
220
  tmpl = O.m2o_id(p.get('product_tmpl_id'))
 
228
  on_hand = p.get('qty_available') or 0.0
229
  po = inpo.get(pid, 0.0)
230
  fba = (fba_by_pid or {}).get(pid)
231
+ fba_refusal = _fba_refusal(pid, fba, fba_report)
232
+ fba_known = fba_refusal is None
233
  avail = on_hand - fba + po if fba_known else None
234
  days_cover = ((avail / avg_day) if avg_day > 0 else
235
  (None if avail <= 0 else 9999.0)) if avail is not None else None
 
263
  })
264
  if not fba_known:
265
  rows[-1]['_coverage_report'] = {**fba_report, 'available': False,
266
+ **fba_refusal}
 
267
  return rows
268
 
269
 
 
666
  unavailable, wrong_days, wrong_buy = [], [], []
667
  for r in rows:
668
  pid = r.get('_product_id')
669
+ report = r.get('_coverage_report', {})
670
+ refusal = _fba_refusal(pid, r.get('_fba'), report)
671
  expected_days, expected_buy = None, None
672
+ if refusal is not None:
673
  unavailable.append({'code': r['code'],
674
+ 'sku_ref': hashlib.sha256(r['code'].encode('utf-8')).hexdigest(),
675
+ **refusal})
676
  else:
677
  effective = stock['non_fba_by_pid'].get(pid, 0.0) + float(r.get('incoming_po') or 0.0)
678
  daily = demand.get(pid, 0.0) / window_days
platform/modules/returns_refunds.py CHANGED
@@ -1987,6 +1987,33 @@ def _return_source(cur, table, date, quantity, sku="sku", reason="reason"):
1987
  + (f" FROM {table}" if cols else " WHERE false") + ") captured")
1988
 
1989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1990
  def _refund_relations(cur):
1991
  """Keep source identities, capture deduplication and ambiguous overlap inside SQL."""
1992
  sc, tc = (_columns(cur, table) for table in FINANCE_TABLES)
@@ -2046,24 +2073,20 @@ def _refund_relations(cur):
2046
  SELECT 'finance',account,settlement_id,event_id,item_id,order_id,sku,asin,day,NULL,money,currency,
2047
  conflict,event_id IS NULL OR position IS NULL OR NOT isfinite(position) OR position<0
2048
  OR position!=trunc(position) OR sku IS NULL AND asin IS NULL, true FROM finance_events),
2049
- refund_events AS (SELECT a.*,COALESCE(a.sku,a.asin,'{NO_SKU}') AS key,
2050
- EXISTS(SELECT 1 FROM refund_candidates b WHERE a.source!=b.source AND a.account=b.account
2051
- AND (a.order_id=b.order_id OR a.order_id IS NULL OR b.order_id IS NULL)
2052
- AND (a.sku=b.sku OR a.sku IS NULL OR b.sku IS NULL)
2053
- AND (a.settlement_id=b.settlement_id OR a.settlement_id IS NULL OR b.settlement_id IS NULL)) AS overlap
2054
- FROM refund_candidates a)"""
2055
 
2056
 
2057
- def _refund_totals_sql(first, last, group=False):
2058
  """Aggregate only event-grain refunds after source capture deduplication."""
2059
- return ("SELECT " + ("key," if group else "") + "COALESCE(sum(units),0) AS refunded,"
2060
  "sum(money) AS refund_cost,min(currency) AS refund_currency,count(DISTINCT currency) AS currencies,"
2061
  "count(*) FILTER (WHERE currency IS NULL OR NOT regexp_full_match(currency,'[A-Z]{3}') "
2062
  "OR money IS NULL OR NOT isfinite(money) OR money<0) AS money_bad,"
2063
  "count(*) FILTER (WHERE identity_bad OR conflict OR overlap OR day IS NULL) AS identity_bad,"
2064
  "count(*) FILTER (WHERE unit_bad) AS unit_bad,count(*) FILTER (WHERE source='finance') AS finance_units,"
2065
  "count(*) FILTER (WHERE overlap) AS overlaps FROM refund_events "
2066
- f"WHERE day BETWEEN DATE '{first}' AND DATE '{last}' OR day IS NULL" + (" GROUP BY key" if group else ""))
 
2067
 
2068
 
2069
  def _rate_values(row, ready):
@@ -3873,25 +3896,51 @@ def _dashboard_windows(first, last):
3873
 
3874
 
3875
  def _dashboard_rates(cur, first, last, independent):
3876
- if not independent:
3877
- return _aggregate(cur, _relations(cur, first, last, {'risingRatio': 1.5, 'risingMinPct': 5}))
3878
- # Independent scalar folds avoid the serving membership and formatter projections.
 
 
 
3879
  merchant_sku = _first(_columns(cur, MFN_TABLE), 'merchant_sku', 'sku') or 'merchant_sku'
3880
  physical = _return_source(cur, RETURNS_TABLE, 'return_date', 'quantity') + ' UNION ALL ' + _return_source(
3881
  cur, MFN_TABLE, 'return_request_date', 'return_quantity', merchant_sku, 'return_reason')
3882
  shipments = _return_source(cur, SHIPPED_TABLE, 'shipment_date', 'quantity')
3883
- operands = _mirror_one(cur, f"""WITH returns AS ({physical}),shipments AS ({shipments}),
3884
- {_refund_relations(cur)} SELECT * FROM
3885
- (SELECT COALESCE(sum(units),0) AS returned FROM returns WHERE day BETWEEN ? AND ?) r CROSS JOIN
3886
- (SELECT COALESCE(sum(units),0) AS shipped FROM shipments WHERE day BETWEEN ? AND ?) s CROSS JOIN
3887
- (SELECT COALESCE(sum(units),0) AS refunded,sum(money) AS refund_cost,
 
 
 
 
3888
  min(currency) AS refund_currency,count(DISTINCT currency) AS currencies,
3889
  count(*) FILTER (WHERE money IS NULL OR money<0 OR NOT isfinite(money)
3890
- OR currency IS NULL OR NOT regexp_full_match(currency,'[A-Z]{{3}}')) AS money_bad,
3891
  bool_or(identity_bad OR conflict OR overlap OR day IS NULL) AS identity_bad,
3892
- bool_or(unit_bad) AS unit_bad FROM refund_events
3893
- WHERE day>=? AND day<=? OR day IS NULL) f""", [first, last, first, last, first, last])
3894
- return _independent_rate_values(operands, _rate_availability(cur))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3895
 
3896
 
3897
  def _dashboard_figures(tenant_key, *, start, end, st=None, user=None, independent=False):
@@ -3901,12 +3950,12 @@ def _dashboard_figures(tenant_key, *, start, end, st=None, user=None, independen
3901
  total, previous, points, reasons, reasons_value = {}, {}, [], [], None
3902
  if allowed:
3903
  with _screen_reader(tenant_key, st) as (cur, _present):
3904
- total = _dashboard_rates(cur, first, last, independent)
3905
- previous = _dashboard_rates(cur, first - (last - first) - dt.timedelta(days=1),
3906
- first - dt.timedelta(days=1), independent)
3907
- points = [(a.isoformat(), _dashboard_rates(cur, a, b, independent))
3908
- for a, b in _dashboard_windows(first, last)]
3909
  ready = _rate_availability(cur)
 
 
 
 
 
3910
  if ready['returns'] and ready['merchantReturns']:
3911
  merchant_sku = _first(_columns(cur, MFN_TABLE), 'merchant_sku', 'sku') or 'merchant_sku'
3912
  source = _return_source(cur, RETURNS_TABLE, 'return_date', 'quantity') + ' UNION ALL ' + _return_source(
@@ -3986,19 +4035,19 @@ def validate_figure(tenant_key, figure, *, start, end, st=None, user=None):
3986
  series += [dict(label=reason_label(reason), value=units) for reason, units in batch]
3987
  value, unit, prior = sum(row['value'] for row in series), 'returns', None
3988
  else:
3989
- total = _dashboard_rates(cur, first, last, True)
 
 
 
3990
  value = total[key]
3991
  unit = total['currency'] if key == 'refundCost' else '%'
3992
  if value is None or not unit:
3993
  return failed
3994
- previous = _dashboard_rates(cur, first - (last - first) - dt.timedelta(days=1),
3995
- first - dt.timedelta(days=1), True)
3996
  prior = previous[key]
3997
  if key == 'refundCost' and previous['currency'] != unit:
3998
  prior = None
3999
  series = []
4000
- for begin, stop in _dashboard_windows(first, last):
4001
- row = _dashboard_rates(cur, begin, stop, True)
4002
  if key == 'refundCost' and row[key] is not None and row['currency'] != unit:
4003
  return failed
4004
  series.append(dict(x=begin.isoformat(), y=row[key]))
 
1987
  + (f" FROM {table}" if cols else " WHERE false") + ") captured")
1988
 
1989
 
1990
+ def _refund_overlap_relations():
1991
+ """Match wildcard identities through distinct projections with at most eight probes per identity."""
1992
+ fields = ('order_id', 'sku', 'settlement_id')
1993
+ mask = '+'.join(f'CASE WHEN {field} IS NULL THEN 0 ELSE {1 << i} END'
1994
+ for i, field in enumerate(fields))
1995
+ projection = ','.join(f"CASE WHEN (p.mask & {1 << i})!=0 THEN {field} ELSE '' END AS {field}"
1996
+ for i, field in enumerate(fields))
1997
+ matches = ' AND '.join(
1998
+ f"b.{field}=CASE WHEN (a.mask & m.mask & {1 << i})!=0 THEN a.{field} ELSE '' END"
1999
+ for i, field in enumerate(fields))
2000
+ identities = ' AND '.join(f'a.{field} IS NOT DISTINCT FROM o.{field}' for field in fields)
2001
+ return f"""refund_identities AS (
2002
+ SELECT DISTINCT source,account,order_id,sku,settlement_id,{mask} AS mask FROM refund_candidates),
2003
+ refund_overlap_keys AS (
2004
+ SELECT DISTINCT source,account,i.mask AS identity_mask,p.mask AS projection_mask,{projection}
2005
+ FROM refund_identities i CROSS JOIN range(8) p(mask) WHERE (i.mask & p.mask)=p.mask),
2006
+ refund_overlaps AS (
2007
+ SELECT a.source,a.account,a.order_id,a.sku,a.settlement_id,bool_or(b.source IS NOT NULL) AS overlap
2008
+ FROM refund_identities a CROSS JOIN range(8) m(mask)
2009
+ LEFT JOIN refund_overlap_keys b ON a.source!=b.source AND a.account=b.account
2010
+ AND b.identity_mask=m.mask AND b.projection_mask=(a.mask & m.mask) AND {matches}
2011
+ GROUP BY a.source,a.account,a.order_id,a.sku,a.settlement_id),
2012
+ refund_events AS (SELECT a.*,COALESCE(a.sku,a.asin,'{NO_SKU}') AS key,o.overlap
2013
+ FROM refund_candidates a JOIN refund_overlaps o ON a.source=o.source AND a.account=o.account
2014
+ AND {identities})"""
2015
+
2016
+
2017
  def _refund_relations(cur):
2018
  """Keep source identities, capture deduplication and ambiguous overlap inside SQL."""
2019
  sc, tc = (_columns(cur, table) for table in FINANCE_TABLES)
 
2073
  SELECT 'finance',account,settlement_id,event_id,item_id,order_id,sku,asin,day,NULL,money,currency,
2074
  conflict,event_id IS NULL OR position IS NULL OR NOT isfinite(position) OR position<0
2075
  OR position!=trunc(position) OR sku IS NULL AND asin IS NULL, true FROM finance_events),
2076
+ {_refund_overlap_relations()}"""
 
 
 
 
 
2077
 
2078
 
2079
+ def _refund_totals_sql(first, last, group=False, *, windows=False):
2080
  """Aggregate only event-grain refunds after source capture deduplication."""
2081
+ return ("SELECT " + ("window_id," if windows else "key," if group else "") + "COALESCE(sum(units),0) AS refunded,"
2082
  "sum(money) AS refund_cost,min(currency) AS refund_currency,count(DISTINCT currency) AS currencies,"
2083
  "count(*) FILTER (WHERE currency IS NULL OR NOT regexp_full_match(currency,'[A-Z]{3}') "
2084
  "OR money IS NULL OR NOT isfinite(money) OR money<0) AS money_bad,"
2085
  "count(*) FILTER (WHERE identity_bad OR conflict OR overlap OR day IS NULL) AS identity_bad,"
2086
  "count(*) FILTER (WHERE unit_bad) AS unit_bad,count(*) FILTER (WHERE source='finance') AS finance_units,"
2087
  "count(*) FILTER (WHERE overlap) AS overlaps FROM refund_events "
2088
+ + ("JOIN windows ON day BETWEEN first AND last OR day IS NULL GROUP BY window_id" if windows else
2089
+ f"WHERE day BETWEEN DATE '{first}' AND DATE '{last}' OR day IS NULL" + (" GROUP BY key" if group else "")))
2090
 
2091
 
2092
  def _rate_values(row, ready):
 
3896
 
3897
 
3898
  def _dashboard_rates(cur, first, last, independent):
3899
+ return _dashboard_rate_windows(cur, [(first, last)], independent)[0]
3900
+
3901
+
3902
+ def _dashboard_rate_windows(cur, windows, independent=False, *, ready=None):
3903
+ """Fold total, comparison and monthly windows in one query over complete source captures."""
3904
+ bounds = ','.join(f"({i},DATE '{a.isoformat()}',DATE '{b.isoformat()}')" for i, (a, b) in enumerate(windows))
3905
  merchant_sku = _first(_columns(cur, MFN_TABLE), 'merchant_sku', 'sku') or 'merchant_sku'
3906
  physical = _return_source(cur, RETURNS_TABLE, 'return_date', 'quantity') + ' UNION ALL ' + _return_source(
3907
  cur, MFN_TABLE, 'return_request_date', 'return_quantity', merchant_sku, 'return_reason')
3908
  shipments = _return_source(cur, SHIPPED_TABLE, 'shipment_date', 'quantity')
3909
+ prefix = f"""WITH windows(window_id,first,last) AS (VALUES {bounds}),
3910
+ returns AS ({physical}),shipments AS ({shipments}),{_refund_relations(cur)},"""
3911
+ if independent:
3912
+ # Separate scalar source folds do not reuse the serving aggregate or formatter.
3913
+ sql = prefix + """r AS (SELECT window_id,COALESCE(sum(units),0) AS returned
3914
+ FROM windows LEFT JOIN returns ON day>=first AND day<=last GROUP BY window_id),
3915
+ s AS (SELECT window_id,COALESCE(sum(units),0) AS shipped
3916
+ FROM windows LEFT JOIN shipments ON day>=first AND day<=last GROUP BY window_id),
3917
+ f AS (SELECT window_id,COALESCE(sum(units),0) AS refunded,sum(money) AS refund_cost,
3918
  min(currency) AS refund_currency,count(DISTINCT currency) AS currencies,
3919
  count(*) FILTER (WHERE money IS NULL OR money<0 OR NOT isfinite(money)
3920
+ OR currency IS NULL OR NOT regexp_full_match(currency,'[A-Z]{3}')) AS money_bad,
3921
  bool_or(identity_bad OR conflict OR overlap OR day IS NULL) AS identity_bad,
3922
+ bool_or(unit_bad) AS unit_bad FROM refund_events JOIN windows
3923
+ ON day>=first AND day<=last OR day IS NULL GROUP BY window_id)
3924
+ SELECT r.*,s.shipped,f.* EXCLUDE(window_id) FROM r JOIN s USING(window_id)
3925
+ LEFT JOIN f USING(window_id) ORDER BY window_id"""
3926
+ else:
3927
+ sql = prefix + """events AS (
3928
+ SELECT day,units AS returned,0.0 AS shipped FROM returns
3929
+ UNION ALL SELECT day,0.0,units FROM shipments),
3930
+ quantities AS (SELECT window_id,COALESCE(sum(returned),0) AS returned,
3931
+ COALESCE(sum(shipped),0) AS shipped FROM windows LEFT JOIN events
3932
+ ON day BETWEEN first AND last GROUP BY window_id),
3933
+ refunds AS (""" + _refund_totals_sql(None, None, windows=True) + """)
3934
+ SELECT quantities.*,refunds.* EXCLUDE(window_id) FROM quantities
3935
+ LEFT JOIN refunds USING(window_id) ORDER BY window_id"""
3936
+ ready = _rate_availability(cur) if ready is None else ready
3937
+ result = cur.execute(sql)
3938
+ names = [field[0] for field in result.description]
3939
+ rows = []
3940
+ while batch := result.fetchmany(MAX_PAGE_SIZE):
3941
+ rows.extend(dict(zip(names, row)) for row in batch)
3942
+ formatter = _independent_rate_values if independent else _rate_values
3943
+ return [formatter(row, ready) for row in rows]
3944
 
3945
 
3946
  def _dashboard_figures(tenant_key, *, start, end, st=None, user=None, independent=False):
 
3950
  total, previous, points, reasons, reasons_value = {}, {}, [], [], None
3951
  if allowed:
3952
  with _screen_reader(tenant_key, st) as (cur, _present):
 
 
 
 
 
3953
  ready = _rate_availability(cur)
3954
+ months = list(_dashboard_windows(first, last))
3955
+ windows = [(first, last), (first - (last - first) - dt.timedelta(days=1),
3956
+ first - dt.timedelta(days=1)), *months]
3957
+ total, previous, *monthly = _dashboard_rate_windows(cur, windows, independent, ready=ready)
3958
+ points = [(a.isoformat(), values) for (a, _), values in zip(months, monthly)]
3959
  if ready['returns'] and ready['merchantReturns']:
3960
  merchant_sku = _first(_columns(cur, MFN_TABLE), 'merchant_sku', 'sku') or 'merchant_sku'
3961
  source = _return_source(cur, RETURNS_TABLE, 'return_date', 'quantity') + ' UNION ALL ' + _return_source(
 
4035
  series += [dict(label=reason_label(reason), value=units) for reason, units in batch]
4036
  value, unit, prior = sum(row['value'] for row in series), 'returns', None
4037
  else:
4038
+ months = list(_dashboard_windows(first, last))
4039
+ windows = [(first, last), (first - (last - first) - dt.timedelta(days=1),
4040
+ first - dt.timedelta(days=1)), *months]
4041
+ total, previous, *monthly = _dashboard_rate_windows(cur, windows, True)
4042
  value = total[key]
4043
  unit = total['currency'] if key == 'refundCost' else '%'
4044
  if value is None or not unit:
4045
  return failed
 
 
4046
  prior = previous[key]
4047
  if key == 'refundCost' and previous['currency'] != unit:
4048
  prior = None
4049
  series = []
4050
+ for (begin, _), row in zip(months, monthly):
 
4051
  if key == 'refundCost' and row[key] is not None and row['currency'] != unit:
4052
  return failed
4053
  series.append(dict(x=begin.isoformat(), y=row[key]))
platform/modules/warehouse.py CHANGED
@@ -13,6 +13,7 @@ reconciles row-built aggregates to independent read_group/search_count paths.
13
  """
14
  import sys
15
  import datetime as dt
 
16
  from pathlib import Path
17
  sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
18
  import core.odoo as O
@@ -792,9 +793,14 @@ def fba_qty_by_product_id(product_ids=None, wh=None):
792
  for i in range(0, len(ids), _FBA_BATCH):
793
  batches += 1
794
  chunk = ids[i:i + _FBA_BATCH]
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
 
 
13
  """
14
  import sys
15
  import datetime as dt
16
+ import math
17
  from pathlib import Path
18
  sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
19
  import core.odoo as O
 
793
  for i in range(0, len(ids), _FBA_BATCH):
794
  batches += 1
795
  chunk = ids[i:i + _FBA_BATCH]
796
+ # Explicit selected ids include archived products; invalid quantities stay unknown.
797
+ for r in o.execute_kw('product.product', 'search_read',
798
+ [[('id', 'in', chunk), ('active', 'in', [True, False])]],
799
  {'fields': ['id', 'qty_available'], 'context': ctx}):
800
+ quantity = r.get('qty_available')
801
+ if (isinstance(quantity, (int, float)) and not isinstance(quantity, bool)
802
+ and math.isfinite(quantity)):
803
+ out[r['id']] = quantity
804
  return out, fba_report(wh, len(ids), len(out), batches,
805
  'stock.warehouse.code at runtime, never a pinned id')
806
 
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 10c06501f5337d94cc128c03b1fd10849aea6e293cf1290eb238d7c65538140d
 
1
+ b50b579feefedb216021592e1ddd9160f4c6539ccea7f0bfbc906ab7944626c5
web/src/apps/AppRail.tsx CHANGED
@@ -13,6 +13,7 @@ interface RailApplication {
13
  deepLink?: string;
14
  templateKind?: string;
15
  templateConnector?: string;
 
16
  openTarget?: { database?: string };
17
  compatibleDatabases?: string[];
18
  capabilities?: Record<string, boolean>;
@@ -31,7 +32,8 @@ function railApplications(body: unknown): RailApplication[] {
31
  || row.capabilities?.read === false) return false;
32
  seen.add(row.id);
33
  return true;
34
- }).sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: "base" }) || a.id.localeCompare(b.id));
 
35
  }
36
 
37
  /** An Amazon App's rail row draws its own mark, never the Amazon logo (QI37). */
 
13
  deepLink?: string;
14
  templateKind?: string;
15
  templateConnector?: string;
16
+ navPlacement?: { order?: number };
17
  openTarget?: { database?: string };
18
  compatibleDatabases?: string[];
19
  capabilities?: Record<string, boolean>;
 
32
  || row.capabilities?.read === false) return false;
33
  seen.add(row.id);
34
  return true;
35
+ }).sort((a, b) => (a.navPlacement?.order ?? 0) - (b.navPlacement?.order ?? 0)
36
+ || a.name.localeCompare(b.name, undefined, { sensitivity: "base" }) || a.id.localeCompare(b.id));
37
  }
38
 
39
  /** An Amazon App's rail row draws its own mark, never the Amazon logo (QI37). */
web/src/apps/account-health/AppView.tsx CHANGED
@@ -1,5 +1,6 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
  import { API_V1, CREDENTIALS } from "../../apiContract";
 
3
  import { useEscapeClose } from "../../ui/useEscapeClose";
4
  import { AppTabs } from "../AppTabs";
5
  import { LineChart } from "../../viz/kit";
@@ -7,7 +8,7 @@ import Dashboard from "./Dashboard";
7
  import "../../styles/sdlc79/account-health.css";
8
  import "../../styles/sdlc81/account-health-risk.css";
9
 
10
- // The dispatcher owns the App frame. This view owns its lists and right-side detail panel.
11
 
12
  /** C2: the App as the dispatcher passes it. Structurally equal to lane A's AppViewProps application. */
13
  interface AccountHealthApplication {
@@ -93,16 +94,30 @@ interface RiskPage {
93
 
94
  interface Point { day: string; value: number | null; target: number | null; status: string }
95
  interface HistoryPage { points: Point[]; cursor: string | null; label: string; unit: string }
 
 
 
 
 
 
96
 
97
  type Load<T> = { phase: "loading"; value?: T } | { phase: "error"; message: string; value?: T }
98
  | { phase: "ready"; value: T };
99
- type Picked = { kind: "metric" | "alert" | "risk"; id: string; metric?: Metric; alert?: Alert; risk?: Risk; linked?: boolean };
100
  type Tab = "dashboard" | "metrics" | "alerts" | "risks";
101
 
102
  const EMPTY = "No account health data";
103
  const ALERT_STATUSES = ["Open", "Investigating", "Resolved", "Dismissed"];
104
  const PAGE = 50;
105
  const HISTORY_PAGE = 50;
 
 
 
 
 
 
 
 
106
 
107
  const text = (value: unknown) => (typeof value === "string" ? value : "");
108
  const num = (value: unknown) => (typeof value === "number" && Number.isFinite(value) ? value : null);
@@ -233,6 +248,21 @@ function parseHistory(raw: unknown): HistoryPage {
233
  };
234
  }
235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  /** "Oct 22" from an ISO day. A calendar day is read in UTC. */
237
  function day(iso: string) {
238
  if (!iso) return "";
@@ -294,6 +324,135 @@ function Failed({ message, onRetry }: { message: string; onRetry: () => void })
294
  <button type="button" className="ah-link" onClick={onRetry}>Try again</button></p>;
295
  }
296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  // Busy buttons stay focusable so Enter keeps its place without a focus restore.
298
  function Pager({ page, previous, next, busy, onPrevious, onNext }: {
299
  page: number; previous: boolean; next: boolean; busy: boolean; onPrevious: () => void; onNext: () => void;
@@ -638,7 +797,11 @@ function MetricCell({ column, row, markets, picked, groups, onPick }: {
638
  switch (column) {
639
  case "label": return <td className="ah-c-label">
640
  <button type="button" className="ah-open" data-metric={row.id} aria-pressed={picked === row.id}
641
- onClick={() => onPick(row)}>{row.label}</button>
 
 
 
 
642
  {markets > 1 && row.marketplaceId && <span className="ah-sub">{row.marketplaceId}</span>}
643
  </td>;
644
  case "status": return <td className="ah-c-status"><Chip status={row.status} /></td>;
@@ -891,7 +1054,7 @@ function AccountHealthBody({ application, canEdit }: AccountHealthViewProps) {
891
  }, [url, tab, riskStatusFilter, riskOffset, query, risksAttempt]);
892
 
893
  useEffect(() => {
894
- if (!picked || picked.kind === "risk") return;
895
  const controller = new AbortController();
896
  setDetailAccess({ owners: [], canEdit: false });
897
  if (picked.kind === "alert" && picked.linked) {
@@ -938,7 +1101,7 @@ function AccountHealthBody({ application, canEdit }: AccountHealthViewProps) {
938
  const pick = (next: Picked) => {
939
  setDetailMetric(next.metric ?? null);
940
  setDetailAlert(next.alert ?? null);
941
- setDetailLoading(next.kind !== "risk");
942
  setDetailError("");
943
  opener.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
944
  setPicked(next);
@@ -969,9 +1132,9 @@ function AccountHealthBody({ application, canEdit }: AccountHealthViewProps) {
969
  const alert = detailAlert;
970
  const alerting = (view?.metrics ?? []).filter((row) => !FOLDED.includes(row.status));
971
  const folded = (view?.metrics ?? []).filter((row) => FOLDED.includes(row.status));
972
- const metricPick = (row: Metric) => pick({ kind: "metric", id: row.id, metric: row });
973
 
974
- const open = Boolean(picked);
975
  const busy = tab === "risks" ? risks.phase !== "ready"
976
  : tab === "metrics" ? summary.phase !== "ready" : alerts.phase !== "ready";
977
  const shownOffset = (tab === "risks" ? riskPage?.offset : tab === "metrics" ? view?.offset : page?.offset) ?? 0;
@@ -1018,13 +1181,13 @@ function AccountHealthBody({ application, canEdit }: AccountHealthViewProps) {
1018
  && <p className="ah-empty" role="status">No matching metrics</p>}
1019
  {summary.phase === "ready" && view?.state === "ready" && alerting.length > 0
1020
  && <MetricTable metrics={alerting} markets={view.markets} groups={view.groups}
1021
- picked={picked?.kind === "metric" ? picked.id : ""} onPick={metricPick} />}
1022
  {summary.phase === "ready" && view?.state === "ready" && view.folded > 0 && <button type="button"
1023
  className="ah-fold" aria-expanded={foldOpen} onClick={() => setFoldOpen((open) => !open)}>
1024
  {foldText(view.folded)}</button>}
1025
  {summary.phase === "ready" && view?.state === "ready" && foldOpen && folded.length > 0
1026
  && <MetricTable metrics={folded} markets={view.markets} groups={view.groups}
1027
- picked={picked?.kind === "metric" ? picked.id : ""} onPick={metricPick} />}
1028
  </div>}
1029
 
1030
  {tab === "alerts" && <div className="ah-body" role="tabpanel" aria-label="Alerts">
@@ -1072,6 +1235,8 @@ function AccountHealthBody({ application, canEdit }: AccountHealthViewProps) {
1072
  onPrevious={() => movePage(Math.max(0, shownOffset - shownLimit))}
1073
  onNext={() => movePage(shownOffset + shownLimit)} />}
1074
  </div>
 
 
1075
  {open && <Panel key={`${picked?.kind}:${picked?.id}`} metric={metric} alert={alert} risk={picked?.risk ?? null}
1076
  editable={picked?.kind === "risk" ? riskEditable : picked?.kind === "alert" && picked.linked
1077
  ? canEdit === true && detailAccess.canEdit && !detailLoading && !detailError : editable}
 
1
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
2
  import { API_V1, CREDENTIALS } from "../../apiContract";
3
+ import { BodyPortal, useOverlayLayer } from "../../customer-grid/OverlaySurface";
4
  import { useEscapeClose } from "../../ui/useEscapeClose";
5
  import { AppTabs } from "../AppTabs";
6
  import { LineChart } from "../../viz/kit";
 
8
  import "../../styles/sdlc79/account-health.css";
9
  import "../../styles/sdlc81/account-health-risk.css";
10
 
11
+ // The dispatcher owns the App frame. This view owns its lists and detail overlays.
12
 
13
  /** C2: the App as the dispatcher passes it. Structurally equal to lane A's AppViewProps application. */
14
  interface AccountHealthApplication {
 
94
 
95
  interface Point { day: string; value: number | null; target: number | null; status: string }
96
  interface HistoryPage { points: Point[]; cursor: string | null; label: string; unit: string }
97
+ interface Category {
98
+ key: string; label: string; count: number | null; status: string; target: string;
99
+ window: { from: string; to: string }; sourceNote: string; sellerCentralUrl: string;
100
+ related: { asin: string; sku: string; issue: string; severity: string }[];
101
+ }
102
+ type CategoryLoad = Load<Category> | { phase: "refused"; message: string };
103
 
104
  type Load<T> = { phase: "loading"; value?: T } | { phase: "error"; message: string; value?: T }
105
  | { phase: "ready"; value: T };
106
+ type Picked = { kind: "metric" | "category" | "alert" | "risk"; id: string; metric?: Metric; alert?: Alert; risk?: Risk; linked?: boolean };
107
  type Tab = "dashboard" | "metrics" | "alerts" | "risks";
108
 
109
  const EMPTY = "No account health data";
110
  const ALERT_STATUSES = ["Open", "Investigating", "Resolved", "Dismissed"];
111
  const PAGE = 50;
112
  const HISTORY_PAGE = 50;
113
+ const CATEGORY_KEYS = new Set([
114
+ "listing_policy_violations", "product_authenticity_customer_complaints", "product_condition_customer_complaints",
115
+ "product_safety_customer_complaints", "food_and_product_safety_issues", "received_intellectual_property_complaints",
116
+ "suspected_intellectual_property_violations", "restricted_product_policy_violations",
117
+ "customer_product_reviews_policy_violations", "other_policy_violations", "document_requests",
118
+ ]);
119
+ const categoryKey = (row: Metric) => row.key || row.id.split("@")[0];
120
+ const isCategory = (row: Metric) => CATEGORY_KEYS.has(categoryKey(row));
121
 
122
  const text = (value: unknown) => (typeof value === "string" ? value : "");
123
  const num = (value: unknown) => (typeof value === "number" && Number.isFinite(value) ? value : null);
 
248
  };
249
  }
250
 
251
+ function parseCategory(raw: unknown, key: string): Category {
252
+ const body = record(raw);
253
+ if (text(body.key) !== key || body.recordList !== false) throw new Error("This category could not be read.");
254
+ const window = record(body.window);
255
+ return {
256
+ key, label: text(body.label), count: num(body.count), status: text(body.status) || "No data",
257
+ target: text(body.target), window: { from: text(window.from), to: text(window.to) },
258
+ sourceNote: text(body.sourceNote), sellerCentralUrl: text(body.sellerCentralUrl),
259
+ related: list(body.related).map((raw) => {
260
+ const row = record(raw);
261
+ return { asin: text(row.asin), sku: text(row.sku), issue: text(row.issue), severity: text(row.severity) };
262
+ }),
263
+ };
264
+ }
265
+
266
  /** "Oct 22" from an ISO day. A calendar day is read in UTC. */
267
  function day(iso: string) {
268
  if (!iso) return "";
 
324
  <button type="button" className="ah-link" onClick={onRetry}>Try again</button></p>;
325
  }
326
 
327
+ function CategoryPopup({ metric, appId, onClose }: { metric: Metric; appId: string; onClose: () => void }) {
328
+ const panel = useRef<HTMLDivElement>(null);
329
+ const titleId = useId();
330
+ const noteId = useId();
331
+ const [load, setLoad] = useState<CategoryLoad>({ phase: "loading" });
332
+ const [attempt, setAttempt] = useState(0);
333
+ useOverlayLayer({ panelRef: panel, onDismiss: onClose, dismissOnOutside: false,
334
+ initialFocus: "[data-category-close]", restoreFocus: true, trapFocus: true });
335
+
336
+ useEffect(() => {
337
+ const controller = new AbortController();
338
+ setLoad({ phase: "loading" });
339
+ const query = new URLSearchParams({ app: appId });
340
+ void fetch(`${API_V1}/account-health/categories/${encodeURIComponent(metric.id)}?${query.toString()}`,
341
+ { credentials: CREDENTIALS, signal: controller.signal })
342
+ .then(async (response) => {
343
+ if (response.status === 401 || response.status === 403) {
344
+ const message = await readJson(response, "You do not have access to this category.")
345
+ .then(() => "You do not have access to this category.")
346
+ .catch((reason: unknown) => failure(reason, "You do not have access to this category."));
347
+ if (!controller.signal.aborted) setLoad({ phase: "refused", message });
348
+ return;
349
+ }
350
+ const category = parseCategory(await readJson(response, "This category could not be read."), categoryKey(metric));
351
+ if (!controller.signal.aborted) setLoad({ phase: "ready", value: category });
352
+ })
353
+ .catch((reason: unknown) => {
354
+ if (!controller.signal.aborted) setLoad({ phase: "error", message: failure(reason, "This category could not be read.") });
355
+ });
356
+ return () => controller.abort();
357
+ }, [appId, metric, attempt]);
358
+
359
+ const category = load.phase === "ready" ? load.value : null;
360
+ const relatedIncomplete = Boolean(category
361
+ && /unavailable|need access|missing or unreadable|missing from the saved report/i.test(category.sourceNote));
362
+ const count = category ? amount(category.count, "count") : "";
363
+ const dataState = load.phase !== "ready" ? load.phase === "refused" ? "no-permission" : load.phase
364
+ : category?.count === null ? "na" : category?.count === 0 ? "empty" : "ready";
365
+
366
+ return <BodyPortal>
367
+ <div className="lp-dialog-scrim">
368
+ <div ref={panel} className="lp-dialog ah-category-popup" role="dialog" aria-modal="true"
369
+ aria-labelledby={titleId} data-category={metric.id} data-state={dataState} tabIndex={-1}>
370
+ <header className="lp-dialog-head ah-category-head">
371
+ <span className="ah-muted">Account health</span>
372
+ <button type="button" className="ah-button is-quiet" data-category-close onClick={onClose}>Close</button>
373
+ </header>
374
+ <div className="lp-dialog-body ah-category-body" aria-busy={load.phase === "loading"}>
375
+ <section className="ah-category-card" aria-labelledby={titleId} data-category-card>
376
+ <header className="ah-category-card-head">
377
+ <div className="ah-category-title">
378
+ <h2 id={titleId} className="lp-dialog-title">{category?.label || metric.label}</h2>
379
+ <svg className="ah-category-info" viewBox="0 0 16 16" role="img" aria-label="Source information">
380
+ <title>Amazon account health</title>
381
+ <circle cx="8" cy="8" r="6.1" fill="none" stroke="currentColor" strokeWidth="1.25" />
382
+ <path d="M8 7.4v3.5" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
383
+ <circle cx="8" cy="5.1" r="0.85" fill="currentColor" />
384
+ </svg>
385
+ </div>
386
+ {category && <span className="ah-category-count" data-category-count aria-label={`Count: ${count}`}>{count}</span>}
387
+ </header>
388
+ <p className="ah-note ah-category-subtitle" data-category-record-note>Amazon sends no record list for this category</p>
389
+ {load.phase === "loading" && <Skeleton rows={5} />}
390
+ {load.phase === "error" && <Failed message={load.message} onRetry={() => {
391
+ panel.current?.querySelector<HTMLButtonElement>("[data-category-close]")?.focus();
392
+ setAttempt((n) => n + 1);
393
+ }} />}
394
+ {load.phase === "refused" && <p className="ah-note" role="status">{load.message}</p>}
395
+ {category && <>
396
+ <div className="ah-category-empty" role="status" data-category-record-list
397
+ data-record-state={category.count === 0 ? "empty" : "unavailable"}>
398
+ <svg className={`ah-category-empty-mark${category.count === 0 ? " is-good" : ""}`}
399
+ viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" aria-hidden="true">
400
+ <circle cx="8" cy="8" r="6.1" />
401
+ <path d={category.count === 0 ? "m5 8 2 2 4-4" : "M5.5 8h5"} strokeLinecap="round" strokeLinejoin="round" />
402
+ </svg>
403
+ <p className="ah-empty">{category.count === 0 ? "No violations reported" : "Record list unavailable"}</p>
404
+ </div>
405
+ <dl className="ah-category-facts">
406
+ <div className="ah-category-fact"><dt>Status</dt><dd><Chip status={category.status} /></dd></div>
407
+ <div className="ah-category-fact"><dt>Amazon&apos;s target</dt><dd>{category.target || "No target"}</dd></div>
408
+ </dl>
409
+ <footer className="ah-category-footer">
410
+ <div className="ah-category-window"><span>Reporting window</span>
411
+ <span>{category.window.from && category.window.to
412
+ ? <><time dateTime={category.window.from}>{day(category.window.from)}</time>{" to "}
413
+ <time dateTime={category.window.to}>{day(category.window.to)}</time></>
414
+ : "No data"}</span>
415
+ </div>
416
+ {category.sellerCentralUrl && <a className="ah-link" data-category-seller-central
417
+ href={category.sellerCentralUrl} target="_blank" rel="noopener noreferrer">Open in Seller Central</a>}
418
+ </footer>
419
+ </>}
420
+ </section>
421
+ {category &&
422
+ <section className="ah-section ah-category-related" aria-label="Flagged listings" aria-describedby={noteId}
423
+ data-related-state={relatedIncomplete ? "incomplete" : category.related.length ? "ready" : "empty"}>
424
+ <div className="ah-category-related-head">
425
+ <h3 className="ah-heading">Flagged listings</h3><span className="ah-muted">Related</span>
426
+ </div>
427
+ <p id={noteId} className="ah-note" data-category-source-note>{category.sourceNote}</p>
428
+ {category.related.length ? <ul className="ah-category-list">
429
+ {category.related.map((listing, index) => {
430
+ const severity = listing.severity.toLowerCase().replace(/_/g, " ");
431
+ const severityTone = /error|critical|fatal/.test(severity) ? "is-risk"
432
+ : /warn/.test(severity) ? "is-near" : "is-quiet";
433
+ return <li key={index} className="ah-category-listing" data-related-listing>
434
+ <div className="ah-category-listing-head">
435
+ <div className="ah-category-identifiers">
436
+ {listing.sku && <span className="ah-category-listing-name">Sku: {listing.sku}</span>}
437
+ {listing.asin && <span>Asin: {listing.asin}</span>}
438
+ {!listing.sku && !listing.asin && <span className="ah-category-listing-name">Listing</span>}
439
+ </div>
440
+ {severity && <span className={`ah-chip ${severityTone}`}>{severity[0].toUpperCase() + severity.slice(1)}</span>}
441
+ </div>
442
+ <p className="ah-note">{listing.issue || "No issue description"}</p>
443
+ </li>;
444
+ })}
445
+ </ul> : <div className="ah-category-related-empty" role="status">
446
+ {relatedIncomplete ? "Related listings unavailable" : "No flagged listings"}
447
+ </div>}
448
+ </section>
449
+ }
450
+ </div>
451
+ </div>
452
+ </div>
453
+ </BodyPortal>;
454
+ }
455
+
456
  // Busy buttons stay focusable so Enter keeps its place without a focus restore.
457
  function Pager({ page, previous, next, busy, onPrevious, onNext }: {
458
  page: number; previous: boolean; next: boolean; busy: boolean; onPrevious: () => void; onNext: () => void;
 
797
  switch (column) {
798
  case "label": return <td className="ah-c-label">
799
  <button type="button" className="ah-open" data-metric={row.id} aria-pressed={picked === row.id}
800
+ data-category-opener={isCategory(row) ? row.id : undefined} aria-haspopup={isCategory(row) ? "dialog" : undefined}
801
+ onClick={(event) => {
802
+ if (isCategory(row)) event.currentTarget.focus({ preventScroll: true });
803
+ onPick(row);
804
+ }}>{row.label}</button>
805
  {markets > 1 && row.marketplaceId && <span className="ah-sub">{row.marketplaceId}</span>}
806
  </td>;
807
  case "status": return <td className="ah-c-status"><Chip status={row.status} /></td>;
 
1054
  }, [url, tab, riskStatusFilter, riskOffset, query, risksAttempt]);
1055
 
1056
  useEffect(() => {
1057
+ if (!picked || picked.kind === "risk" || picked.kind === "category") return;
1058
  const controller = new AbortController();
1059
  setDetailAccess({ owners: [], canEdit: false });
1060
  if (picked.kind === "alert" && picked.linked) {
 
1101
  const pick = (next: Picked) => {
1102
  setDetailMetric(next.metric ?? null);
1103
  setDetailAlert(next.alert ?? null);
1104
+ setDetailLoading(next.kind !== "risk" && next.kind !== "category");
1105
  setDetailError("");
1106
  opener.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
1107
  setPicked(next);
 
1132
  const alert = detailAlert;
1133
  const alerting = (view?.metrics ?? []).filter((row) => !FOLDED.includes(row.status));
1134
  const folded = (view?.metrics ?? []).filter((row) => FOLDED.includes(row.status));
1135
+ const metricPick = (row: Metric) => pick({ kind: isCategory(row) ? "category" : "metric", id: row.id, metric: row });
1136
 
1137
+ const open = Boolean(picked && picked.kind !== "category");
1138
  const busy = tab === "risks" ? risks.phase !== "ready"
1139
  : tab === "metrics" ? summary.phase !== "ready" : alerts.phase !== "ready";
1140
  const shownOffset = (tab === "risks" ? riskPage?.offset : tab === "metrics" ? view?.offset : page?.offset) ?? 0;
 
1181
  && <p className="ah-empty" role="status">No matching metrics</p>}
1182
  {summary.phase === "ready" && view?.state === "ready" && alerting.length > 0
1183
  && <MetricTable metrics={alerting} markets={view.markets} groups={view.groups}
1184
+ picked={picked?.kind === "metric" || picked?.kind === "category" ? picked.id : ""} onPick={metricPick} />}
1185
  {summary.phase === "ready" && view?.state === "ready" && view.folded > 0 && <button type="button"
1186
  className="ah-fold" aria-expanded={foldOpen} onClick={() => setFoldOpen((open) => !open)}>
1187
  {foldText(view.folded)}</button>}
1188
  {summary.phase === "ready" && view?.state === "ready" && foldOpen && folded.length > 0
1189
  && <MetricTable metrics={folded} markets={view.markets} groups={view.groups}
1190
+ picked={picked?.kind === "metric" || picked?.kind === "category" ? picked.id : ""} onPick={metricPick} />}
1191
  </div>}
1192
 
1193
  {tab === "alerts" && <div className="ah-body" role="tabpanel" aria-label="Alerts">
 
1235
  onPrevious={() => movePage(Math.max(0, shownOffset - shownLimit))}
1236
  onNext={() => movePage(shownOffset + shownLimit)} />}
1237
  </div>
1238
+ {picked?.kind === "category" && picked.metric && <CategoryPopup key={picked.id} metric={picked.metric}
1239
+ appId={appId} onClose={closePanel} />}
1240
  {open && <Panel key={`${picked?.kind}:${picked?.id}`} metric={metric} alert={alert} risk={picked?.risk ?? null}
1241
  editable={picked?.kind === "risk" ? riskEditable : picked?.kind === "alert" && picked.linked
1242
  ? canEdit === true && detailAccess.canEdit && !detailLoading && !detailError : editable}
web/src/apps/amazon-profit/AppView.tsx CHANGED
@@ -4,6 +4,7 @@ import { ACCESS_REFUSED_MESSAGE, clearTopicRowsCache, dropTableRowsCache } from
4
  import { AMAZON_DATA_ENTRY_TABLE, AMAZON_PROFIT_TABLE, AmazonPeriodSelect, COMMAND_CENTER_TABLES,
5
  CommandCenterTabStrip, CommandCenterTableContext } from "../../customer-grid/AmazonPeriodPicker";
6
  import { OverlayProvider } from "../../customer-grid/OverlaySurface";
 
7
  import type { AppViewProps } from "../../customer-grid/viewModes";
8
  import { LogoLoader } from "../../ui/LogoLoader";
9
  import FbaCapacityControl from "./FbaCapacityControl";
@@ -16,8 +17,12 @@ import "../../styles/sdlc81/command-center-app.css";
16
  // Lazy, as every other host loads it: a static import pulls the grid into this App's chunk.
17
  const CustomerGrid = lazy(() => import("../../customer-grid/CustomerGrid"));
18
 
19
- /** Which Command Center tables exist, keyed by table; "failed" mounts the grid as before. */
20
- type TableProbe = Record<string, boolean> | "failed" | null;
 
 
 
 
21
 
22
  // The Command Center App body: the Table module's own Grid, hosted under the App frame.
23
  // The tab strip switches `table` through the context, so the URL stays on the App.
@@ -28,6 +33,17 @@ export default function ProjectManagementView(_props: AppViewProps) {
28
  const [projects, setProjects] = useState<ProjectsTarget | null>(null);
29
  const [competitors, setCompetitors] = useState<ProjectsTarget | null>(null);
30
  const [gridRevision, setGridRevision] = useState(0);
 
 
 
 
 
 
 
 
 
 
 
31
  const reloadGrid = useCallback(() => {
32
  for (const key of [AMAZON_PROFIT_TABLE, "ut_amazon_asin_profit", "ut_amazon_parent_profit"]) {
33
  clearTopicRowsCache(`tables/${key}/rows`);
@@ -40,23 +56,27 @@ export default function ProjectManagementView(_props: AppViewProps) {
40
  useEffect(() => {
41
  const controller = new AbortController();
42
  void fetch(`${API_V1}/amazon-profit/tables`, { credentials: CREDENTIALS, cache: "no-store",
43
- signal: controller.signal })
44
  .then(async (res) => {
45
  if (!res.ok) throw new Error("probe refused");
46
  const body = await res.json() as { tables?: Record<string, boolean> };
47
- setProbe(body.tables ?? {});
 
 
48
  })
49
- .catch(() => { if (!controller.signal.aborted) setProbe((last) => last ?? "failed"); });
50
  return () => controller.abort();
51
  }, [table]);
52
- const notBuilt = probe !== null && probe !== "failed" && probe[table] === false;
53
- const inaccessible = probe !== null && probe !== "failed" && !Object.hasOwn(probe, table);
 
 
 
 
54
  const dataEntry = table === AMAZON_DATA_ENTRY_TABLE;
55
  // Only an empty, permitted row response proves every SKU has its data.
56
- const [entryEmpty, setEntryEmpty] = useState<{ table: string; empty: boolean } | null>(null);
57
  useEffect(() => {
58
- if (!dataEntry) { setEntryEmpty(null); return; }
59
- if (probe === null || notBuilt || inaccessible) return;
60
  const controller = new AbortController();
61
  void fetch(`${API_V1}/tables/${AMAZON_DATA_ENTRY_TABLE}/rows`, { credentials: CREDENTIALS,
62
  cache: "no-store", signal: AbortSignal.any([controller.signal, AbortSignal.timeout(20000)]) })
@@ -67,27 +87,32 @@ export default function ProjectManagementView(_props: AppViewProps) {
67
  })
68
  .catch(() => { if (!controller.signal.aborted) setEntryEmpty({ table, empty: false }); });
69
  return () => controller.abort();
70
- }, [dataEntry, probe, notBuilt, inaccessible, table]);
71
  const entryChecked = !dataEntry || notBuilt || entryEmpty?.table === table;
72
- const allComplete = dataEntry && !notBuilt && entryEmpty?.table === table && entryEmpty.empty;
73
  // A new key remounts the grid per table, as the Table module does on a database switch.
74
- return <CommandCenterTableContext.Provider value={setTable}>
75
  <OverlayProvider>
76
- {dataEntry && probe !== null && !inaccessible && <FbaCapacityControl />}
77
- {table === "ut_amazon_projects" ? <div style={{ display: "flex", flexDirection: "column", flex: "1 1 0", minHeight: 0, padding: "4px 12px 0" }}>
78
- <CommandCenterTabStrip database={table} onOpenTable={setTable} />
79
- <ProjectsPage />
80
- </div> : probe === null ? <LogoLoader data-app-reason="loading" /> : inaccessible || notBuilt || allComplete
81
- ? <div className="cc-not-built" data-testid={inaccessible ? "command-center-refused" : notBuilt ? "command-center-not-built" : "command-center-complete"}
82
  style={{ display: "flex", flexDirection: "column", flex: "1 1 0", minHeight: 0, padding: "4px 12px 0" }}>
83
- <CommandCenterTabStrip database={table} onOpenTable={setTable} />
84
- <p className="ap-state" role="status">{inaccessible ? ACCESS_REFUSED_MESSAGE : notBuilt ? "This table is not built yet. An admin Refresh builds it."
85
- : "Every SKU has its data."}</p>
 
 
 
 
 
86
  </div>
87
  : !entryChecked ? <LogoLoader data-app-reason="loading" /> : <Suspense fallback={<LogoLoader data-app-reason="loading" />}>
88
  {/* C3 App-only props (S82-T68). Projects keeps New record. */}
89
  <CustomerGrid key={`${table}:${gridRevision}`} address={{ database: table }} hideViewRail
90
  hideModeSwitch
 
 
91
  appCellActions={{
92
  projects: row => setProjects(projectsTargetFromRow(table, row)),
93
  competitors: row => setCompetitors(competitorsTargetFromRow(table, row)),
 
4
  import { AMAZON_DATA_ENTRY_TABLE, AMAZON_PROFIT_TABLE, AmazonPeriodSelect, COMMAND_CENTER_TABLES,
5
  CommandCenterTabStrip, CommandCenterTableContext } from "../../customer-grid/AmazonPeriodPicker";
6
  import { OverlayProvider } from "../../customer-grid/OverlaySurface";
7
+ import { LP_BLUE_DEEP, LP_GREEN_DEEP } from "../../customer-grid/theme";
8
  import type { AppViewProps } from "../../customer-grid/viewModes";
9
  import { LogoLoader } from "../../ui/LogoLoader";
10
  import FbaCapacityControl from "./FbaCapacityControl";
 
17
  // Lazy, as every other host loads it: a static import pulls the grid into this App's chunk.
18
  const CustomerGrid = lazy(() => import("../../customer-grid/CustomerGrid"));
19
 
20
+ const PROJECT_PROGRESS_COLORS = { inProgress: LP_BLUE_DEEP, completed: LP_GREEN_DEEP };
21
+ const PROJECT_PROGRESS_TABLES = new Set([AMAZON_PROFIT_TABLE, "ut_amazon_asin_profit", "ut_amazon_parent_profit"]);
22
+
23
+ /** Admission belongs to the selected tab's fresh probe. */
24
+ type TableProbe = { table: string; status: "ready"; tables: Record<string, boolean> }
25
+ | { table: string; status: "failed" } | null;
26
 
27
  // The Command Center App body: the Table module's own Grid, hosted under the App frame.
28
  // The tab strip switches `table` through the context, so the URL stays on the App.
 
33
  const [projects, setProjects] = useState<ProjectsTarget | null>(null);
34
  const [competitors, setCompetitors] = useState<ProjectsTarget | null>(null);
35
  const [gridRevision, setGridRevision] = useState(0);
36
+ const [entryEmpty, setEntryEmpty] = useState<{ table: string; empty: boolean } | null>(null);
37
+ const openTable = useCallback((next: string) => {
38
+ if (next === table) return;
39
+ // Clear synchronously: returning to a tab must not reuse its earlier admission.
40
+ setProbe(null);
41
+ setEntryEmpty(null);
42
+ setPicture(null);
43
+ setProjects(null);
44
+ setCompetitors(null);
45
+ setTable(next);
46
+ }, [table]);
47
  const reloadGrid = useCallback(() => {
48
  for (const key of [AMAZON_PROFIT_TABLE, "ut_amazon_asin_profit", "ut_amazon_parent_profit"]) {
49
  clearTopicRowsCache(`tables/${key}/rows`);
 
56
  useEffect(() => {
57
  const controller = new AbortController();
58
  void fetch(`${API_V1}/amazon-profit/tables`, { credentials: CREDENTIALS, cache: "no-store",
59
+ signal: AbortSignal.any([controller.signal, AbortSignal.timeout(20000)]) })
60
  .then(async (res) => {
61
  if (!res.ok) throw new Error("probe refused");
62
  const body = await res.json() as { tables?: Record<string, boolean> };
63
+ if (!body.tables || typeof body.tables !== "object" || Array.isArray(body.tables)
64
+ || Object.values(body.tables).some(value => typeof value !== "boolean")) throw new Error("invalid probe");
65
+ if (!controller.signal.aborted) setProbe({ table, status: "ready", tables: body.tables });
66
  })
67
+ .catch(() => { if (!controller.signal.aborted) setProbe({ table, status: "failed" }); });
68
  return () => controller.abort();
69
  }, [table]);
70
+ const currentProbe = probe?.table === table ? probe : null;
71
+ const loading = currentProbe === null;
72
+ const probeFailed = currentProbe?.status === "failed";
73
+ const notBuilt = currentProbe?.status === "ready" && currentProbe.tables[table] === false;
74
+ const inaccessible = currentProbe?.status === "ready" && !Object.hasOwn(currentProbe.tables, table);
75
+ const admitted = currentProbe?.status === "ready" && currentProbe.tables[table] === true;
76
  const dataEntry = table === AMAZON_DATA_ENTRY_TABLE;
77
  // Only an empty, permitted row response proves every SKU has its data.
 
78
  useEffect(() => {
79
+ if (!dataEntry || !admitted) { setEntryEmpty(null); return; }
 
80
  const controller = new AbortController();
81
  void fetch(`${API_V1}/tables/${AMAZON_DATA_ENTRY_TABLE}/rows`, { credentials: CREDENTIALS,
82
  cache: "no-store", signal: AbortSignal.any([controller.signal, AbortSignal.timeout(20000)]) })
 
87
  })
88
  .catch(() => { if (!controller.signal.aborted) setEntryEmpty({ table, empty: false }); });
89
  return () => controller.abort();
90
+ }, [dataEntry, admitted, table]);
91
  const entryChecked = !dataEntry || notBuilt || entryEmpty?.table === table;
92
+ const allComplete = dataEntry && admitted && entryEmpty?.table === table && entryEmpty.empty;
93
  // A new key remounts the grid per table, as the Table module does on a database switch.
94
+ return <CommandCenterTableContext.Provider value={openTable}>
95
  <OverlayProvider>
96
+ {dataEntry && admitted && <FbaCapacityControl />}
97
+ {!admitted || allComplete
98
+ ? <div className="cc-not-built" data-testid={loading ? "command-center-loading" : probeFailed ? "command-center-probe-failed"
99
+ : inaccessible ? "command-center-refused" : notBuilt ? "command-center-not-built" : "command-center-complete"}
 
 
100
  style={{ display: "flex", flexDirection: "column", flex: "1 1 0", minHeight: 0, padding: "4px 12px 0" }}>
101
+ <CommandCenterTabStrip database={table} onOpenTable={openTable} />
102
+ {loading ? <LogoLoader data-app-reason="loading" />
103
+ : <p className="ap-state" role="status">{inaccessible ? ACCESS_REFUSED_MESSAGE : probeFailed ? "This table could not be checked. Switch tabs to try again."
104
+ : notBuilt ? "This table is not built yet. An admin Refresh builds it." : "Every SKU has its data."}</p>}
105
+ </div>
106
+ : table === "ut_amazon_projects" ? <div style={{ display: "flex", flexDirection: "column", flex: "1 1 0", minHeight: 0, padding: "4px 12px 0" }}>
107
+ <CommandCenterTabStrip database={table} onOpenTable={openTable} />
108
+ <ProjectsPage />
109
  </div>
110
  : !entryChecked ? <LogoLoader data-app-reason="loading" /> : <Suspense fallback={<LogoLoader data-app-reason="loading" />}>
111
  {/* C3 App-only props (S82-T68). Projects keeps New record. */}
112
  <CustomerGrid key={`${table}:${gridRevision}`} address={{ database: table }} hideViewRail
113
  hideModeSwitch
114
+ appHistoryInToolbar
115
+ appProgressColors={PROJECT_PROGRESS_TABLES.has(table) ? PROJECT_PROGRESS_COLORS : undefined}
116
  appCellActions={{
117
  projects: row => setProjects(projectsTargetFromRow(table, row)),
118
  competitors: row => setCompetitors(competitorsTargetFromRow(table, row)),
web/src/apps/amazon-profit/ProjectResults.tsx CHANGED
@@ -1,5 +1,8 @@
1
  import { useEffect, useRef, useState } from "react";
2
- import { API_V1, CREDENTIALS } from "../../apiContract";
 
 
 
3
 
4
  export type ResultMetric = "sales" | "units" | "returns" | "return_rate" | "refund_rate" | "roas";
5
  export interface ResultConfig {
@@ -23,73 +26,11 @@ interface Result {
23
  deltaPercent: number | null;
24
  }
25
  interface Answer { timezone: string; anchor: string | null; results: Result[] }
26
- const METRICS: Array<{ key: string; metric: ResultMetric; label: string; compare: "prior" | "yoy" }> = [
27
- { key: "sales", metric: "sales", label: "Sales", compare: "prior" },
28
- { key: "sales-yoy", metric: "sales", label: "Sales YoY", compare: "yoy" },
29
- { key: "return_rate", metric: "return_rate", label: "Returns %", compare: "prior" },
30
- { key: "units", metric: "units", label: "Units", compare: "prior" },
31
- { key: "returns", metric: "returns", label: "Returns", compare: "prior" },
32
- { key: "refund_rate", metric: "refund_rate", label: "Refunds %", compare: "prior" },
33
- { key: "roas", metric: "roas", label: "ROAS", compare: "prior" },
34
- ];
35
- const configKey = (config: ResultConfig) => config.metric === "sales" && config.compare === "yoy" ? "sales-yoy" : config.metric;
36
  const periodLabel = (period: WindowResult) => {
37
  const last = new Date(`${period.end_exclusive}T00:00:00Z`);
38
  last.setUTCDate(last.getUTCDate() - 1);
39
  return `${period.start} to ${last.toISOString().slice(0, 10)}`;
40
  };
41
- function figure(value: number | null, unit: string) {
42
- if (value === null || !Number.isFinite(value)) return "Unavailable";
43
- if (/^[A-Z]{3}$/.test(unit)) return value.toLocaleString("en-US", { style: "currency", currency: unit, maximumFractionDigits: 2 });
44
- return `${value.toLocaleString("en-US", { maximumFractionDigits: 2 })}${unit === "percent" ? "%" : unit === "ratio" ? "×" : ""}`;
45
- }
46
-
47
- function Trend({ result }: { result: Result }) {
48
- const current = result.current, comparison = result.comparison;
49
- if (!current || !comparison || result.state !== "ready") return <p className="lp-proj-muted" role="status">
50
- {result.state === "waiting_for_completion" ? "Complete the project to view results"
51
- : result.state === "waiting_for_window" || result.state === "waiting_for_completed_day"
52
- ? "Results appear when this period ends" : "Results unavailable"}
53
- </p>;
54
- const values = [...current.series, ...comparison.series].map(point => point.value)
55
- .filter((value): value is number => value !== null && Number.isFinite(value));
56
- if (!values.length) return <p className="lp-proj-muted" role="status">Results unavailable</p>;
57
- const low = Math.min(0, ...values), high = Math.max(1, ...values);
58
- const count = Math.max(current.series.length, comparison.series.length, 2);
59
- const x = (index: number) => 70 + index * 700 / (count - 1);
60
- const y = (value: number) => 180 - (value - low) * 150 / (high - low);
61
- const path = (points: Point[]) => {
62
- let joined = false;
63
- return points.map((point, index) => {
64
- if (point.value === null || !Number.isFinite(point.value)) { joined = false; return ""; }
65
- const command = joined ? "L" : "M";
66
- joined = true;
67
- return `${command}${x(index)},${y(point.value)}`;
68
- }).join(" ");
69
- };
70
- return <>
71
- <dl className="lp-proj-result-values">
72
- <div><dt>After completion</dt><dd>{figure(current.value, result.unit)}</dd></div>
73
- <div><dt>{result.config.compare === "yoy" ? "Prior year" : "Before completion"}</dt><dd>{figure(comparison.value, result.unit)}</dd></div>
74
- <div><dt>Change</dt><dd>{figure(result.delta, result.unit)}</dd></div>
75
- </dl>
76
- <svg className="lp-proj-trend" viewBox="0 0 800 220" role="img" aria-label={`${result.label} trend`}>
77
- <title>{result.label}: {periodLabel(current)} compared with {periodLabel(comparison)}</title>
78
- {[low, (high + low) / 2, high].map((value, index) => <g key={index}>
79
- <line x1="70" y1={y(value)} x2="770" y2={y(value)} className="lp-proj-trend-grid" />
80
- <text x="60" y={y(value) + 4} textAnchor="end">{figure(value, result.unit)}</text>
81
- </g>)}
82
- <path d={path(comparison.series)} className="lp-proj-trend-prior" />
83
- <path d={path(current.series)} className="lp-proj-trend-current" />
84
- <text x="70" y="207">Day 1</text><text x="770" y="207" textAnchor="end">Day {count}</text>
85
- </svg>
86
- <div className="lp-proj-trend-legend">
87
- <span><i className="lp-proj-trend-current" />After completion · {periodLabel(current)}</span>
88
- <span><i className="lp-proj-trend-prior" />{result.config.compare === "yoy" ? "Prior year" : "Before completion"} · {periodLabel(comparison)}</span>
89
- </div>
90
- </>;
91
- }
92
-
93
  export function ProjectResultPreview({ projectId, configs, revision, completedOn, onOpen }: {
94
  projectId: string; configs: ResultConfig[]; revision: string; completedOn: string | null; onOpen: () => void;
95
  }) {
@@ -139,91 +80,135 @@ export function ProjectResultPreview({ projectId, configs, revision, completedOn
139
  </div>;
140
  }
141
 
142
- export default function ProjectResults({ projectId, configs, completedOn, updatedAt, canEdit, onSaved }: {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  projectId: string; configs: ResultConfig[]; completedOn: string | null; updatedAt?: string;
144
  canEdit: boolean; onSaved: () => void;
 
145
  }) {
146
- const [draft, setDraft] = useState(configs);
147
- const [answer, setAnswer] = useState<Answer | null>(null);
148
- const [error, setError] = useState("");
149
- const [busy, setBusy] = useState(false);
150
- const [customPeriod, setCustomPeriod] = useState(false);
151
- const [moreMetrics, setMoreMetrics] = useState(false);
152
- const [days, setDays] = useState(configs[0]?.days ?? 7);
153
- const [window, setWindow] = useState<ResultConfig["window"]>(configs[0]?.window ?? "fixed");
154
- const configStamp = JSON.stringify(configs);
155
- useEffect(() => { setDraft(JSON.parse(configStamp) as ResultConfig[]); }, [configStamp]);
156
  useEffect(() => {
157
  const controller = new AbortController();
158
- setAnswer(null);
159
- setError("");
160
- void fetch(`${API_V1}/amazon-projects/${encodeURIComponent(projectId)}/results`, { credentials: CREDENTIALS,
161
  signal: AbortSignal.any([controller.signal, AbortSignal.timeout(20000)]) })
162
  .then(async response => {
163
- if (!response.ok) throw new Error("Results could not be loaded.");
164
- const body = await response.json() as Answer;
165
- if (!controller.signal.aborted) setAnswer(body);
166
- }).catch(() => { if (!controller.signal.aborted) setError("Results could not be loaded."); });
 
 
 
 
 
 
 
 
 
 
167
  return () => controller.abort();
168
- }, [projectId, configStamp, completedOn, updatedAt]);
169
- const dirty = JSON.stringify(draft) !== configStamp;
170
- const period = (nextWindow: ResultConfig["window"], nextDays: number) => {
171
- setWindow(nextWindow); setDays(nextDays);
172
- setDraft(current => current.map(config => ({ ...config, window: nextWindow, days: nextDays })));
173
- };
174
- const save = async () => {
175
- if (busy) return;
176
- setBusy(true); setError("");
 
177
  try {
178
- const response = await fetch(`${API_V1}/amazon-projects/${encodeURIComponent(projectId)}`, {
179
- method: "PATCH", credentials: CREDENTIALS, headers: { "Content-Type": "application/json" },
180
- signal: AbortSignal.timeout(20000), body: JSON.stringify({ resultConfigs: draft }),
181
- });
182
- if (!response.ok) throw new Error("The results were not saved.");
183
- onSaved();
184
- } catch { setError("The results were not saved."); }
185
- finally { setBusy(false); }
186
  };
187
- return <section className="lp-proj-section lp-proj-results" aria-label="Results">
188
- <h4 className="lp-proj-section-title">Results</h4>
189
- <div className="lp-proj-result-controls">
190
- <select className="lp-proj-input" aria-label="Result period" disabled={!canEdit || busy}
191
- value={window === "completion_to_now" ? "now" : [7, 30, 90].includes(days) ? String(days) : "custom"}
192
- onChange={event => {
193
- if (event.target.value === "custom") setCustomPeriod(true);
194
- period(event.target.value === "now" ? "completion_to_now" : "fixed",
195
- ["now", "custom"].includes(event.target.value) ? days : Number(event.target.value));
196
- }}>
197
- <option value="7">7 days before vs 7 days after completion</option>
198
- <option value="30">30 days before vs 30 days after completion</option>
199
- <option value="90">90 days before vs 90 days after completion</option>
200
- <option value="now">Completion to now</option><option value="custom">Custom period</option>
201
- </select>
202
- <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={!canEdit || busy}
203
- aria-expanded={customPeriod} onClick={() => {
204
- if (!customPeriod && window === "completion_to_now") period("fixed", days);
205
- setCustomPeriod(value => !value);
206
- }}>Custom period</button>
207
- {customPeriod ? <label className="lp-proj-result-days">Days<input className="lp-proj-input" type="number" aria-label="Result days"
208
- min={1} max={366} step={1} value={days} disabled={!canEdit || busy || window === "completion_to_now"}
209
- onChange={event => { const value = event.target.valueAsNumber; if (Number.isInteger(value) && value >= 1 && value <= 366) period("fixed", value); }} /></label> : null}
210
- </div>
211
- <div className="lp-proj-result-metrics" role="group" aria-label="Result metrics">
212
- {METRICS.filter(metric => moreMetrics || draft.some(config => configKey(config) === metric.key)).map(metric => <label key={metric.key}><input type="checkbox" disabled={!canEdit || busy}
213
- checked={draft.some(config => configKey(config) === metric.key)} onChange={event => setDraft(current => event.target.checked
214
- ? [...current, { id: crypto.randomUUID(), metric: metric.metric, grain: current[0]?.grain ?? "sku", window, days, compare: metric.compare }]
215
- : current.filter(config => configKey(config) !== metric.key))} />{metric.label}</label>)}
216
- {canEdit ? <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={busy} aria-expanded={moreMetrics}
217
- onClick={() => setMoreMetrics(value => !value)}>{moreMetrics ? "Done" : "Add metric"}</button> : null}
218
  </div>
219
- {dirty && canEdit ? <div className="lp-proj-form-actions">
220
- <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={busy} onClick={() => setDraft(configs)}>Cancel</button>
221
- <button type="button" className="lp-proj-btn lp-proj-btn-primary" disabled={busy} onClick={() => void save()}>{busy ? "Saving…" : "Save results"}</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  </div> : null}
223
- {error ? <p className="lp-proj-error" role="status">{error}</p> : null}
224
- {answer?.results.map(result => <div className="lp-proj-result" key={result.config.id}>
225
- <h5 className="lp-proj-section-title">{result.label}{result.config.compare === "yoy" ? " YoY" : ""}</h5>
226
- <Trend result={result} />
227
- </div>)}
228
  </section>;
229
  }
 
1
  import { useEffect, useRef, useState } from "react";
2
+ import { API_V1, CREDENTIALS, PRINCIPAL_HEADER, TENANT_HEADER, checkTenant, currentTenant } from "../../apiContract";
3
+ import { refusalMessage } from "../../customer-grid/apiBridge";
4
+ import { LineChart, formatMeasure } from "../../viz/kit";
5
+ import "../../styles/sdlc84/project-results.css";
6
 
7
  export type ResultMetric = "sales" | "units" | "returns" | "return_rate" | "refund_rate" | "roas";
8
  export interface ResultConfig {
 
26
  deltaPercent: number | null;
27
  }
28
  interface Answer { timezone: string; anchor: string | null; results: Result[] }
 
 
 
 
 
 
 
 
 
 
29
  const periodLabel = (period: WindowResult) => {
30
  const last = new Date(`${period.end_exclusive}T00:00:00Z`);
31
  last.setUTCDate(last.getUTCDate() - 1);
32
  return `${period.start} to ${last.toISOString().slice(0, 10)}`;
33
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  export function ProjectResultPreview({ projectId, configs, revision, completedOn, onOpen }: {
35
  projectId: string; configs: ResultConfig[]; revision: string; completedOn: string | null; onOpen: () => void;
36
  }) {
 
80
  </div>;
81
  }
82
 
83
+ const CHARTS = [
84
+ { key: "sales", label: "Sales" },
85
+ { key: "units", label: "Units" },
86
+ { key: "margin", label: "Margin" },
87
+ { key: "returns", label: "Returns" },
88
+ { key: "returns_pct", label: "Returns %" },
89
+ { key: "refunds_pct", label: "Refunds %" },
90
+ { key: "roas", label: "ROAS" },
91
+ { key: "sales_yoy", label: "Sales YoY" },
92
+ ] as const;
93
+ type ChartKey = typeof CHARTS[number]["key"];
94
+ interface SeriesMetric {
95
+ key: ChartKey;
96
+ label: string;
97
+ unit: string;
98
+ points: Point[];
99
+ total: number | null;
100
+ tie: { ok: boolean; ours: number | null; independent: number | null };
101
+ reason: string | null;
102
+ }
103
+ interface SeriesAnswer {
104
+ projectId: string;
105
+ from: string;
106
+ to: string;
107
+ markers: { start: string | null; target: string | null; completed: string | null };
108
+ metrics: SeriesMetric[];
109
+ }
110
+ interface ChartVisibility { projectId: string; storageKey: string; hidden: ChartKey[] }
111
+
112
+ function readHiddenCharts(storageKey: string): ChartKey[] {
113
+ try {
114
+ const value: unknown = JSON.parse(localStorage.getItem(storageKey) ?? "[]");
115
+ return Array.isArray(value) ? CHARTS.filter(chart => value.includes(chart.key)).map(chart => chart.key) : [];
116
+ } catch { return []; }
117
+ }
118
+
119
+ export default function ProjectResults({ projectId, configs, completedOn, updatedAt, scope }: {
120
  projectId: string; configs: ResultConfig[]; completedOn: string | null; updatedAt?: string;
121
  canEdit: boolean; onSaved: () => void;
122
+ scope?: { grain: "sku" | "asin" | "parent"; keys: string[] };
123
  }) {
124
+ const [retry, setRetry] = useState(0);
125
+ const [loaded, setLoaded] = useState<{ request: string; answer: SeriesAnswer | null; error: string } | null>(null);
126
+ const [visibility, setVisibility] = useState<ChartVisibility | null>(null);
127
+ const [storageError, setStorageError] = useState("");
128
+ const host = useRef<HTMLElement>(null);
129
+ const scopeValue = scope ? `${scope.grain}:${scope.keys.join(",")}` : "all";
130
+ const request = JSON.stringify([projectId, configs, completedOn, updatedAt, scopeValue, retry]);
 
 
 
131
  useEffect(() => {
132
  const controller = new AbortController();
133
+ // C3 owns the default dates and the tenant's completed-day boundary.
134
+ const query = new URLSearchParams({ scope: scopeValue });
135
+ void fetch(`${API_V1}/amazon-projects/${encodeURIComponent(projectId)}/series?${query}`, { credentials: CREDENTIALS,
136
  signal: AbortSignal.any([controller.signal, AbortSignal.timeout(20000)]) })
137
  .then(async response => {
138
+ if (controller.signal.aborted || checkTenant(response)) return;
139
+ const storageKey = `aios-project-result-charts:${JSON.stringify([
140
+ response.headers.get(TENANT_HEADER) ?? currentTenant(), response.headers.get(PRINCIPAL_HEADER), projectId,
141
+ ])}`;
142
+ setVisibility(previous => previous?.storageKey === storageKey ? previous
143
+ : { projectId, storageKey, hidden: readHiddenCharts(storageKey) });
144
+ const body = await response.json().catch(() => null);
145
+ if (!response.ok) throw new Error(refusalMessage(body, response.status, "Results could not be loaded"));
146
+ if (!body || body.projectId !== projectId || !Array.isArray(body.metrics)) throw new Error("Results could not be loaded");
147
+ if (!controller.signal.aborted) setLoaded({ request, answer: body as SeriesAnswer, error: "" });
148
+ }).catch(error => {
149
+ if (!controller.signal.aborted) setLoaded({ request, answer: null,
150
+ error: error instanceof Error ? error.message : "Results could not be loaded" });
151
+ });
152
  return () => controller.abort();
153
+ }, [projectId, scopeValue, request]);
154
+ const current = loaded?.request === request ? loaded : null;
155
+ const answer = current?.answer;
156
+ const loading = current === null;
157
+ const hidden = visibility?.projectId === projectId ? visibility.hidden : [];
158
+ const charts = CHARTS.filter(chart => !hidden.includes(chart.key));
159
+ const period = answer ? `${answer.from} to ${answer.to}` : "";
160
+ const changeHidden = (next: ChartKey[], focusIndex = 0) => {
161
+ if (!visibility || visibility.projectId !== projectId) return;
162
+ setVisibility({ ...visibility, hidden: next });
163
  try {
164
+ localStorage.setItem(visibility.storageKey, JSON.stringify(next));
165
+ setStorageError("");
166
+ } catch { setStorageError("Chart visibility lasts for this visit"); }
167
+ requestAnimationFrame(() => {
168
+ const controls = host.current?.querySelectorAll<HTMLButtonElement>("[data-chart-visibility]");
169
+ controls?.[Math.min(focusIndex, controls.length - 1)]?.focus();
170
+ });
 
171
  };
172
+ return <section ref={host} className="lp-proj-section lp-proj-results" aria-label="Results" aria-busy={loading}>
173
+ <div className="lp-proj-series-heading">
174
+ <h4 className="lp-proj-section-title">Results</h4>
175
+ {hidden.length > 0 && charts.length > 0 ? <button type="button" className="lp-proj-series-action" data-chart-visibility
176
+ onClick={() => changeHidden([])}>Show all charts</button> : null}
177
+ {current?.error ? <button type="button" className="lp-proj-series-action" onClick={() => setRetry(value => value + 1)}>Retry</button> : null}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  </div>
179
+ {storageError ? <p className="lp-proj-series-notice" role="status">{storageError}</p> : null}
180
+ {charts.length === 0 ? <div className="lp-proj-series-empty">
181
+ <p role="status">All charts are hidden</p>
182
+ <button type="button" className="lp-proj-series-action" data-chart-visibility onClick={() => changeHidden([])}>Show all charts</button>
183
+ </div> : null}
184
+ {charts.length > 0 ? <div className="lp-proj-series-stack">
185
+ {charts.map((chart, index) => {
186
+ const metric = answer?.metrics.find(item => item.key === chart.key);
187
+ const label = metric?.label || chart.label;
188
+ const available = metric !== undefined && metric.total !== null;
189
+ const reason = current?.error || metric?.reason || (!metric && !loading ? "This metric was not returned" : "");
190
+ return <article key={chart.key} className="lp-proj-series-card" data-metric={chart.key} aria-label={`${label} chart`}>
191
+ <header className="lp-proj-series-head">
192
+ <div className="lp-proj-series-summary">
193
+ <h5>{label}</h5>
194
+ {loading ? <span className="lp-proj-series-value-skeleton" aria-hidden="true" />
195
+ : <span className="lp-proj-series-total">{available
196
+ ? `${formatMeasure(metric.total, metric.unit)}${metric.unit === "ratio" ? "×" : ""}` : "Unavailable"}</span>}
197
+ {period ? <span className="lp-proj-series-period">{period}</span> : null}
198
+ </div>
199
+ <button type="button" className="lp-proj-series-action" data-chart-visibility aria-label={`Hide ${label} chart`}
200
+ disabled={visibility?.projectId !== projectId} onClick={() => changeHidden([...hidden, chart.key], index + (hidden.length > 0 ? 1 : 0))}>Hide</button>
201
+ </header>
202
+ {loading ? <div className="lp-proj-series-skeleton" role="status" aria-label={`Loading ${label} chart`} />
203
+ : <>
204
+ {available ? <LineChart label={label} unit={metric.unit}
205
+ series={[{ key: chart.key, label, points: metric.points }]} /> : null}
206
+ {reason || !available ? <p className="lp-proj-series-notice" role="status">
207
+ {reason || "This metric is unavailable"}
208
+ </p> : null}
209
+ </>}
210
+ </article>;
211
+ })}
212
  </div> : null}
 
 
 
 
 
213
  </section>;
214
  }
web/src/apps/amazon-profit/Projects.tsx CHANGED
@@ -1,10 +1,10 @@
1
  import { createContext, useContext, useCallback, useEffect, useId, useMemo, useRef, useState, type FormEvent, type ReactNode } from "react";
2
  import { createPortal } from "react-dom";
3
- import { ArrowRight } from "@phosphor-icons/react/dist/csr/ArrowRight";
4
  import { CellSignalHigh } from "@phosphor-icons/react/dist/csr/CellSignalHigh";
5
  import { CellSignalMedium } from "@phosphor-icons/react/dist/csr/CellSignalMedium";
6
  import { CellSignalLow } from "@phosphor-icons/react/dist/csr/CellSignalLow";
7
  import { Check } from "@phosphor-icons/react/dist/csr/Check";
 
8
  import { CircleDashed } from "@phosphor-icons/react/dist/csr/CircleDashed";
9
  import { Cube } from "@phosphor-icons/react/dist/csr/Cube";
10
  import { DotsThree } from "@phosphor-icons/react/dist/csr/DotsThree";
@@ -21,7 +21,7 @@ import { SidebarSimple } from "@phosphor-icons/react/dist/csr/SidebarSimple";
21
  import { API_V1, CREDENTIALS, TOAST_EVENT, signal } from "../../apiContract";
22
  import { refusalMessage } from "../../customer-grid/apiBridge";
23
  import { assetUrl } from "../../customer-grid/catalogData";
24
- import { BodyPortal, useOverlayLayer } from "../../customer-grid/OverlaySurface";
25
  import "../../styles/sdlc82/amazon-projects.css";
26
  import ProjectResults, { ProjectResultPreview } from "./ProjectResults";
27
  import type { ResultConfig } from "./ProjectResults";
@@ -34,6 +34,11 @@ export const PROJECT_PRIORITIES = ["No priority", "Urgent", "High", "Medium", "L
34
  type Status = (typeof PROJECT_STATUSES)[number];
35
  type Priority = (typeof PROJECT_PRIORITIES)[number];
36
  type Health = "On track" | "At risk" | "Off track" | null;
 
 
 
 
 
37
 
38
  export interface Project {
39
  id: string;
@@ -103,6 +108,7 @@ interface ProjectInput {
103
  ownerUsername?: string | null;
104
  startOn?: string | null;
105
  targetOn?: string | null;
 
106
  progress?: number;
107
  notes?: string | null;
108
  targets?: Array<{ grain: ProjectGrain; key: string }>;
@@ -111,8 +117,9 @@ interface ProjectInput {
111
  type ProjectDraft = Required<Omit<ProjectInput, "targets">>;
112
 
113
  function draftOf(project: Project): ProjectDraft {
114
- const { name, status, priority, owner, startOn, targetOn, progress, notes } = project;
115
- return { name, status, priority, owner, ownerUsername: project.ownerUsername ?? null, startOn, targetOn, progress, notes };
 
116
  }
117
 
118
  const BASE = `${API_V1}/amazon-projects`;
@@ -129,27 +136,40 @@ const FIELD_LABEL: Record<string, string> = {
129
  };
130
  const enc = encodeURIComponent;
131
 
132
- function OwnerSelect({ value, legacy, disabled, onChange }: {
133
  value: string | null; legacy?: string | null; disabled: boolean; onChange: (value: string | null) => void;
 
134
  }) {
135
  const [owners, setOwners] = useState<Array<{ username: string; name: string }>>([]);
136
  const [failed, setFailed] = useState(false);
 
 
137
  useEffect(() => {
138
  const controller = new AbortController();
 
 
139
  void call<{ owners: Array<{ username: string; name: string }> }>("/owners", { signal: controller.signal })
140
  .then(answer => { if (!controller.signal.aborted) setOwners(answer.owners); })
141
- .catch(() => { if (!controller.signal.aborted) setFailed(true); });
 
142
  return () => controller.abort();
143
- }, []);
144
  const selected = value || (legacy ? "__legacy__" : "");
145
- return <select className="lp-proj-input" aria-label="Owner" value={selected} disabled={disabled || failed}
 
146
  onChange={event => onChange(event.target.value || null)}>
147
- <option value="">Unassigned</option>
148
  {legacy && !value ? <option value="__legacy__" disabled>{legacy}</option> : null}
149
  {value && !owners.some(owner => owner.username === value)
150
  ? <option value={value} disabled>{legacy || "Previous owner"}</option> : null}
151
  {owners.map(owner => <option key={owner.username} value={owner.username}>{owner.name}</option>)}
152
- </select>;
 
 
 
 
 
 
153
  }
154
 
155
  class ProjectRequestError extends Error {
@@ -259,9 +279,9 @@ function StatusRing({ status, progress }: { status: Status; progress: number })
259
  const share = clampPercent(progress) / 100;
260
  if (status === "Completed" || status === "Canceled" || status === "Backlog") {
261
  const Icon = status === "Completed" ? Check : status === "Canceled" ? Prohibit : CircleDashed;
262
- return <Icon className="lp-proj-ring" data-status={status} size={14} aria-hidden="true" />;
263
  }
264
- return <svg className="lp-proj-ring" data-status={status} width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">
265
  <circle cx="7" cy="7" r={radius} fill="none" stroke="currentColor" strokeWidth="1.5"
266
  opacity={status === "Planned" ? 1 : 0.35} />
267
  <circle cx="7" cy="7" r={radius} fill="none" stroke="currentColor" strokeWidth="1.5"
@@ -270,12 +290,73 @@ function StatusRing({ status, progress }: { status: Status; progress: number })
270
  </svg>;
271
  }
272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  function HealthGlyph({ health }: { health: Health }) {
274
  if (!health) return null;
275
  const Icon = health === "On track" ? TrendUp : health === "At risk" ? Minus : TrendDown;
276
  return <Icon className="lp-proj-health" data-health={health} size={14} aria-hidden="true" />;
277
  }
278
 
 
 
 
 
 
 
279
  function PriorityGlyph({ priority }: { priority: Priority }) {
280
  const Icon = priority === "Urgent" ? Warning : priority === "High" ? CellSignalHigh
281
  : priority === "Medium" ? CellSignalMedium : priority === "Low" ? CellSignalLow : DotsThree;
@@ -408,7 +489,8 @@ function ProjectTable({ projects, grain, onOpen, empty, visible = DEFAULT_PROJEC
408
  <tbody>
409
  {projects.map((project) => {
410
  const via = project.via && grain && (project.via.grain !== grain) ? project.via : null;
411
- return <tr key={project.id} className="lp-proj-row" data-project-id={project.id} onClick={() => onOpen(project.id)}>
 
412
  <td><div className="lp-proj-name-wrap">
413
  <button type="button" className="lp-proj-name" onClick={(event) => { event.stopPropagation(); onOpen(project.id); }}>
414
  <Cube className="lp-proj-name-glyph" size={14} aria-hidden="true" />
@@ -420,8 +502,7 @@ function ProjectTable({ projects, grain, onOpen, empty, visible = DEFAULT_PROJEC
420
  let cell: ReactNode;
421
  switch (key) {
422
  case "health":
423
- cell = <span className="lp-proj-cell"><HealthGlyph health={project.health} />
424
- {project.health ? <span>{project.health}</span> : null}</span>;
425
  break;
426
  case "priority":
427
  cell = <span className="lp-proj-cell"><PriorityGlyph priority={project.priority} />
@@ -437,7 +518,7 @@ function ProjectTable({ projects, grain, onOpen, empty, visible = DEFAULT_PROJEC
437
  cell = formatDay(project[key]) || <span className="lp-proj-muted">No date</span>;
438
  break;
439
  case "status":
440
- cell = <span className="lp-proj-cell"><StatusRing status={project.status} progress={project.progress} />{project.status}</span>;
441
  break;
442
  case "results":
443
  cell = <ProjectResultPreview projectId={project.id} configs={project.resultConfigs ?? []}
@@ -583,7 +664,7 @@ function ProjectBrowser({ read, grain, onOpen }: {
583
  <div id={`${id}-summary`} role="tabpanel" aria-labelledby={`${id}-${tab}`} title="Counts follow the other filters">
584
  {tab === "Health" ? healthCounts.map(({ label, count }) => <button type="button" className="lp-proj-summary-row" key={label}
585
  aria-pressed={health === label} onClick={() => setHealth(health === label ? "" : label)}>
586
- <span><HealthGlyph health={label === "No health" ? null : label} />{label}</span><span>{count}</span>
587
  </button>) : ownerCounts.map(({ value, count }) => <button type="button" className="lp-proj-summary-row" key={ownerKey(value)}
588
  aria-pressed={owner === ownerKey(value)} onClick={() => setOwner(owner === ownerKey(value) ? "" : ownerKey(value))}>
589
  <Owner name={value} /><span>{count}</span>
@@ -595,7 +676,46 @@ function ProjectBrowser({ read, grain, onOpen }: {
595
  </section>;
596
  }
597
 
598
- // ---- One project: the detail reference's dates pair and progress, then notes, SKUs, activity ----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
 
600
  function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }: {
601
  id: string; canEdit: boolean; onChanged: () => void; onDeleted: () => void; onCancel: () => void;
@@ -607,22 +727,19 @@ function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }:
607
  const [busy, setBusy] = useState(false);
608
  const [adding, setAdding] = useState(false);
609
  const [confirming, setConfirming] = useState(false);
610
- const [editingDates, setEditingDates] = useState(false);
611
- const dateEditorId = useId();
 
 
612
  const formId = useId();
613
- const startDateRef = useRef<HTMLInputElement>(null);
614
  const writing = useRef(false);
615
- const mainRef = useRef<HTMLDivElement>(null);
616
  const loaded = detail !== null;
617
 
618
  useEffect(() => {
619
- if (loaded) mainRef.current?.querySelector<HTMLElement>("input, h3")?.focus();
620
  }, [loaded]);
621
 
622
- useEffect(() => {
623
- if (editingDates) startDateRef.current?.focus();
624
- }, [editingDates]);
625
-
626
  useEffect(() => {
627
  const controller = new AbortController();
628
  call<Detail>(`/${enc(id)}`, { signal: controller.signal }, "This project did not load.")
@@ -636,7 +753,8 @@ function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }:
636
  return () => controller.abort();
637
  }, [id, tick]);
638
 
639
- const write = useCallback(async (path: string, method: string, body: unknown, after?: () => void) => {
 
640
  if (writing.current || !mayEdit || !detail?.canEdit) return;
641
  writing.current = true;
642
  setBusy(true);
@@ -648,10 +766,19 @@ function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }:
648
  if (method === "PATCH") setDraft(draftOf(project));
649
  }
650
  setError("");
 
 
651
  if (after) after(); else setTick((value) => value + 1);
652
  onChanged();
653
  } catch (reason) {
654
- setError(messageOf(reason, "The change was not saved."));
 
 
 
 
 
 
 
655
  if (reason instanceof ProjectRequestError && reason.status === 403) {
656
  setDetail((current) => current ? { ...current, canEdit: false } : current);
657
  onChanged();
@@ -662,105 +789,158 @@ function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }:
662
  }
663
  }, [detail?.canEdit, mayEdit, onChanged]);
664
 
665
- const patch = useCallback((change: ProjectInput) => write(`/${enc(id)}`, "PATCH", change), [id, write]);
666
-
667
- if (!detail || !draft) return error ? <p className="lp-proj-error" role="alert">{error}</p> : <LoadingRows />;
668
- const { project, targets, activity } = detail;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
669
  const canEdit = mayEdit && detail.canEdit;
670
  const display = canEdit ? draft : project;
671
  const progress = Number.isFinite(display.progress) ? clampPercent(display.progress) : 0;
672
  const dirty = JSON.stringify(draft) !== JSON.stringify(draftOf(project));
673
- const set = <K extends keyof ProjectDraft>(key: K, value: ProjectDraft[K]) =>
674
- setDraft((current) => current ? { ...current, [key]: value } : current);
 
 
 
 
 
 
675
  const save = (event: FormEvent) => {
676
  event.preventDefault();
677
  if (!canEdit || !dirty) return;
678
  const name = draft.name.trim();
679
- if (!name) { setError("Name the project."); return; }
680
  if (!Number.isInteger(draft.progress) || draft.progress < 0 || draft.progress > 100) {
681
- setError("Enter a whole percent from 0 to 100."); return;
 
 
 
 
 
 
 
682
  }
683
- const { owner, ownerUsername, ...fields } = draft;
684
- void patch({ ...fields, name, notes: draft.notes?.trim() || null,
685
- ...(!ownerUsername && owner ? {} : { ownerUsername }) });
 
 
 
 
 
 
686
  };
687
 
688
- return <form id={formId} className="lp-proj-detail" data-can-edit={canEdit ? "true" : "false"} onSubmit={save}>
689
- <div className="lp-proj-main" ref={mainRef}>
690
- {canEdit
691
- ? <input className="lp-proj-title-input" value={draft.name} aria-label="Project name" disabled={busy}
692
- required maxLength={300} onChange={(event) => set("name", event.target.value)} />
693
- : <h3 className="lp-proj-title" tabIndex={-1}>{project.name}</h3>}
694
-
695
- <div className="lp-proj-props" role="group" aria-label="Properties">
696
- <span className="lp-proj-props-label">Properties</span>
697
- <div className="lp-proj-props-row">
698
- <span className="lp-proj-chip"><StatusRing status={display.status} progress={progress} />
699
- {canEdit
700
- ? <select aria-label="Status" value={draft.status} disabled={busy}
701
- onChange={(event) => set("status", event.target.value as Status)}>
702
- {PROJECT_STATUSES.map((value) => <option key={value} value={value}>{value}</option>)}
703
- </select>
704
- : <span>{project.status}</span>}
705
- </span>
706
- <span className="lp-proj-chip"><PriorityGlyph priority={display.priority} />
 
 
 
 
707
  {canEdit
708
- ? <select aria-label="Priority" value={draft.priority} disabled={busy}
 
709
  onChange={(event) => set("priority", event.target.value as Priority)}>
710
- {PROJECT_PRIORITIES.map((value) => <option key={value} value={value}>{value}</option>)}
711
  </select>
712
  : <span>{project.priority}</span>}
713
- </span>
714
- <span className="lp-proj-chip">
715
- {canEdit
716
- ? <OwnerSelect value={draft.ownerUsername} legacy={draft.owner} disabled={busy}
717
- onChange={value => setDraft(current => current ? { ...current, ownerUsername: value, owner: null } : current)} />
718
- : <Owner name={project.owner} />}
719
- </span>
720
- <span className="lp-proj-chip lp-proj-dates" data-dates="start-target">
721
- {canEdit && editingDates
722
- ? <span className="lp-proj-date-editor" id={dateEditorId}>
723
- <input ref={startDateRef} type="date" aria-label="Start date" value={draft.startOn ?? ""} disabled={busy}
724
- onChange={(event) => set("startOn", event.target.value || null)} />
725
- <ArrowRight size={14} aria-hidden="true" />
726
- <input type="date" aria-label="Target date" value={draft.targetOn ?? ""} disabled={busy}
727
- onChange={(event) => set("targetOn", event.target.value || null)} />
728
- </span>
729
- : <span className="lp-proj-date-labels">
730
- <time dateTime={display.startOn || undefined} title={display.startOn || "No start date"}>
731
- {formatDay(display.startOn) || "No start"}</time>
732
- <ArrowRight size={14} aria-hidden="true" />
733
- <time dateTime={display.targetOn || undefined} title={display.targetOn || "No target date"}>
734
- {formatDay(display.targetOn) || "No target"}</time>
735
- </span>}
736
- {canEdit ? <button type="button" className="lp-proj-date-toggle" disabled={busy}
737
- aria-expanded={editingDates} aria-controls={editingDates ? dateEditorId : undefined}
738
- onClick={() => setEditingDates((current) => !current)}>{editingDates ? "Done" : "Edit dates"}</button> : null}
739
- </span>
740
- {project.health ? <span className="lp-proj-chip"><HealthGlyph health={project.health} />{project.health}</span> : null}
741
- </div>
 
 
 
 
 
 
 
742
  </div>
743
 
744
  {error ? <p className="lp-proj-error" role="alert">{error}</p> : null}
745
  {detail.cause ? <p className="lp-proj-muted" role="status">{detail.cause}</p> : null}
746
-
747
- <ProjectResults projectId={id} configs={project.resultConfigs ?? []} completedOn={project.completedOn}
748
- updatedAt={project.updatedAt} canEdit={canEdit} onSaved={() => { setTick(value => value + 1); onChanged(); }} />
749
-
750
- <section className="lp-proj-section" aria-label="Notes">
751
- <h4 className="lp-proj-section-title">Notes</h4>
752
- {canEdit
753
- ? <textarea className="lp-proj-notes" aria-label="Notes" placeholder="Add notes" value={draft.notes ?? ""}
754
- rows={4} disabled={busy}
755
- onChange={(event) => set("notes", event.target.value)} />
756
- : <p className="lp-proj-notes-text">{project.notes || <span className="lp-proj-muted">No notes</span>}</p>}
757
- </section>
758
-
759
  <section className="lp-proj-section" aria-label="SKUs">
760
  <div className="lp-proj-section-head">
761
- <h4 className="lp-proj-section-title">SKUs <span className="lp-proj-muted">{targets.length}</span></h4>
762
  {canEdit && !adding
763
- ? <button type="button" className="lp-proj-btn" disabled={busy} onClick={() => setAdding(true)}>Add SKUs</button>
764
  : null}
765
  </div>
766
  {canEdit && adding
@@ -780,40 +960,29 @@ function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }:
780
  : null}
781
  </li>)}
782
  </ul>
783
- : <p className="lp-proj-muted">No SKUs yet</p>}
784
  </section>
 
 
 
 
 
 
 
 
 
 
785
  </div>
786
 
787
  <aside className="lp-proj-side">
788
- <section className="lp-proj-card" aria-label="Dates">
789
- <h4 className="lp-proj-section-title">Dates</h4>
790
- <dl className="lp-proj-facts">
791
- <div><dt>Start</dt><dd>{formatDay(display.startOn) || "Not set"}</dd></div>
792
- <div><dt>Target</dt><dd>{formatDay(display.targetOn) || "Not set"}</dd></div>
793
- {project.completedOn ? <div><dt>Completed</dt><dd>{formatDay(project.completedOn)}</dd></div> : null}
794
- </dl>
795
- </section>
796
-
797
- <section className="lp-proj-card" aria-label="Activity">
798
- <h4 className="lp-proj-section-title">Activity</h4>
799
- {activity.length
800
- ? <ol className="lp-proj-activity">
801
- {activity.map((entry, index) => <li key={`${entry.at}:${index}`}>
802
- <span className="lp-proj-activity-dot" aria-hidden="true" />
803
- <span>{activityLine(entry)} <span className="lp-proj-muted">{formatWhen(entry.at)}</span></span>
804
- </li>)}
805
- </ol>
806
- : <p className="lp-proj-muted">No activity yet</p>}
807
- </section>
808
-
809
- <ProjectFooter><div className="lp-proj-save">
810
  <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={busy} onClick={onCancel}>Cancel</button>
811
  {canEdit ? <button type="submit" form={formId} className="lp-proj-btn lp-proj-btn-primary" disabled={busy || !dirty}>
812
  {busy ? "Saving…" : "Save changes"}
813
  </button> : null}
814
  {canEdit && dirty ? <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={busy}
815
- onClick={() => { setDraft(draftOf(project)); setError(""); }}>Discard changes</button> : null}
816
- </div></ProjectFooter>
817
  {canEdit
818
  ? <div className="lp-proj-delete">
819
  {confirming
@@ -827,6 +996,35 @@ function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }:
827
  : null}
828
  </aside>
829
  </form>;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
830
  }
831
 
832
  function TargetLine({ target }: { target: Target }) {
@@ -935,7 +1133,8 @@ function ProjectForm({ targets, onCreated, onCancel, onForbidden }: {
935
  onCancel: () => void; onForbidden: () => void;
936
  }) {
937
  const [draft, setDraft] = useState<ProjectDraft>({
938
- name: "", status: "Planned", priority: "Medium", owner: "", ownerUsername: null, startOn: "", targetOn: "", progress: 0, notes: "",
 
939
  });
940
  const formId = useId();
941
  const [busy, setBusy] = useState(false);
@@ -973,10 +1172,8 @@ function ProjectForm({ targets, onCreated, onCancel, onForbidden }: {
973
  <label className="lp-proj-field lp-proj-field-wide"><span>Project name</span>
974
  <input className="lp-proj-input" value={draft.name} onChange={(event) => set("name", event.target.value)}
975
  placeholder="Name your project" autoFocus required maxLength={300} disabled={busy} /></label>
976
- <label className="lp-proj-field"><span>Status</span>
977
- <select className="lp-proj-input" value={draft.status} disabled={busy} onChange={(event) => set("status", event.target.value as Status)}>
978
- {PROJECT_STATUSES.map((value) => <option key={value} value={value}>{value}</option>)}
979
- </select></label>
980
  <label className="lp-proj-field"><span>Owner</span>
981
  <OwnerSelect value={draft.ownerUsername} disabled={busy} onChange={value => set("ownerUsername", value)} /></label>
982
  <label className="lp-proj-field"><span>Priority</span>
 
1
  import { createContext, useContext, useCallback, useEffect, useId, useMemo, useRef, useState, type FormEvent, type ReactNode } from "react";
2
  import { createPortal } from "react-dom";
 
3
  import { CellSignalHigh } from "@phosphor-icons/react/dist/csr/CellSignalHigh";
4
  import { CellSignalMedium } from "@phosphor-icons/react/dist/csr/CellSignalMedium";
5
  import { CellSignalLow } from "@phosphor-icons/react/dist/csr/CellSignalLow";
6
  import { Check } from "@phosphor-icons/react/dist/csr/Check";
7
+ import { CaretDown } from "@phosphor-icons/react/dist/csr/CaretDown";
8
  import { CircleDashed } from "@phosphor-icons/react/dist/csr/CircleDashed";
9
  import { Cube } from "@phosphor-icons/react/dist/csr/Cube";
10
  import { DotsThree } from "@phosphor-icons/react/dist/csr/DotsThree";
 
21
  import { API_V1, CREDENTIALS, TOAST_EVENT, signal } from "../../apiContract";
22
  import { refusalMessage } from "../../customer-grid/apiBridge";
23
  import { assetUrl } from "../../customer-grid/catalogData";
24
+ import { AnchoredOverlay, BodyPortal, useOverlayLayer } from "../../customer-grid/OverlaySurface";
25
  import "../../styles/sdlc82/amazon-projects.css";
26
  import ProjectResults, { ProjectResultPreview } from "./ProjectResults";
27
  import type { ResultConfig } from "./ProjectResults";
 
34
  type Status = (typeof PROJECT_STATUSES)[number];
35
  type Priority = (typeof PROJECT_PRIORITIES)[number];
36
  type Health = "On track" | "At risk" | "Off track" | null;
37
+ const STATUS_GROUPS: Array<{ label: string; statuses: readonly Status[] }> = [
38
+ { label: "Not started", statuses: ["Backlog", "Planned"] },
39
+ { label: "Active", statuses: ["In progress"] },
40
+ { label: "Done", statuses: ["Completed", "Canceled"] },
41
+ ];
42
 
43
  export interface Project {
44
  id: string;
 
108
  ownerUsername?: string | null;
109
  startOn?: string | null;
110
  targetOn?: string | null;
111
+ completedOn?: string | null;
112
  progress?: number;
113
  notes?: string | null;
114
  targets?: Array<{ grain: ProjectGrain; key: string }>;
 
117
  type ProjectDraft = Required<Omit<ProjectInput, "targets">>;
118
 
119
  function draftOf(project: Project): ProjectDraft {
120
+ const { name, status, priority, owner, startOn, targetOn, completedOn, progress, notes } = project;
121
+ return { name, status, priority, owner, ownerUsername: project.ownerUsername ?? null,
122
+ startOn, targetOn, completedOn, progress, notes };
123
  }
124
 
125
  const BASE = `${API_V1}/amazon-projects`;
 
136
  };
137
  const enc = encodeURIComponent;
138
 
139
+ function OwnerSelect({ value, legacy, disabled, onChange, label = "Owner", emptyLabel = "Unassigned" }: {
140
  value: string | null; legacy?: string | null; disabled: boolean; onChange: (value: string | null) => void;
141
+ label?: string; emptyLabel?: string;
142
  }) {
143
  const [owners, setOwners] = useState<Array<{ username: string; name: string }>>([]);
144
  const [failed, setFailed] = useState(false);
145
+ const [loading, setLoading] = useState(true);
146
+ const [attempt, setAttempt] = useState(0);
147
  useEffect(() => {
148
  const controller = new AbortController();
149
+ setLoading(true);
150
+ setFailed(false);
151
  void call<{ owners: Array<{ username: string; name: string }> }>("/owners", { signal: controller.signal })
152
  .then(answer => { if (!controller.signal.aborted) setOwners(answer.owners); })
153
+ .catch(() => { if (!controller.signal.aborted) setFailed(true); })
154
+ .finally(() => { if (!controller.signal.aborted) setLoading(false); });
155
  return () => controller.abort();
156
+ }, [attempt]);
157
  const selected = value || (legacy ? "__legacy__" : "");
158
+ return <><select className="lp-proj-input" aria-label={label} value={selected} disabled={disabled || failed || loading}
159
+ aria-busy={loading}
160
  onChange={event => onChange(event.target.value || null)}>
161
+ <option value="">{loading ? "Loading assignees…" : emptyLabel}</option>
162
  {legacy && !value ? <option value="__legacy__" disabled>{legacy}</option> : null}
163
  {value && !owners.some(owner => owner.username === value)
164
  ? <option value={value} disabled>{legacy || "Previous owner"}</option> : null}
165
  {owners.map(owner => <option key={owner.username} value={owner.username}>{owner.name}</option>)}
166
+ </select>
167
+ {failed ? <div className="lp-proj-setup-inline">
168
+ <span className="lp-proj-error" role="alert">Assignees did not load.</span>
169
+ <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={disabled}
170
+ onClick={() => setAttempt(current => current + 1)}>Retry</button>
171
+ </div> : null}
172
+ </>;
173
  }
174
 
175
  class ProjectRequestError extends Error {
 
279
  const share = clampPercent(progress) / 100;
280
  if (status === "Completed" || status === "Canceled" || status === "Backlog") {
281
  const Icon = status === "Completed" ? Check : status === "Canceled" ? Prohibit : CircleDashed;
282
+ return <Icon className="lp-proj-ring lp-proj-accent" data-status={status} size={14} aria-hidden="true" />;
283
  }
284
+ return <svg className="lp-proj-ring lp-proj-accent" data-status={status} width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">
285
  <circle cx="7" cy="7" r={radius} fill="none" stroke="currentColor" strokeWidth="1.5"
286
  opacity={status === "Planned" ? 1 : 0.35} />
287
  <circle cx="7" cy="7" r={radius} fill="none" stroke="currentColor" strokeWidth="1.5"
 
290
  </svg>;
291
  }
292
 
293
+ function StatusPill({ status, progress }: { status: Status; progress: number }) {
294
+ return <span className="lp-proj-status lp-proj-accent" data-status={status}>
295
+ <StatusRing status={status} progress={progress} />{status}
296
+ </span>;
297
+ }
298
+
299
+ function StatusPicker({ value, disabled, onChange }: {
300
+ value: Status; disabled: boolean; onChange: (status: Status) => void;
301
+ }) {
302
+ const [open, setOpen] = useState(false);
303
+ const trigger = useRef<HTMLButtonElement>(null);
304
+ const menuId = useId();
305
+ const expanded = open && !disabled;
306
+ return <>
307
+ <button ref={trigger} type="button" className="lp-proj-status lp-proj-status-trigger lp-proj-accent" data-status={value}
308
+ aria-label={`Status: ${value}`} aria-haspopup="menu" aria-expanded={expanded}
309
+ aria-controls={expanded ? menuId : undefined} disabled={disabled} onClick={() => setOpen(current => !current)}
310
+ onKeyDown={event => {
311
+ if (event.key === "ArrowDown" || event.key === "ArrowUp") {
312
+ event.preventDefault(); setOpen(true);
313
+ }
314
+ }}>
315
+ <span className="lp-proj-status-dot" aria-hidden="true" />{value}<CaretDown size={12} aria-hidden="true" />
316
+ </button>
317
+ {expanded ? <AnchoredOverlay anchor={trigger.current} className="lp-proj-status-menu" id={menuId} role="menu"
318
+ ariaLabel="Project status" initialFocus='[aria-checked="true"]' onDismiss={() => setOpen(false)}
319
+ onKeyDown={event => {
320
+ if (event.key === "Tab") { event.preventDefault(); setOpen(false); return; }
321
+ const items = Array.from(event.currentTarget.querySelectorAll<HTMLButtonElement>('[role="menuitemradio"]'));
322
+ const current = items.findIndex(item => item === document.activeElement);
323
+ let next: HTMLButtonElement | undefined;
324
+ if (event.key === "ArrowDown") next = items[(current + 1) % items.length];
325
+ else if (event.key === "ArrowUp") next = items[(current - 1 + items.length) % items.length];
326
+ else if (event.key === "Home") next = items[0];
327
+ else if (event.key === "End") next = items.at(-1);
328
+ else if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
329
+ next = [...items.slice(current + 1), ...items.slice(0, current + 1)]
330
+ .find(item => item.textContent?.toLowerCase().startsWith(event.key.toLowerCase()));
331
+ }
332
+ if (next) { event.preventDefault(); event.stopPropagation(); next.focus(); }
333
+ }}>
334
+ {STATUS_GROUPS.map((group, index) => <div key={group.label} className="lp-proj-status-group" role="group"
335
+ aria-labelledby={`${menuId}-${index}`}>
336
+ <span id={`${menuId}-${index}`} className="lp-proj-status-heading">{group.label}</span>
337
+ {group.statuses.map(status => <button type="button" key={status} role="menuitemradio" aria-checked={status === value}
338
+ tabIndex={status === value ? 0 : -1} className="lp-proj-status-option lp-proj-accent" data-status={status}
339
+ onClick={() => { onChange(status); setOpen(false); }}>
340
+ <span className="lp-proj-status-dot" aria-hidden="true" /><span>{status}</span>
341
+ {status === value ? <Check size={14} aria-hidden="true" /> : null}
342
+ </button>)}
343
+ </div>)}
344
+ </AnchoredOverlay> : null}
345
+ </>;
346
+ }
347
+
348
  function HealthGlyph({ health }: { health: Health }) {
349
  if (!health) return null;
350
  const Icon = health === "On track" ? TrendUp : health === "At risk" ? Minus : TrendDown;
351
  return <Icon className="lp-proj-health" data-health={health} size={14} aria-hidden="true" />;
352
  }
353
 
354
+ function HealthChip({ health }: { health: Health }) {
355
+ return health ? <span className="lp-proj-health-chip" data-health={health}>
356
+ <HealthGlyph health={health} />{health}
357
+ </span> : null;
358
+ }
359
+
360
  function PriorityGlyph({ priority }: { priority: Priority }) {
361
  const Icon = priority === "Urgent" ? Warning : priority === "High" ? CellSignalHigh
362
  : priority === "Medium" ? CellSignalMedium : priority === "Low" ? CellSignalLow : DotsThree;
 
489
  <tbody>
490
  {projects.map((project) => {
491
  const via = project.via && grain && (project.via.grain !== grain) ? project.via : null;
492
+ return <tr key={project.id} className="lp-proj-row lp-proj-accent" data-status={project.status}
493
+ data-project-id={project.id} onClick={() => onOpen(project.id)}>
494
  <td><div className="lp-proj-name-wrap">
495
  <button type="button" className="lp-proj-name" onClick={(event) => { event.stopPropagation(); onOpen(project.id); }}>
496
  <Cube className="lp-proj-name-glyph" size={14} aria-hidden="true" />
 
502
  let cell: ReactNode;
503
  switch (key) {
504
  case "health":
505
+ cell = <HealthChip health={project.health} />;
 
506
  break;
507
  case "priority":
508
  cell = <span className="lp-proj-cell"><PriorityGlyph priority={project.priority} />
 
518
  cell = formatDay(project[key]) || <span className="lp-proj-muted">No date</span>;
519
  break;
520
  case "status":
521
+ cell = <StatusPill status={project.status} progress={project.progress} />;
522
  break;
523
  case "results":
524
  cell = <ProjectResultPreview projectId={project.id} configs={project.resultConfigs ?? []}
 
664
  <div id={`${id}-summary`} role="tabpanel" aria-labelledby={`${id}-${tab}`} title="Counts follow the other filters">
665
  {tab === "Health" ? healthCounts.map(({ label, count }) => <button type="button" className="lp-proj-summary-row" key={label}
666
  aria-pressed={health === label} onClick={() => setHealth(health === label ? "" : label)}>
667
+ <span>{label === "No health" ? label : <HealthChip health={label} />}</span><span>{count}</span>
668
  </button>) : ownerCounts.map(({ value, count }) => <button type="button" className="lp-proj-summary-row" key={ownerKey(value)}
669
  aria-pressed={owner === ownerKey(value)} onClick={() => setOwner(owner === ownerKey(value) ? "" : ownerKey(value))}>
670
  <Owner name={value} /><span>{count}</span>
 
676
  </section>;
677
  }
678
 
679
+ // ---- One project: Results, the existing Setup form, and Log ----
680
+
681
+ const PROJECT_PANES = [
682
+ { key: "results", label: "Results" },
683
+ { key: "setup", label: "Setup" },
684
+ { key: "log", label: "Log" },
685
+ ] as const;
686
+ type ProjectPane = (typeof PROJECT_PANES)[number]["key"];
687
+
688
+ function SetupRow({ field, label, error, children, readOnly = false }: {
689
+ field: string; label: string; error?: string; children: ReactNode; readOnly?: boolean;
690
+ }) {
691
+ const labelId = useId();
692
+ return <div className="lp-proj-setup-row" data-field={field} data-read-only={readOnly || undefined}
693
+ role="group" aria-labelledby={labelId}>
694
+ <span className="lp-proj-setup-label" id={labelId}>{label}</span>
695
+ <div className="lp-proj-setup-value">
696
+ {children}
697
+ {error ? <p className="lp-proj-error lp-proj-setup-error" role="alert">{error}</p> : null}
698
+ </div>
699
+ </div>;
700
+ }
701
+
702
+ function SetupDate({ label, value, canEdit, busy, onChange, max, hint }: {
703
+ label: string; value: string | null; canEdit: boolean; busy: boolean;
704
+ onChange: (value: string | null) => void; max?: string; hint?: string;
705
+ }) {
706
+ const [editing, setEditing] = useState(false);
707
+ const caption = value ? new Date(`${value}T00:00:00`).toLocaleDateString("en-US",
708
+ { month: "short", day: "numeric", year: "numeric" }) : "Not set";
709
+ if (!canEdit) return value ? <time dateTime={value}>{caption}</time> : <span className="lp-proj-muted">Not set</span>;
710
+ return editing ? <input className="lp-proj-setup-input" type="date" aria-label={label}
711
+ value={value ?? ""} disabled={busy} max={max} autoFocus
712
+ onChange={event => onChange(event.target.value || null)} onBlur={() => setEditing(false)} />
713
+ : <button type="button" className="lp-proj-setup-edit" data-empty={!value || undefined}
714
+ aria-label={`${value ? "Edit" : "Add"} ${label.toLowerCase()}`} title={hint}
715
+ disabled={busy} onClick={() => setEditing(true)}>
716
+ {value ? caption : `Add ${label.toLowerCase()}`}
717
+ </button>;
718
+ }
719
 
720
  function ProjectDetail({ id, canEdit: mayEdit, onChanged, onDeleted, onCancel }: {
721
  id: string; canEdit: boolean; onChanged: () => void; onDeleted: () => void; onCancel: () => void;
 
727
  const [busy, setBusy] = useState(false);
728
  const [adding, setAdding] = useState(false);
729
  const [confirming, setConfirming] = useState(false);
730
+ const [editingDescription, setEditingDescription] = useState(false);
731
+ const [fieldErrors, setFieldErrors] = useState<Partial<Record<keyof ProjectDraft, string>>>({});
732
+ const [saved, setSaved] = useState(false);
733
+ const [pane, setPane] = useState<ProjectPane>("results");
734
  const formId = useId();
735
+ const railRef = useRef<HTMLElement>(null);
736
  const writing = useRef(false);
 
737
  const loaded = detail !== null;
738
 
739
  useEffect(() => {
740
+ if (loaded) railRef.current?.querySelector<HTMLButtonElement>('[aria-selected="true"]')?.focus();
741
  }, [loaded]);
742
 
 
 
 
 
743
  useEffect(() => {
744
  const controller = new AbortController();
745
  call<Detail>(`/${enc(id)}`, { signal: controller.signal }, "This project did not load.")
 
753
  return () => controller.abort();
754
  }, [id, tick]);
755
 
756
+ const write = useCallback(async (path: string, method: string, body: unknown, after?: () => void,
757
+ fields?: Array<keyof ProjectDraft>) => {
758
  if (writing.current || !mayEdit || !detail?.canEdit) return;
759
  writing.current = true;
760
  setBusy(true);
 
766
  if (method === "PATCH") setDraft(draftOf(project));
767
  }
768
  setError("");
769
+ setFieldErrors({});
770
+ if (method === "PATCH") setSaved(true);
771
  if (after) after(); else setTick((value) => value + 1);
772
  onChanged();
773
  } catch (reason) {
774
+ const message = messageOf(reason, "The change was not saved.");
775
+ if (fields?.length) {
776
+ const namedField = fields.find(field => message.toLowerCase().includes(
777
+ ({ startOn: "start_on", targetOn: "target_on", completedOn: "completed_on",
778
+ ownerUsername: "owner" } as Partial<Record<keyof ProjectDraft, string>>)[field] ?? field.toLowerCase()));
779
+ setFieldErrors(Object.fromEntries((namedField ? [namedField] : fields).map(field => [field, message])));
780
+ setError("");
781
+ } else setError(message);
782
  if (reason instanceof ProjectRequestError && reason.status === 403) {
783
  setDetail((current) => current ? { ...current, canEdit: false } : current);
784
  onChanged();
 
789
  }
790
  }, [detail?.canEdit, mayEdit, onChanged]);
791
 
792
+ const patch = useCallback((change: ProjectInput, fields: Array<keyof ProjectDraft>) =>
793
+ write(`/${enc(id)}`, "PATCH", change, undefined, fields), [id, write]);
794
+
795
+ const rail = <nav className="lp-proj-detail-rail" ref={railRef} aria-label="Project sections">
796
+ <div className="lp-proj-detail-rail-list" role="tablist" aria-label="Project sections" aria-orientation="vertical">
797
+ {PROJECT_PANES.map(({ key, label }, index) => <button key={key} type="button" role="tab"
798
+ className={`lp-proj-detail-rail-row${pane === key ? " is-on" : ""}`}
799
+ id={`${formId}-tab-${key}`} aria-controls={`${formId}-pane-${key}`}
800
+ aria-selected={pane === key} tabIndex={pane === key ? 0 : -1} onClick={() => setPane(key)}
801
+ onKeyDown={event => {
802
+ let next: number;
803
+ if (event.key === "ArrowDown" || event.key === "ArrowRight") next = (index + 1) % PROJECT_PANES.length;
804
+ else if (event.key === "ArrowUp" || event.key === "ArrowLeft") next = (index - 1 + PROJECT_PANES.length) % PROJECT_PANES.length;
805
+ else if (event.key === "Home") next = 0;
806
+ else if (event.key === "End") next = PROJECT_PANES.length - 1;
807
+ else return;
808
+ event.preventDefault();
809
+ event.stopPropagation();
810
+ setPane(PROJECT_PANES[next].key);
811
+ railRef.current?.querySelectorAll<HTMLButtonElement>('[role="tab"]')[next]?.focus();
812
+ }}><span className="lp-proj-detail-rail-label">{label}</span></button>)}
813
+ </div>
814
+ </nav>;
815
+
816
+ if (!detail || !draft) return <div className="lp-proj-detail-layout">
817
+ {rail}
818
+ <div className="lp-proj-panes">
819
+ {PROJECT_PANES.map(({ key }) => <section key={key} className="lp-proj-pane" role="tabpanel"
820
+ id={`${formId}-pane-${key}`} aria-labelledby={`${formId}-tab-${key}`} hidden={pane !== key}
821
+ tabIndex={0} aria-busy={!error}>
822
+ {error ? <div><p className="lp-proj-error" role="alert">{error}</p>
823
+ <button type="button" className="lp-proj-btn" onClick={() => { setError(""); setTick(value => value + 1); }}>Retry</button>
824
+ </div> : <LoadingRows />}
825
+ </section>)}
826
+ </div>
827
+ </div>;
828
+ const { project, targets } = detail;
829
+ const activity = [...detail.activity].sort((a, b) => Date.parse(b.at) - Date.parse(a.at));
830
  const canEdit = mayEdit && detail.canEdit;
831
  const display = canEdit ? draft : project;
832
  const progress = Number.isFinite(display.progress) ? clampPercent(display.progress) : 0;
833
  const dirty = JSON.stringify(draft) !== JSON.stringify(draftOf(project));
834
+ const set = <K extends keyof ProjectDraft>(key: K, value: ProjectDraft[K]) => {
835
+ setSaved(false);
836
+ setFieldErrors(current => ({ ...current, [key]: undefined }));
837
+ setDraft(current => current ? { ...current, [key]: value,
838
+ ...(key === "status" && value !== "Completed" ? { completedOn: null } : {}),
839
+ ...(key === "completedOn" && value ? { status: "Completed" as Status } : {}),
840
+ } : current);
841
+ };
842
  const save = (event: FormEvent) => {
843
  event.preventDefault();
844
  if (!canEdit || !dirty) return;
845
  const name = draft.name.trim();
846
+ if (!name) { setFieldErrors({ name: "Name the project." }); return; }
847
  if (!Number.isInteger(draft.progress) || draft.progress < 0 || draft.progress > 100) {
848
+ setFieldErrors({ progress: "Enter a whole percent from 0 to 100." }); return;
849
+ }
850
+ const today = new Date().toISOString().slice(0, 10);
851
+ if (draft.completedOn && draft.completedOn > today) {
852
+ setFieldErrors({ completedOn: "Choose today or an earlier date." }); return;
853
+ }
854
+ if (project.completedOn && draft.status === "Completed" && !draft.completedOn) {
855
+ setFieldErrors({ completedOn: "Choose a completed date, or change Status to reopen the project." }); return;
856
  }
857
+ const original = draftOf(project);
858
+ const changed = (Object.keys(draft) as Array<keyof ProjectDraft>).filter(key => draft[key] !== original[key]);
859
+ const fields = Object.fromEntries(changed.filter(key => key !== "owner").map(key => [key, draft[key]])) as ProjectInput;
860
+ if (changed.includes("owner")) fields.ownerUsername = draft.ownerUsername;
861
+ if (changed.includes("name")) fields.name = name;
862
+ if (changed.includes("notes")) fields.notes = draft.notes?.trim() || null;
863
+ setSaved(false);
864
+ setFieldErrors({});
865
+ void patch(fields, changed);
866
  };
867
 
868
+ const setup = <form id={formId} className="lp-proj-detail lp-proj-accent" data-status={display.status}
869
+ data-can-edit={canEdit ? "true" : "false"} onSubmit={save} aria-label="Project setup" aria-busy={busy} noValidate>
870
+ <div className="lp-proj-main">
871
+ <div data-field="name">
872
+ <h2 className="lp-proj-title" tabIndex={-1}>
873
+ {canEdit ? <input className="lp-proj-title-input" value={draft.name} aria-label="Project name"
874
+ aria-invalid={Boolean(fieldErrors.name)} disabled={busy} required maxLength={300}
875
+ aria-describedby={fieldErrors.name ? `${formId}-name-error` : undefined}
876
+ onChange={event => set("name", event.target.value)} /> : project.name}
877
+ </h2>
878
+ {fieldErrors.name ? <p id={`${formId}-name-error`} className="lp-proj-error lp-proj-setup-error" role="alert">
879
+ {fieldErrors.name}
880
+ </p> : null}
881
+ </div>
882
+
883
+ <div className="lp-proj-setup-fields">
884
+ <SetupRow field="status" label="Status" error={fieldErrors.status}>
885
+ {canEdit
886
+ ? <StatusPicker value={draft.status} disabled={busy} onChange={value => set("status", value)} />
887
+ : <StatusPill status={project.status} progress={progress} />}
888
+ </SetupRow>
889
+ <SetupRow field="priority" label="Priority" error={fieldErrors.priority}>
890
+ <div className="lp-proj-setup-inline"><PriorityGlyph priority={display.priority} />
891
  {canEdit
892
+ ? <select className="lp-proj-setup-input" aria-label="Priority" value={draft.priority} disabled={busy}
893
+ data-empty={draft.priority === "No priority" || undefined}
894
  onChange={(event) => set("priority", event.target.value as Priority)}>
895
+ {PROJECT_PRIORITIES.map((value) => <option key={value} value={value}>{value === "No priority" ? "Add priority" : value}</option>)}
896
  </select>
897
  : <span>{project.priority}</span>}
898
+ </div>
899
+ </SetupRow>
900
+ <SetupRow field="ownerUsername" label="Assignee" error={fieldErrors.ownerUsername || fieldErrors.owner}>
901
+ {canEdit ? <div className="lp-proj-setup-assignee" data-empty={!draft.ownerUsername && !draft.owner || undefined}>
902
+ <OwnerSelect value={draft.ownerUsername} legacy={draft.owner} disabled={busy} label="Assignee" emptyLabel="Add assignee"
903
+ onChange={value => { set("ownerUsername", value); set("owner", null); }} />
904
+ </div> : project.owner ? <Owner name={project.owner} /> : <span className="lp-proj-muted">Unassigned</span>}
905
+ </SetupRow>
906
+ {([ ["startOn", "Start date"], ["targetOn", "Target date"], ["completedOn", "Completed date"] ] as const)
907
+ .map(([field, label]) => <SetupRow key={field} field={field} label={label} error={fieldErrors[field]}>
908
+ <SetupDate label={label} value={display[field]} canEdit={canEdit} busy={busy}
909
+ max={field === "completedOn" ? new Date().toISOString().slice(0, 10) : undefined}
910
+ hint={field === "completedOn" && display.status !== "Completed" ? "Also sets Status to Completed" : undefined}
911
+ onChange={value => set(field, value)} />
912
+ </SetupRow>)}
913
+ <SetupRow field="progress" label="Progress" error={fieldErrors.progress}>
914
+ {canEdit ? <div className="lp-proj-setup-inline">
915
+ <input className="lp-proj-setup-input lp-proj-setup-percent" type="number" aria-label="Progress"
916
+ min={0} max={100} step={1} value={Number.isNaN(draft.progress) ? "" : draft.progress} disabled={busy}
917
+ aria-invalid={Boolean(fieldErrors.progress)}
918
+ onChange={event => set("progress", event.target.valueAsNumber)} /><span>%</span>
919
+ </div> : <span>{progress}%</span>}
920
+ </SetupRow>
921
+ <SetupRow field="health" label="Health" readOnly>
922
+ {project.health ? <HealthChip health={project.health} /> : <span className="lp-proj-muted">Not set</span>}
923
+ </SetupRow>
924
+ <SetupRow field="notes" label="Description" error={fieldErrors.notes}>
925
+ {canEdit ? draft.notes || editingDescription
926
+ ? <textarea className="lp-proj-setup-input lp-proj-setup-description" aria-label="Description"
927
+ placeholder="Add description" value={draft.notes ?? ""} rows={4} disabled={busy}
928
+ autoFocus={editingDescription} onChange={event => set("notes", event.target.value)}
929
+ onBlur={() => setEditingDescription(false)} />
930
+ : <button type="button" className="lp-proj-setup-edit" data-empty disabled={busy}
931
+ onClick={() => setEditingDescription(true)}>Add description</button>
932
+ : <p className="lp-proj-notes-text">{project.notes || <span className="lp-proj-muted">Not set</span>}</p>}
933
+ </SetupRow>
934
  </div>
935
 
936
  {error ? <p className="lp-proj-error" role="alert">{error}</p> : null}
937
  {detail.cause ? <p className="lp-proj-muted" role="status">{detail.cause}</p> : null}
938
+ <SetupRow field="targets" label="SKUs">
 
 
 
 
 
 
 
 
 
 
 
 
939
  <section className="lp-proj-section" aria-label="SKUs">
940
  <div className="lp-proj-section-head">
941
+ <span className="lp-proj-muted">{targets.length ? `${targets.length} linked` : "None linked"}</span>
942
  {canEdit && !adding
943
+ ? <button type="button" className="lp-proj-setup-edit" data-empty disabled={busy} onClick={() => setAdding(true)}>Add SKUs</button>
944
  : null}
945
  </div>
946
  {canEdit && adding
 
960
  : null}
961
  </li>)}
962
  </ul>
963
+ : null}
964
  </section>
965
+ </SetupRow>
966
+ <div className="lp-proj-setup-footer" aria-label="Project timestamps">
967
+ {([ ["createdAt", "Created"], ["updatedAt", "Updated"] ] as const).map(([field, label]) =>
968
+ <SetupRow key={field} field={field} label={label} readOnly>
969
+ {project[field] ? <time dateTime={project[field]} title={project[field]}>
970
+ {new Date(project[field]).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })}
971
+ </time> : <span>Not available</span>}
972
+ </SetupRow>)}
973
+ </div>
974
+ {saved && !dirty ? <span className="lp-proj-muted" role="status">Changes saved</span> : null}
975
  </div>
976
 
977
  <aside className="lp-proj-side">
978
+ <ProjectFooter>{pane === "setup" ? <div className="lp-proj-save">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979
  <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={busy} onClick={onCancel}>Cancel</button>
980
  {canEdit ? <button type="submit" form={formId} className="lp-proj-btn lp-proj-btn-primary" disabled={busy || !dirty}>
981
  {busy ? "Saving…" : "Save changes"}
982
  </button> : null}
983
  {canEdit && dirty ? <button type="button" className="lp-proj-btn lp-proj-btn-quiet" disabled={busy}
984
+ onClick={() => { setDraft(draftOf(project)); setError(""); setFieldErrors({}); setSaved(false); }}>Discard changes</button> : null}
985
+ </div> : null}</ProjectFooter>
986
  {canEdit
987
  ? <div className="lp-proj-delete">
988
  {confirming
 
996
  : null}
997
  </aside>
998
  </form>;
999
+
1000
+ return <div className="lp-proj-detail-layout">
1001
+ {rail}
1002
+ <div className="lp-proj-panes">
1003
+ <section className="lp-proj-pane lp-proj-pane-stack" role="tabpanel" id={`${formId}-pane-results`}
1004
+ aria-labelledby={`${formId}-tab-results`} hidden={pane !== "results"} tabIndex={0}>
1005
+ <h2 className="lp-proj-title">{project.name}</h2>
1006
+ <ProjectResults projectId={id} configs={project.resultConfigs ?? []} completedOn={project.completedOn}
1007
+ updatedAt={project.updatedAt} canEdit={canEdit} onSaved={() => { setTick(value => value + 1); onChanged(); }} />
1008
+ </section>
1009
+ <section className="lp-proj-pane" role="tabpanel" id={`${formId}-pane-setup`}
1010
+ aria-labelledby={`${formId}-tab-setup`} hidden={pane !== "setup"} tabIndex={0}>
1011
+ {setup}
1012
+ </section>
1013
+ <section className="lp-proj-pane lp-proj-pane-stack lp-proj-log" role="tabpanel" id={`${formId}-pane-log`}
1014
+ aria-labelledby={`${formId}-tab-log`} hidden={pane !== "log"} tabIndex={0}>
1015
+ <h2 className="lp-proj-title">{project.name}</h2>
1016
+ {activity.length
1017
+ ? <ol className="lp-proj-activity" aria-label="Activity">
1018
+ {activity.map((entry, index) => <li key={`${entry.at}:${index}`}>
1019
+ <span className="lp-proj-activity-dot" aria-hidden="true" />
1020
+ <span>{activityLine(entry)} <time className="lp-proj-muted" dateTime={entry.at} title={entry.at}>
1021
+ {formatWhen(entry.at)}</time></span>
1022
+ </li>)}
1023
+ </ol>
1024
+ : <p className="lp-proj-muted">No activity yet</p>}
1025
+ </section>
1026
+ </div>
1027
+ </div>;
1028
  }
1029
 
1030
  function TargetLine({ target }: { target: Target }) {
 
1133
  onCancel: () => void; onForbidden: () => void;
1134
  }) {
1135
  const [draft, setDraft] = useState<ProjectDraft>({
1136
+ name: "", status: "Planned", priority: "Medium", owner: "", ownerUsername: null,
1137
+ startOn: "", targetOn: "", completedOn: null, progress: 0, notes: "",
1138
  });
1139
  const formId = useId();
1140
  const [busy, setBusy] = useState(false);
 
1172
  <label className="lp-proj-field lp-proj-field-wide"><span>Project name</span>
1173
  <input className="lp-proj-input" value={draft.name} onChange={(event) => set("name", event.target.value)}
1174
  placeholder="Name your project" autoFocus required maxLength={300} disabled={busy} /></label>
1175
+ <div className="lp-proj-field"><span>Status</span>
1176
+ <StatusPicker value={draft.status} disabled={busy} onChange={value => set("status", value)} /></div>
 
 
1177
  <label className="lp-proj-field"><span>Owner</span>
1178
  <OwnerSelect value={draft.ownerUsername} disabled={busy} onChange={value => set("ownerUsername", value)} /></label>
1179
  <label className="lp-proj-field"><span>Priority</span>
web/src/connectors/ConnectorsPage.tsx CHANGED
@@ -253,11 +253,109 @@ export interface ConnectorSyncReading {
253
  lastSyncAt: string | null;
254
  passMinutes?: number | null;
255
  budgetMinutes?: number | null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  }
257
 
258
  /** Read sync status independently of database pages and schema discovery. */
259
  export function useConnectorSyncStatus(connectorKey: string, connectionId?: string, refreshKey = 0) {
260
  const identity = JSON.stringify([connectorKey, connectionId ?? null]);
 
 
261
  const [reading, setReading] = useState<{
262
  identity: string; status: ConnectorSyncReading | null; problem: string;
263
  } | null>(null);
@@ -290,6 +388,7 @@ export function useConnectorSyncStatus(connectorKey: string, connectionId?: stri
290
  && body.passMinutes >= 0 ? body.passMinutes : null,
291
  budgetMinutes: typeof body.budgetMinutes === "number" && Number.isFinite(body.budgetMinutes)
292
  && body.budgetMinutes > 0 ? body.budgetMinutes : null,
 
293
  } });
294
  } catch {
295
  if (!controller.signal.aborted) setReading((current) => ({
@@ -300,12 +399,26 @@ export function useConnectorSyncStatus(connectorKey: string, connectionId?: stri
300
  };
301
  void read();
302
  return () => controller.abort();
303
- }, [connectorKey, connectionId, identity, refreshKey]);
 
 
 
 
 
304
  const current = reading?.identity === identity ? reading : null;
305
- return { status: current?.status ?? null, problem: current?.problem ?? "" };
 
 
 
 
 
 
 
 
 
306
  }
307
 
308
- /** Daily sources whose sync route reports `stale` on Odoo's cadence rule, with the name a card shows. */
309
  const DAILY_SOURCE_NAMES: Record<string, string> = { amazon_sp: "Amazon", meta_ads: "Meta" };
310
 
311
  /** The card warning for one sync reading, PURE: none unless the route says a completed sync is stale. */
@@ -340,9 +453,11 @@ export function ConnectorSyncStatusLine({ status, problem = "" }: {
340
  const showDuration = status && typeof status.passMinutes === "number"
341
  && Number.isFinite(status.passMinutes) && status.passMinutes >= 0;
342
  return <p className="conn-settings-last-success" role="status" data-connector-sync={status?.provider}
343
- aria-busy={!status && !problem} data-sync-status={status ? "ready" : problem ? "error" : "loading"}>
344
  {status ? <>Last sync at {status.lastSyncAt
345
- ? <time dateTime={status.lastSyncAt}>{readableSyncStamp(status.lastSyncAt)}</time>
 
 
346
  : "not recorded"}</> : problem || "Loading sync status…"}
347
  {showBudget ? <>{", "}<span data-sync-budget={status.provider}>
348
  {showDuration ? `${status.passMinutes} min of ${status.budgetMinutes} min budget`
@@ -608,8 +723,9 @@ function ConnectorSetupDialog({ row, onClose, onChanged, onPullReviews, onOpenDe
608
  const panelRef = useRef<HTMLDivElement>(null);
609
  const clientId = row.clientId || row.key;
610
  const client = row.kind === "mcp" && isClientId(clientId) ? clientId : null;
611
- const [tab, setTab] = useState(() => client ? "overview" : connectorTab(initialTab));
612
- const tabs = client ? [{ key: "overview" as const, label: "Overview" }] : connectorTabs(row);
 
613
  useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false,
614
  initialFocus: "[data-overlay-autofocus]", trapFocus: !pulling, restoreFocus: true });
615
  return (
@@ -626,16 +742,18 @@ function ConnectorSetupDialog({ row, onClose, onChanged, onPullReviews, onOpenDe
626
  </svg>
627
  </button>
628
  </header>
629
- <div className="conn-modal-tabs" role="tablist" aria-label="Connector sections">
630
  {tabs.map((entry) => <button key={entry.key} type="button" role="tab"
631
  className="conn-modal-tab" id={`conn-guide-tab-${entry.key}`} aria-selected={tab === entry.key}
632
  aria-controls="conn-guide-panel" onClick={() => setTab(connectorTab(entry.key))}>{entry.label}</button>)}
633
- </div>
634
- <div className="conn-modal-body" id="conn-guide-panel" role="tabpanel" aria-labelledby={`conn-guide-tab-${tab}`}>
 
 
635
  {client ? (
636
  <ExternalAccessPane initialClient={client} view="client"
637
  onChanged={onChanged} onOpenApi={onOpenApi} />
638
- ) : tab === "overview" ? <p className="conn-settings-empty">{row.desc || row.label}</p>
639
  : !isAdmin ? <p className="conn-setup-refusal">Only an admin can manage connector keys.</p>
640
  : row.key === "generic" ? <GenericSecretsSetup isAdmin={isAdmin} />
641
  : row.key === "whatsapp" ? <WhatsAppSenderTabs onChanged={onChanged} />
@@ -884,7 +1002,9 @@ export default function ConnectorsPage({
884
  | null;
885
  if (dead) return;
886
  if (!res.ok) {
887
- const message = body?.error?.message || `The server answered ${res.status}.`;
 
 
888
  setDirectoryError(message);
889
  setLoad((current) => current.phase === "ready" ? current : { phase: "error", message });
890
  return;
@@ -1323,32 +1443,19 @@ export function OdooSyncStatus({ onOpenDatabases }: { onOpenDatabases?: () => vo
1323
  );
1324
  }
1325
 
1326
- /**
1327
- * ⭐⭐ W31-T44 (owner item 6b) — THE BANNER THAT SAYS THE LIST IS SHORT, EXPORTED FOR THE SAME
1328
- * REASON `ConnectorCard` IS.
1329
- *
1330
- * The page fetches on mount and `renderToStaticMarkup` does not run effects, so a static render
1331
- * of `ConnectorsPage` is a spinner forever — which is why W30 lifted the card out. This ticket's
1332
- * `done-when` is likewise visual ("the page can render incomplete"), so the thing that has to be
1333
- * looked at has to be reachable without a browser session. `_conn_shot.tsx` paints THIS.
1334
- *
1335
- * ⛔ IT IS RENDERED ABOVE THE GRID AND NOT GATED ON `rows.length`, which is the
1336
- * whole distinction the ticket is about. A directory that lost one registry still shows a
1337
- * confident wall of cards, and "confidently short" is worse than empty: the reader has no way to
1338
- * know a connector they HAVE is simply not on the page.
1339
- */
1340
  export function DirectoryGaps({ gaps }: { gaps: ConnectorGap[] }) {
1341
  if (gaps.length === 0) return null;
 
 
 
 
1342
  return (
1343
- <div className="conn-note is-warn" role="status">
1344
- <strong>This list is incomplete.</strong>
1345
  <ul>
1346
  {gaps.map((g) => (
1347
  <li key={g.source + g.cause}>
1348
- {g.cause}
1349
- {/* ⚠ The joiner was an em dash and standing rule 2 bans it on screen. A full stop
1350
- carries the same pause and needs no glyph. */}
1351
- {g.recommendation ? `. ${g.recommendation}` : ""}
1352
  </li>
1353
  ))}
1354
  </ul>
 
253
  lastSyncAt: string | null;
254
  passMinutes?: number | null;
255
  budgetMinutes?: number | null;
256
+ amazonSchedule?: AmazonSyncScheduleReading;
257
+ }
258
+
259
+ export interface AmazonTableSync {
260
+ table: string;
261
+ connectionId: string;
262
+ account: string;
263
+ lastSyncAt: string | null;
264
+ nextSyncAt: string;
265
+ perDay: number;
266
+ state: string;
267
+ }
268
+
269
+ export interface AmazonSyncScheduleReading {
270
+ state: "ready" | "empty" | "unavailable";
271
+ timezone: string;
272
+ canSync: boolean;
273
+ tables: AmazonTableSync[];
274
+ }
275
+
276
+ /** Reject an incomplete schedule instead of hiding malformed rows. */
277
+ export function parseAmazonSyncSchedule(body: Record<string, unknown>): AmazonSyncScheduleReading {
278
+ const unavailable: AmazonSyncScheduleReading = {
279
+ state: "unavailable", timezone: "UTC", canSync: false, tables: [],
280
+ };
281
+ if (body.schedule !== "per_table" || typeof body.displayTimezone !== "string"
282
+ || !["ready", "empty", "unavailable"].includes(String(body.scheduleState))
283
+ || !Array.isArray(body.tableSchedule)) return unavailable;
284
+ try { new Intl.DateTimeFormat("en-US", { timeZone: body.displayTimezone }).format(0); }
285
+ catch { return unavailable; }
286
+ const tables: AmazonTableSync[] = [];
287
+ const identities = new Set<string>();
288
+ for (const value of body.tableSchedule) {
289
+ if (!value || typeof value !== "object") return unavailable;
290
+ const row = value as Record<string, unknown>;
291
+ if (typeof row.table !== "string" || !row.table || typeof row.connectionId !== "string" || !row.connectionId
292
+ || typeof row.account !== "string" || typeof row.state !== "string"
293
+ || (row.lastSyncAt !== null && (typeof row.lastSyncAt !== "string" || !Number.isFinite(Date.parse(row.lastSyncAt))))
294
+ || typeof row.nextSyncAt !== "string" || !Number.isFinite(Date.parse(row.nextSyncAt))
295
+ || typeof row.perDay !== "number" || !Number.isInteger(row.perDay) || row.perDay < 1) return unavailable;
296
+ const identity = JSON.stringify([row.connectionId, row.table]);
297
+ if (identities.has(identity)) return unavailable;
298
+ identities.add(identity);
299
+ tables.push(row as unknown as AmazonTableSync);
300
+ }
301
+ const state = body.scheduleState as AmazonSyncScheduleReading["state"];
302
+ if ((state === "ready") !== (tables.length > 0)) return unavailable;
303
+ return { state, timezone: body.displayTimezone, canSync: body.canSync === true, tables };
304
+ }
305
+
306
+ /** Format instants in the workspace zone, including its daylight saving offset. */
307
+ export function amazonSyncStamp(stamp: string, timezone: string): string {
308
+ return new Intl.DateTimeFormat("en-US", { timeZone: timezone, year: "numeric", month: "short", day: "numeric",
309
+ hour: "numeric", minute: "2-digit", timeZoneName: "short" }).format(new Date(stamp));
310
+ }
311
+
312
+ /** Reuse the connector table skin for every account and declared table. */
313
+ export function AmazonSyncSchedule({ reading, problem = "", onRetry }: {
314
+ reading: AmazonSyncScheduleReading | null; problem?: string; onRetry?: () => void;
315
+ }) {
316
+ const state = problem || reading?.state === "unavailable" ? "error" : reading?.state ?? "loading";
317
+ const multipleAccounts = new Set(reading?.tables.map((row) => row.connectionId)).size > 1;
318
+ return <section data-amazon-schedule={state} data-sync-role={reading?.canSync ? "admin" : "member"}
319
+ data-table-count={reading?.tables.length ?? 0} aria-label="Amazon table syncs" aria-busy={state === "loading"}>
320
+ {state === "loading" ? <div className="conn-settings-skeleton" role="status" aria-label="Loading Amazon sync schedule">
321
+ <span /><span /><span />
322
+ </div> : state === "empty" ? <p className="conn-settings-empty" role="status">No Amazon connection</p>
323
+ : state === "error" ? <div className="conn-modal-error">
324
+ <p role="alert">Amazon sync schedule is unavailable.</p>
325
+ <button type="button" className="conn-modal-retry" onClick={onRetry}>Try again</button>
326
+ </div> : null}
327
+ {reading && reading.tables.length > 0 ? <>
328
+ <h3 className="conn-settings-table-title">Table syncs</h3>
329
+ <p className="conn-settings-last-success" data-sync-timezone={reading.timezone}>{reading.timezone}</p>
330
+ <div className="conn-settings-table-scroll" role="region" aria-label="Amazon sync schedule" tabIndex={0}>
331
+ <table className="conn-settings-databases" aria-label="Amazon table sync schedule">
332
+ <thead><tr><th scope="col">Table</th>{multipleAccounts ? <th scope="col">Account</th> : null}
333
+ <th scope="col">Last sync</th><th scope="col">Next sync</th>
334
+ <th scope="col" className="conn-settings-number">Passes per day</th></tr></thead>
335
+ <tbody>{reading.tables.map((row) => <tr key={JSON.stringify([row.connectionId, row.table])}
336
+ data-amazon-table={row.table} data-connection-id={row.connectionId} data-table-sync-state={row.state}>
337
+ <th scope="row">{row.table.replace(/_/g, " ")}</th>
338
+ {multipleAccounts ? <td>{row.account}</td> : null}
339
+ <td className="conn-settings-database-success">{row.lastSyncAt
340
+ ? <time dateTime={row.lastSyncAt}>{amazonSyncStamp(row.lastSyncAt, reading.timezone)}</time> : "Not recorded"}</td>
341
+ <td className="conn-settings-database-success">
342
+ <time dateTime={row.nextSyncAt}>{amazonSyncStamp(row.nextSyncAt, reading.timezone)}</time>
343
+ {row.state === "throttled" ? <p role="status">Rate limited, retry scheduled</p>
344
+ : ["failed", "refused"].includes(row.state) ? <p role="status">Sync needs attention</p> : null}
345
+ </td>
346
+ <td className="conn-settings-number">{row.perDay}</td>
347
+ </tr>)}</tbody>
348
+ </table>
349
+ </div>
350
+ </> : null}
351
+ </section>;
352
  }
353
 
354
  /** Read sync status independently of database pages and schema discovery. */
355
  export function useConnectorSyncStatus(connectorKey: string, connectionId?: string, refreshKey = 0) {
356
  const identity = JSON.stringify([connectorKey, connectionId ?? null]);
357
+ const [refreshVersion, setRefreshVersion] = useState(0);
358
+ const refresh = useCallback(() => setRefreshVersion((value) => value + 1), []);
359
  const [reading, setReading] = useState<{
360
  identity: string; status: ConnectorSyncReading | null; problem: string;
361
  } | null>(null);
 
388
  && body.passMinutes >= 0 ? body.passMinutes : null,
389
  budgetMinutes: typeof body.budgetMinutes === "number" && Number.isFinite(body.budgetMinutes)
390
  && body.budgetMinutes > 0 ? body.budgetMinutes : null,
391
+ amazonSchedule: connectorKey === "amazon_sp" ? parseAmazonSyncSchedule(body) : undefined,
392
  } });
393
  } catch {
394
  if (!controller.signal.aborted) setReading((current) => ({
 
399
  };
400
  void read();
401
  return () => controller.abort();
402
+ }, [connectorKey, connectionId, identity, refreshKey, refreshVersion]);
403
+ useEffect(() => {
404
+ if (connectorKey !== "amazon_sp") return;
405
+ const timer = window.setInterval(() => setRefreshVersion((value) => value + 1), 15000);
406
+ return () => window.clearInterval(timer);
407
+ }, [connectorKey]);
408
  const current = reading?.identity === identity ? reading : null;
409
+ return { status: current?.status ?? null, problem: current?.problem ?? "", refresh };
410
+ }
411
+
412
+ /** Keep the schedule's loading, retry and polling independent of database detail pages. */
413
+ export function AmazonSyncOverview({ refreshKey = 0, connectionId }: {
414
+ refreshKey?: number; connectionId?: string;
415
+ }) {
416
+ const sync = useConnectorSyncStatus("amazon_sp", connectionId, refreshKey);
417
+ return <AmazonSyncSchedule reading={sync.status?.amazonSchedule ?? null}
418
+ problem={sync.problem} onRetry={sync.refresh} />;
419
  }
420
 
421
+ /** Sources whose status routes own their stale bounds, with the names their cards show. */
422
  const DAILY_SOURCE_NAMES: Record<string, string> = { amazon_sp: "Amazon", meta_ads: "Meta" };
423
 
424
  /** The card warning for one sync reading, PURE: none unless the route says a completed sync is stale. */
 
453
  const showDuration = status && typeof status.passMinutes === "number"
454
  && Number.isFinite(status.passMinutes) && status.passMinutes >= 0;
455
  return <p className="conn-settings-last-success" role="status" data-connector-sync={status?.provider}
456
+ aria-busy={!status && !problem} data-sync-status={problem ? "error" : status ? "ready" : "loading"}>
457
  {status ? <>Last sync at {status.lastSyncAt
458
+ ? <time dateTime={status.lastSyncAt}>{status.provider === "amazon_sp"
459
+ ? amazonSyncStamp(status.lastSyncAt, status.amazonSchedule?.timezone ?? "UTC")
460
+ : readableSyncStamp(status.lastSyncAt)}</time>
461
  : "not recorded"}</> : problem || "Loading sync status…"}
462
  {showBudget ? <>{", "}<span data-sync-budget={status.provider}>
463
  {showDuration ? `${status.passMinutes} min of ${status.budgetMinutes} min budget`
 
723
  const panelRef = useRef<HTMLDivElement>(null);
724
  const clientId = row.clientId || row.key;
725
  const client = row.kind === "mcp" && isClientId(clientId) ? clientId : null;
726
+ const singlePage = row.section === "contact" || row.kind === "mcp" || row.kind === "api";
727
+ const [tab, setTab] = useState(() => connectorTab(initialTab));
728
+ const tabs = singlePage ? [] : connectorTabs(row);
729
  useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false,
730
  initialFocus: "[data-overlay-autofocus]", trapFocus: !pulling, restoreFocus: true });
731
  return (
 
742
  </svg>
743
  </button>
744
  </header>
745
+ {!singlePage ? <div className="conn-modal-tabs" role="tablist" aria-label="Connector sections">
746
  {tabs.map((entry) => <button key={entry.key} type="button" role="tab"
747
  className="conn-modal-tab" id={`conn-guide-tab-${entry.key}`} aria-selected={tab === entry.key}
748
  aria-controls="conn-guide-panel" onClick={() => setTab(connectorTab(entry.key))}>{entry.label}</button>)}
749
+ </div> : null}
750
+ <div className="conn-modal-body" id={singlePage ? undefined : "conn-guide-panel"}
751
+ role={singlePage ? undefined : "tabpanel"}
752
+ aria-labelledby={singlePage ? undefined : `conn-guide-tab-${tab}`}>
753
  {client ? (
754
  <ExternalAccessPane initialClient={client} view="client"
755
  onChanged={onChanged} onOpenApi={onOpenApi} />
756
+ ) : !singlePage && tab === "overview" ? <p className="conn-settings-empty">{row.desc || row.label}</p>
757
  : !isAdmin ? <p className="conn-setup-refusal">Only an admin can manage connector keys.</p>
758
  : row.key === "generic" ? <GenericSecretsSetup isAdmin={isAdmin} />
759
  : row.key === "whatsapp" ? <WhatsAppSenderTabs onChanged={onChanged} />
 
1002
  | null;
1003
  if (dead) return;
1004
  if (!res.ok) {
1005
+ const message = res.status === 403
1006
+ ? "You do not have permission to view connectors."
1007
+ : (body?.error?.message || `The server answered ${res.status}.`);
1008
  setDirectoryError(message);
1009
  setLoad((current) => current.phase === "ready" ? current : { phase: "error", message });
1010
  return;
 
1443
  );
1444
  }
1445
 
1446
+ /** Keep failed-source notices visible even when no cards could be read. */
 
 
 
 
 
 
 
 
 
 
 
 
 
1447
  export function DirectoryGaps({ gaps }: { gaps: ConnectorGap[] }) {
1448
  if (gaps.length === 0) return null;
1449
+ const sentence = (text: string) => {
1450
+ const content = text.trim().replace(/[.!?]+$/, "");
1451
+ return content ? `${content}.` : "";
1452
+ };
1453
  return (
1454
+ <div className="conn-note conn-directory-gaps" role="status">
 
1455
  <ul>
1456
  {gaps.map((g) => (
1457
  <li key={g.source + g.cause}>
1458
+ {[sentence(g.cause), sentence(g.recommendation)].filter(Boolean).join(" ")}
 
 
 
1459
  </li>
1460
  ))}
1461
  </ul>
web/src/connectors/connectors.css CHANGED
@@ -1,4 +1,19 @@
1
  /* S70-T07, DESIGN.md sections 2 to 5 and C1. All dimensions derive from house tokens. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  .conn-settings-overview .conn-settings-header {
3
  flex-direction: column;
4
  align-items: stretch;
 
1
  /* S70-T07, DESIGN.md sections 2 to 5 and C1. All dimensions derive from house tokens. */
2
+ .conn-note.conn-directory-gaps {
3
+ color: var(--lp-ink);
4
+ background: transparent;
5
+ }
6
+
7
+ .conn-directory-gaps ul {
8
+ margin: 0;
9
+ padding: 0;
10
+ list-style: none;
11
+ }
12
+
13
+ .conn-directory-gaps li + li {
14
+ margin-top: var(--lp-r-sm);
15
+ }
16
+
17
  .conn-settings-overview .conn-settings-header {
18
  flex-direction: column;
19
  align-items: stretch;
web/src/customer-grid/AmazonPeriodPicker.tsx CHANGED
@@ -100,9 +100,7 @@ function PeriodSelectField({ status, pending, busy, choose, notesHost }: {
100
  choose: (key: string) => Promise<void>; notesHost?: HTMLElement | null }) {
101
  const choices = status.periods.some((p) => p.key === status.period.key)
102
  ? status.periods : [{ key: status.period.key, label: status.period.label }, ...status.periods];
103
- const notes = <>
104
- {busy && <span className="cg-period-busy">Updating</span>}
105
- </>;
106
  return (
107
  <div className="cg-period-picker" data-testid="amazon-period-picker">
108
  <label>
@@ -118,7 +116,7 @@ function PeriodSelectField({ status, pending, busy, choose, notesHost }: {
118
  {choices.map((p) => <option key={p.key} value={p.key}>{p.label}</option>)}
119
  </select>
120
  </label>
121
- {notesHost ? createPortal(<span className="cg-period-notes">{notes}</span>, notesHost) : notes}
122
  </div>
123
  );
124
  }
 
100
  choose: (key: string) => Promise<void>; notesHost?: HTMLElement | null }) {
101
  const choices = status.periods.some((p) => p.key === status.period.key)
102
  ? status.periods : [{ key: status.period.key, label: status.period.label }, ...status.periods];
103
+ const notes = busy ? <span className="cg-period-busy">Updating</span> : null;
 
 
104
  return (
105
  <div className="cg-period-picker" data-testid="amazon-period-picker">
106
  <label>
 
116
  {choices.map((p) => <option key={p.key} value={p.key}>{p.label}</option>)}
117
  </select>
118
  </label>
119
+ {notes && (notesHost ? createPortal(<span className="cg-period-notes">{notes}</span>, notesHost) : notes)}
120
  </div>
121
  );
122
  }
web/src/customer-grid/CustomerGrid.tsx CHANGED
@@ -205,6 +205,7 @@ import { avatarInitials, barCellRenderer, dotCellRenderer, formatDisplay, imageC
205
  matchChoices, pickerTypeSeed, ratingCellRenderer, setAvatarRepaint, userCellRenderer } from "./cells";
206
  import { chartCellRenderer } from "./sparklineCell";
207
  import { asButtonCell, buttonCellRenderer, makeProgressCell, progressCellRenderer } from "./cells";
 
208
  import type { AppCellActions } from "./types";
209
  // S82-T20 (R6, R33): the App-only product cell.
210
  import { isProductCell, productCellRenderer, resolveRowHeight, withProductRow } from "./cells";
@@ -2223,6 +2224,10 @@ interface CustomerGridProps {
2223
  /** C3, R33, App only: a listed field's cells draw as buttons and a click or Enter calls the
2224
  * App's door instead of the Grid's picker, link window or editor. Pass a stable object. */
2225
  appCellActions?: AppCellActions;
 
 
 
 
2226
  /** C3 App-only (S82-T68): no View switcher, and a saved map or swipe mode opens as the grid. */
2227
  hideModeSwitch?: boolean;
2228
  /** C3 App-only (S82-T68): an App control drawn in the mode switch's slot, just left of Fields. */
@@ -2704,6 +2709,8 @@ function CustomerGridSurface({
2704
  toolbarLead,
2705
  onPictureClick,
2706
  hideNewRecord = false,
 
 
2707
  appCellActions,
2708
  }: CustomerGridProps = {}) {
2709
  // S72-T63 / C3: `address` is the hosted mode. Its location lives in this instance, not in
@@ -5891,7 +5898,8 @@ function CustomerGridSurface({
5891
  const labelKey = field.format?.labelKey;
5892
  const drawn = field.format?.display === "progress" && field.type === "pct"
5893
  ? makeProgressCell(field, base.kind === GridCellKind.Number ? base.data : undefined,
5894
- labelKey ? record?.[labelKey] as string | number | null | undefined : undefined)
 
5895
  : base;
5896
  return appCellActions?.[field.key] ? asButtonCell(drawn) : drawn;
5897
  }
@@ -5904,7 +5912,7 @@ function CustomerGridSurface({
5904
  if (share === undefined) return base;
5905
  return { ...base, displayData: withShare(base.displayData, share, field.showAs) };
5906
  },
5907
- [rawGetCellContent, visibleCols, fieldByKey, summaryMap, shareTotals, appCellActions, displayRows]
5908
  );
5909
  /**
5910
  * ⭐⭐ W50-T05 — **"BOTH PUTS THE SHARE UNDER THE VALUE IN MUTED INK", WHICH IS THE PRD'S DESIGN
@@ -11558,6 +11566,11 @@ function CustomerGridSurface({
11558
  {catalogData ? `${catalogData.rows.length.toLocaleString()} ${catalogData.rows.length === 1 ? "record" : "records"}${isRoutingApplication ? "" : " on this page"}`
11559
  : isRoutingApplication ? "Loading records" : "Loading page"}
11560
  </span>}
 
 
 
 
 
11561
  </> : cohortAction}
11562
  filterSeed={filterSeed}
11563
  modeControl={toolbarModeSlot}
@@ -12015,7 +12028,7 @@ function CustomerGridSurface({
12015
  </div>
12016
  ) : (
12017
  <>
12018
- <div className="cg-drawer-launchers" aria-label="Workspace details">{isUserTable && <button type="button" className="cg-btn" aria-expanded={workspaceDrawer === "history"} onClick={() => setWorkspaceDrawer((open) => open === "history" ? null : "history")}>History</button>}</div>
12019
  {/* ⭐⭐ W51-T06 CLAUSE 5 — THE PINNED RECORD IS THE **FIRST CHIP IN THE FILTER BAR**.
12020
  ⛔⛔ IT IS DRAWN FROM A PROP AND IS NEVER IN THE FILTER TREE, WHICH IS WHAT KEEPS
12021
  CLAUSE 8 TRUE. `config.filters` is what `persistView` serialises into the target
 
205
  matchChoices, pickerTypeSeed, ratingCellRenderer, setAvatarRepaint, userCellRenderer } from "./cells";
206
  import { chartCellRenderer } from "./sparklineCell";
207
  import { asButtonCell, buttonCellRenderer, makeProgressCell, progressCellRenderer } from "./cells";
208
+ import type { AppProgressColors } from "./cells";
209
  import type { AppCellActions } from "./types";
210
  // S82-T20 (R6, R33): the App-only product cell.
211
  import { isProductCell, productCellRenderer, resolveRowHeight, withProductRow } from "./cells";
 
2224
  /** C3, R33, App only: a listed field's cells draw as buttons and a click or Enter calls the
2225
  * App's door instead of the Grid's picker, link window or editor. Pass a stable object. */
2226
  appCellActions?: AppCellActions;
2227
+ /** App-only progress fills. Absent, keep the Table's current paint. */
2228
+ appProgressColors?: AppProgressColors;
2229
+ /** App-only History placement. Absent, keep the Table's top-right launcher. */
2230
+ appHistoryInToolbar?: boolean;
2231
  /** C3 App-only (S82-T68): no View switcher, and a saved map or swipe mode opens as the grid. */
2232
  hideModeSwitch?: boolean;
2233
  /** C3 App-only (S82-T68): an App control drawn in the mode switch's slot, just left of Fields. */
 
2709
  toolbarLead,
2710
  onPictureClick,
2711
  hideNewRecord = false,
2712
+ appProgressColors,
2713
+ appHistoryInToolbar = false,
2714
  appCellActions,
2715
  }: CustomerGridProps = {}) {
2716
  // S72-T63 / C3: `address` is the hosted mode. Its location lives in this instance, not in
 
5898
  const labelKey = field.format?.labelKey;
5899
  const drawn = field.format?.display === "progress" && field.type === "pct"
5900
  ? makeProgressCell(field, base.kind === GridCellKind.Number ? base.data : undefined,
5901
+ labelKey ? record?.[labelKey] as string | number | null | undefined : undefined,
5902
+ appProgressColors)
5903
  : base;
5904
  return appCellActions?.[field.key] ? asButtonCell(drawn) : drawn;
5905
  }
 
5912
  if (share === undefined) return base;
5913
  return { ...base, displayData: withShare(base.displayData, share, field.showAs) };
5914
  },
5915
+ [rawGetCellContent, visibleCols, fieldByKey, summaryMap, shareTotals, appCellActions, displayRows, appProgressColors]
5916
  );
5917
  /**
5918
  * ⭐⭐ W50-T05 — **"BOTH PUTS THE SHARE UNDER THE VALUE IN MUTED INK", WHICH IS THE PRD'S DESIGN
 
11566
  {catalogData ? `${catalogData.rows.length.toLocaleString()} ${catalogData.rows.length === 1 ? "record" : "records"}${isRoutingApplication ? "" : " on this page"}`
11567
  : isRoutingApplication ? "Loading records" : "Loading page"}
11568
  </span>}
11569
+ </> : appHistoryInToolbar ? <>
11570
+ {cohortAction}
11571
+ {isUserTable && <button type="button" className="cg-tb-btn cg-app-history"
11572
+ aria-expanded={workspaceDrawer === "history"}
11573
+ onClick={() => setWorkspaceDrawer((open) => open === "history" ? null : "history")}>History</button>}
11574
  </> : cohortAction}
11575
  filterSeed={filterSeed}
11576
  modeControl={toolbarModeSlot}
 
12028
  </div>
12029
  ) : (
12030
  <>
12031
+ {!appHistoryInToolbar && <div className="cg-drawer-launchers" aria-label="Workspace details">{isUserTable && <button type="button" className="cg-btn" aria-expanded={workspaceDrawer === "history"} onClick={() => setWorkspaceDrawer((open) => open === "history" ? null : "history")}>History</button>}</div>}
12032
  {/* ⭐⭐ W51-T06 CLAUSE 5 — THE PINNED RECORD IS THE **FIRST CHIP IN THE FILTER BAR**.
12033
  ⛔⛔ IT IS DRAWN FROM A PROP AND IS NEVER IN THE FILTER TREE, WHICH IS WHAT KEEPS
12034
  CLAUSE 8 TRUE. `config.filters` is what `persistView` serialises into the target
web/src/customer-grid/cells.ts CHANGED
@@ -469,6 +469,11 @@ export const barCellRenderer: CustomRenderer<BarCell> = {
469
 
470
  // --- progress: a manual percent as a bar plus a sibling field's text (R19) --
471
 
 
 
 
 
 
472
  export interface ProgressCellData {
473
  kind: "aios-progress";
474
  /** null = blank, draws `emptyLabel` in muted text. */
@@ -476,19 +481,22 @@ export interface ProgressCellData {
476
  /** The `format.labelKey` field's text from the same row, for example "2 of 3 completed". */
477
  label: string;
478
  emptyLabel: string;
 
 
479
  }
480
  export type ProgressCell = CustomCell<ProgressCellData>;
481
 
482
  const PROGRESS_TRACK_W = 72;
483
 
484
  /** A `pct` field with `format.display` "progress". The caller passes the `labelKey` value from the row. */
485
- export function makeProgressCell(field: Field, v: CellValue, label: CellValue): GridCell {
486
  const n = numericIsBlank(v) ? null : num(v);
487
  const text = label == null ? "" : String(label);
488
  const emptyLabel = field.format?.emptyLabel ?? "";
489
  return {
490
  kind: GridCellKind.Custom,
491
- data: { kind: "aios-progress", value: n, label: text, emptyLabel } satisfies ProgressCellData,
 
492
  copyData: n === null ? "" : String(n),
493
  allowOverlay: false,
494
  readonly: true,
@@ -521,7 +529,9 @@ function drawProgress(args: DrawArgs, data: ProgressCellData, left: number, righ
521
  if (fillW > 0) {
522
  ctx.beginPath();
523
  ctx.roundRect(left, ty, Math.max(fillW, BAR_TRACK_H), BAR_TRACK_H, r);
524
- ctx.fillStyle = LP_GREEN_DEEP;
 
 
525
  ctx.fill();
526
  }
527
  }
 
469
 
470
  // --- progress: a manual percent as a bar plus a sibling field's text (R19) --
471
 
472
+ export interface AppProgressColors {
473
+ readonly inProgress: string;
474
+ readonly completed: string;
475
+ }
476
+
477
  export interface ProgressCellData {
478
  kind: "aios-progress";
479
  /** null = blank, draws `emptyLabel` in muted text. */
 
481
  /** The `format.labelKey` field's text from the same row, for example "2 of 3 completed". */
482
  label: string;
483
  emptyLabel: string;
484
+ /** App-only colours. Absent, keep the Table's green fill. */
485
+ colors?: AppProgressColors;
486
  }
487
  export type ProgressCell = CustomCell<ProgressCellData>;
488
 
489
  const PROGRESS_TRACK_W = 72;
490
 
491
  /** A `pct` field with `format.display` "progress". The caller passes the `labelKey` value from the row. */
492
+ export function makeProgressCell(field: Field, v: CellValue, label: CellValue, colors?: AppProgressColors): GridCell {
493
  const n = numericIsBlank(v) ? null : num(v);
494
  const text = label == null ? "" : String(label);
495
  const emptyLabel = field.format?.emptyLabel ?? "";
496
  return {
497
  kind: GridCellKind.Custom,
498
+ data: { kind: "aios-progress", value: n, label: text, emptyLabel,
499
+ ...(colors ? { colors } : {}) } satisfies ProgressCellData,
500
  copyData: n === null ? "" : String(n),
501
  allowOverlay: false,
502
  readonly: true,
 
529
  if (fillW > 0) {
530
  ctx.beginPath();
531
  ctx.roundRect(left, ty, Math.max(fillW, BAR_TRACK_H), BAR_TRACK_H, r);
532
+ ctx.fillStyle = data.colors
533
+ ? (data.value >= 100 ? data.colors.completed : data.colors.inProgress)
534
+ : LP_GREEN_DEEP;
535
  ctx.fill();
536
  }
537
  }
web/src/customer-grid/windows.ts CHANGED
@@ -270,7 +270,9 @@ export function endingBounds(
270
  n = 0, unit: string = "month",
271
  ): [string, string] | null {
272
  if (!(TO_DATE_KINDS as readonly string[]).includes(kind)) return null;
273
- const end = resolveEnding(mode, value, today, step, offset, n, unit);
 
 
274
  if (!end) return null;
275
  const start = resolveWindow({ kind }, today)![0]!;
276
  return start <= end ? [start, end] : null;
 
270
  n = 0, unit: string = "month",
271
  ): [string, string] | null {
272
  if (!(TO_DATE_KINDS as readonly string[]).includes(kind)) return null;
273
+ const endingToday = kind === "ytd_last_year" && mode !== "fixed" && mode !== "cell"
274
+ ? resolveWindow({ kind }, today)![1]! : today;
275
+ const end = resolveEnding(mode, value, endingToday, step, offset, n, unit);
276
  if (!end) return null;
277
  const start = resolveWindow({ kind }, today)![0]!;
278
  return start <= end ? [start, end] : null;
web/src/settings/ActivityLogPage.tsx CHANGED
@@ -1,69 +1,58 @@
1
- import { useEffect, useId, useState } from "react";
2
- import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT } from "../apiContract";
 
3
  import { isAdmin } from "../shell/session";
4
  import type { SessionUser } from "../shell/session";
5
- import type { ActivityEvent, ActivityTarget, MovementSample } from "./activityLog";
 
6
  import "./activityLog.css";
7
 
8
- interface Limits {
9
- retentionDays: number;
10
- maxSessions: number;
11
- maxEventsPerSession: number;
12
- maxMovementSamples: number;
13
- movementWindowSeconds: number;
14
- pageSize: number;
15
- }
16
  interface SessionRow {
17
  sessionId: string;
18
- startedAt: number;
19
- endedAt: number;
20
- route: string;
21
- events: number;
22
- errors: number;
23
  dropped: number;
24
  prunedEvents: number;
25
  }
26
- interface SessionList { sessions: SessionRow[]; limits: Limits }
27
  interface Timeline {
28
- sessionId: string;
29
  events: ActivityEvent[];
30
  total: number;
31
  offset: number;
32
  limit: number;
33
- dropped: number;
34
- prunedEvents: number;
35
- limits: Limits;
36
  }
37
- type ApiResult<T> = { ok: true; data: T } | { ok: false; message: string };
38
 
39
- async function call<T>(path: string, signal: AbortSignal): Promise<ApiResult<T>> {
40
- try {
41
- const response = await fetch(`${API_V1}${path}`, { credentials: CREDENTIALS, signal });
42
- if (response.status === 401) window.dispatchEvent(new CustomEvent(UNAUTHORIZED_EVENT));
43
- if (!response.ok) return { ok: false, message: response.status === 403
44
- ? "You do not have access to this activity."
45
- : response.status === 404 ? "This session is no longer available." : "Activity could not be loaded." };
46
- return { ok: true, data: await response.json() as T };
47
- } catch { return { ok: false, message: "The server could not be reached." }; }
48
  }
49
 
50
- const number = (value: number) => value.toLocaleString();
51
- const clock = (ts: number) => new Date(ts).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" });
52
- const stamp = (ts: number) => new Date(ts).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" });
53
- const duration = (ms: number) => ms < 60_000 ? `${number(Math.round(ms / 1000))} s` : `${number(Math.floor(ms / 60_000))} min ${Math.floor(ms / 1000) % 60} s`;
54
- const elapsed = (ms?: number) => `${number(ms ?? 0)} ms`;
 
 
 
 
 
55
 
56
- function targetName(target?: ActivityTarget): string {
57
- if (!target) return "Element";
58
- const label = target["aria-label"];
59
- if (label) return `${target.tagName}: ${label}`;
60
- if (target.id) return `${target.tagName}#${target.id}`;
61
- if (target.data?.["data-section"] && target.data["data-section"] !== "present") return `${target.tagName}: ${target.data["data-section"]}`;
62
- if (target.role) return `${target.tagName} (${target.role})`;
63
- if (target.className) return `${target.tagName}.${target.className.split(" ").join(".")}`;
64
- return target.tagName;
65
  }
66
 
 
 
 
 
 
67
  function movementDetail(sample: MovementSample): string {
68
  if (sample.kind === "keypress") return sample.key ?? "Key";
69
  if (sample.kind === "pointer" || sample.kind === "scroll") return `${number(sample.x ?? 0)}, ${number(sample.y ?? 0)}`;
@@ -72,160 +61,215 @@ function movementDetail(sample: MovementSample): string {
72
 
73
  function Skeleton() {
74
  return <div className="activity-loading set-skeleton" role="status" aria-label="Loading activity" aria-busy="true">
75
- {[0, 1, 2, 3].map(row => <span key={row} className="set-skeleton-line" aria-hidden="true" />)}
76
  </div>;
77
  }
78
 
79
- function ErrorRow({ event, limits }: { event: ActivityEvent; limits: Limits }) {
 
80
  const [expanded, setExpanded] = useState(false);
81
  const [samplePage, setSamplePage] = useState(0);
82
  const regionId = useId();
83
  const buttonId = useId();
84
  const samples = event.movement ?? [];
85
  const sampleSize = 50;
86
- return <li className="activity-event activity-event-error">
87
- <button type="button" id={buttonId} className="activity-button activity-error-toggle set-secondary"
88
- aria-expanded={expanded} aria-controls={regionId} onClick={() => setExpanded(open => !open)}>
89
- <time className="activity-number" dateTime={new Date(event.ts).toISOString()}>{clock(event.ts)}</time>
90
- <span className="activity-event-copy"><span className="activity-kind">Error</span><span title="Dynamic error text is omitted to keep cell values private.">{event.name}: {event.message}</span>
91
- {event.source ? <span className="activity-source">{event.source}{event.line ? `:${event.line}` : ""}</span> : null}</span>
92
- <svg className={"activity-chevron" + (expanded ? " is-open" : "")} viewBox="0 0 16 16" aria-hidden="true"><path d="m6 3 5 5-5 5" /></svg>
93
- </button>
 
 
 
 
 
 
 
 
 
 
 
 
94
  {expanded ? <div id={regionId} role="region" aria-labelledby={buttonId} className="activity-trail">
95
- <h2 className="set-h4">Before the error</h2>
96
- <p className="set-help">Up to {number(limits.movementWindowSeconds)} seconds and {number(limits.maxMovementSamples)} movement samples.</p>
97
  {event.movementDropped ? <p className="set-notice" role="status">{number(event.movementDropped)} samples were omitted by the capture limits.</p> : null}
98
  {samples.length === 0 ? <p className="set-help">No movement was captured before this error.</p> : <>
99
  <ol className="activity-movement">
100
  {samples.slice(samplePage * sampleSize, (samplePage + 1) * sampleSize).map((sample, index) => <li key={`${sample.ts}-${index}`}>
101
  <span className="activity-number">{Math.max(0, (event.ts - sample.ts) / 1000).toFixed(1)} s before</span>
102
  <span className="activity-sample-kind">{sample.kind}</span>
103
- <span className="activity-target" title={sample.target.path?.join(" > ")}>{targetName(sample.target)}</span>
104
  <span className="activity-number">{movementDetail(sample)}</span>
105
  </li>)}
106
  </ol>
107
- {samples.length > sampleSize ? <div className="activity-toolbar">
108
  <span className="set-help">Samples {number(samplePage * sampleSize + 1)} to {number(Math.min((samplePage + 1) * sampleSize, samples.length))} of {number(samples.length)}</span>
109
- <button type="button" className="activity-button activity-control set-secondary" disabled={samplePage === 0} onClick={() => setSamplePage(page => page - 1)}>Earlier samples</button>
110
- <button type="button" className="activity-button activity-control set-secondary" disabled={(samplePage + 1) * sampleSize >= samples.length} onClick={() => setSamplePage(page => page + 1)}>Later samples</button>
111
  </div> : null}
112
  </>}
113
  </div> : null}
114
  </li>;
115
  }
116
 
117
- function EventRow({ event, limits }: { event: ActivityEvent; limits: Limits }) {
118
- if (event.kind === "error") return <ErrorRow event={event} limits={limits} />;
119
- return <li className="activity-event activity-action">
120
- <time className="activity-number" dateTime={new Date(event.ts).toISOString()}>{clock(event.ts)}</time>
121
- <span className="activity-event-copy">
122
- <span className="activity-kind">{event.kind === "api" ? "Request" : event.kind === "route" ? "Route" : "Click"}</span>
123
- {event.kind === "route" ? <span>{event.from || "Start"} to {event.to}</span> : null}
124
- {event.kind === "click" ? <span title={event.target?.path?.join(" > ")}>{targetName(event.target)}</span> : null}
125
- {event.kind === "api" ? <span>{event.method?.toLowerCase()} {event.url}</span> : null}
126
- </span>
127
- <span className="activity-number activity-metrics">
128
- {event.kind === "api" ? <span>{event.status === 0 ? "Network failure" : `Status ${event.status}`}</span> : null}
129
- {event.kind !== "click" ? <span>{elapsed(event.ms)}</span> : null}
130
- </span>
131
- </li>;
132
- }
133
-
134
  function ActivityPane({ user }: { user: SessionUser }) {
135
  const admin = isAdmin(user);
136
- const [selectedUser, setSelectedUser] = useState(user.username);
137
- const owner = admin ? selectedUser : user.username;
138
  const [users, setUsers] = useState<string[]>([user.username]);
139
- const [usersError, setUsersError] = useState("");
140
- const [list, setList] = useState<SessionList | null>(null);
141
- const [selected, setSelected] = useState<SessionRow | null>(null);
142
- const [timeline, setTimeline] = useState<Timeline | null>(null);
143
- const [offset, setOffset] = useState(0);
144
  const [revision, setRevision] = useState(0);
145
  const [loading, setLoading] = useState(true);
146
- const [error, setError] = useState("");
 
 
 
 
 
147
 
148
  useEffect(() => {
149
- if (!admin) return;
150
  const controller = new AbortController();
151
- void call<{ users: string[] }>("/activity/users", controller.signal).then(result => {
152
- if (controller.signal.aborted) return;
153
- setUsersError(result.ok ? "" : result.message);
154
- if (result.ok) setUsers(Array.from(new Set([user.username, ...result.data.users])));
155
- });
156
- return () => controller.abort();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  }, [admin, user.username, revision]);
158
 
159
- useEffect(() => {
160
- const controller = new AbortController();
161
- setLoading(true);
162
- setError("");
163
- const query = admin ? `user=${encodeURIComponent(owner)}&` : "";
164
- const path = selected
165
- ? `/activity/sessions/${encodeURIComponent(selected.sessionId)}?${query}limit=100&offset=${offset}`
166
- : `/activity/sessions?${query}`;
167
- void call<SessionList | Timeline>(path, controller.signal).then(result => {
168
- if (controller.signal.aborted) return;
169
- setLoading(false);
170
- if (!result.ok) { setError(result.message); return; }
171
- if (selected) setTimeline(result.data as Timeline);
172
- else setList(result.data as SessionList);
173
- });
174
- return () => controller.abort();
175
- }, [admin, owner, selected, offset, revision]);
176
-
177
- const limits = selected ? timeline?.limits ?? list?.limits : list?.limits;
178
- const refresh = () => { setLoading(true); setError(""); setRevision(value => value + 1); };
179
- function open(row: SessionRow | null) {
180
- setSelected(row); setTimeline(null); setOffset(0); setLoading(true); setError("");
181
  }
182
 
183
  return <div className="set-pane activity-pane">
184
- <h3 className="set-h">Activity log</h3>
185
- <div className="activity-toolbar">
186
- {admin ? <label className="activity-user"><span className="set-label">User</span>
187
- <select className="pg-select" value={owner} onChange={event => { setSelectedUser(event.target.value); setList(null); open(null); }}>
188
- {users.map(username => <option key={username} value={username}>{username}</option>)}
189
- </select>
190
- </label> : null}
191
- {selected ? <button type="button" className="activity-button activity-control set-secondary" onClick={() => open(null)}>Sessions</button> : null}
192
- <button type="button" className="activity-button activity-control set-primary" onClick={refresh} disabled={loading}>Refresh</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  </div>
194
- {usersError ? <div className="set-error" role="alert"><span>{usersError}</span>
195
- <button type="button" className="set-secondary" onClick={refresh}>Retry</button></div> : null}
196
- {limits ? <p className="set-help activity-retention">Last {number(limits.retentionDays)} days, up to {number(limits.maxSessions)} sessions per workspace and {number(limits.maxEventsPerSession)} events per session.</p> : null}
197
- {error ? <div className="set-error" role="alert"><span>{error}</span><button type="button" className="set-secondary" onClick={refresh}>Retry</button></div>
198
- : loading ? <Skeleton />
199
- : selected && timeline ? <>
200
- <div className="activity-heading"><h2 className="set-h4">Session timeline</h2><span className="set-help">{stamp(selected.startedAt)} · {selected.route}</span></div>
201
- {timeline.prunedEvents > 0 ? <p className="set-notice" role="status">{number(timeline.prunedEvents)} older events were removed; the newest {number(timeline.limits.maxEventsPerSession)} are kept.</p> : null}
202
- {timeline.dropped > 0 ? <p className="set-notice" role="status">{number(timeline.dropped)} events could not be captured or saved.</p> : null}
203
- {timeline.events.length ? <ol className="activity-timeline" aria-label="Session actions">
204
- {timeline.events.map((event, index) => <EventRow key={event.eventId ?? `${event.ts}-${index}`} event={event} limits={timeline.limits} />)}
205
- </ol> : <p className="set-help">No events on this page.</p>}
206
- <div className="activity-toolbar activity-pagination">
207
- <span className="set-help">Events {number(timeline.total ? timeline.offset + 1 : 0)} to {number(Math.min(timeline.offset + timeline.events.length, timeline.total))} of {number(timeline.total)}; {number(timeline.limit)} per page.</span>
208
- <button type="button" className="activity-button activity-control set-secondary" disabled={offset === 0} onClick={() => { setLoading(true); setOffset(Math.max(0, offset - timeline.limit)); }}>Previous</button>
209
- <button type="button" className="activity-button activity-control set-secondary" disabled={offset + timeline.limit >= timeline.total} onClick={() => { setLoading(true); setOffset(offset + timeline.limit); }}>Next</button>
210
- </div>
211
- </> : list?.sessions.length ? <>
212
- <h2 className="set-h4">Sessions</h2>
213
- <ol className="activity-sessions" aria-label="Activity sessions">
214
- {list.sessions.map(row => <li key={row.sessionId}>
215
- <button type="button" className="activity-button activity-session set-secondary" onClick={() => open(row)}>
216
- <time className="activity-number" dateTime={new Date(row.startedAt).toISOString()}>{stamp(row.startedAt)}</time>
217
- <span className="activity-session-route">{row.route}</span>
218
- <span className="activity-number">{duration(row.endedAt - row.startedAt)}</span>
219
- <span className="activity-number">{number(row.events)} events</span>
220
- <span className={"activity-number" + (row.errors > 0 ? " activity-negative" : "")}>{number(row.errors)} errors</span>
221
- </button>
222
- </li>)}
223
- </ol>
224
- </> : <div className="activity-empty"><p className="set-help">No sessions yet.</p><button type="button" className="activity-button activity-control" onClick={refresh}>Refresh</button></div>}
225
  </div>;
226
  }
227
 
228
  export default function ActivityLogPage({ user }: { user: SessionUser }) {
229
- // Remount all fetched state if the signed-in identity changes without a reload.
230
  return <ActivityPane key={`${user.tenant}:${user.username}:${user.role}`} user={user} />;
231
  }
 
1
+ import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
2
+ import { ArrowClockwise, ArrowsLeftRight, CaretDown, CaretRight, CursorClick, Globe, MagnifyingGlass, WarningCircle } from "@phosphor-icons/react";
3
+ import { API_V1, CREDENTIALS, checkTenant, UNAUTHORIZED_EVENT } from "../apiContract";
4
  import { isAdmin } from "../shell/session";
5
  import type { SessionUser } from "../shell/session";
6
+ import { ACTIVITY_ACTIONS, activityDescription, activityTargetName, filterActivity, groupActivityByDay } from "./activityLog";
7
+ import type { ActivityEntry, ActivityEvent, ActivityFilters, ActivityLimits, ActivityTimeframe, MovementSample } from "./activityLog";
8
  import "./activityLog.css";
9
 
 
 
 
 
 
 
 
 
10
  interface SessionRow {
11
  sessionId: string;
 
 
 
 
 
12
  dropped: number;
13
  prunedEvents: number;
14
  }
15
+ interface SessionList { sessions: SessionRow[]; limits: ActivityLimits }
16
  interface Timeline {
 
17
  events: ActivityEvent[];
18
  total: number;
19
  offset: number;
20
  limit: number;
21
+ limits: ActivityLimits;
 
 
22
  }
 
23
 
24
+ class ActivityReadError extends Error {
25
+ readonly denied: boolean;
26
+ readonly missing: boolean;
27
+ constructor(message: string, denied = false, missing = false) {
28
+ super(message); this.denied = denied; this.missing = missing;
29
+ }
 
 
 
30
  }
31
 
32
+ async function call<T>(path: string, signal: AbortSignal): Promise<T> {
33
+ const response = await fetch(`${API_V1}${path}`, { credentials: CREDENTIALS, signal, cache: "no-store" });
34
+ if (checkTenant(response)) throw new ActivityReadError("Reopen Settings to view this workspace's activity.", true);
35
+ if (response.status === 401) window.dispatchEvent(new CustomEvent(UNAUTHORIZED_EVENT));
36
+ if (!response.ok) throw new ActivityReadError(response.status === 403
37
+ ? "You do not have access to this activity."
38
+ : response.status === 404 ? "This activity session is no longer available." : "Activity could not be loaded.",
39
+ response.status === 401 || response.status === 403, response.status === 404);
40
+ return await response.json() as T;
41
+ }
42
 
43
+ // Bound concurrent reads, never the number of sessions or event pages.
44
+ async function readEach<T>(items: T[], read: (item: T) => Promise<void>, signal: AbortSignal): Promise<void> {
45
+ let next = 0;
46
+ await Promise.all(Array.from({ length: Math.min(4, items.length) }, async () => {
47
+ while (next < items.length && !signal.aborted) await read(items[next++]);
48
+ }));
 
 
 
49
  }
50
 
51
+ const PAGE_SIZE = 50;
52
+ const DEFAULT_FILTERS: ActivityFilters = { search: "", actor: "", action: "all", timeframe: "all" };
53
+ const number = (value: number) => value.toLocaleString();
54
+ const clock = (ts: number) => new Date(ts).toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
55
+
56
  function movementDetail(sample: MovementSample): string {
57
  if (sample.kind === "keypress") return sample.key ?? "Key";
58
  if (sample.kind === "pointer" || sample.kind === "scroll") return `${number(sample.x ?? 0)}, ${number(sample.y ?? 0)}`;
 
61
 
62
  function Skeleton() {
63
  return <div className="activity-loading set-skeleton" role="status" aria-label="Loading activity" aria-busy="true">
64
+ {[0, 1, 2, 3, 4].map(row => <span key={row} className="set-skeleton-line" aria-hidden="true" />)}
65
  </div>;
66
  }
67
 
68
+ function EventRow({ entry }: { entry: ActivityEntry }) {
69
+ const { event, actor, limits } = entry;
70
  const [expanded, setExpanded] = useState(false);
71
  const [samplePage, setSamplePage] = useState(0);
72
  const regionId = useId();
73
  const buttonId = useId();
74
  const samples = event.movement ?? [];
75
  const sampleSize = 50;
76
+ const description = activityDescription(event);
77
+ const Icon = event.kind === "error" ? WarningCircle : event.kind === "click" ? CursorClick : event.kind === "api" ? ArrowsLeftRight : Globe;
78
+ const content = <>
79
+ <span className={`activity-event-icon${event.kind === "error" ? " is-error" : ""}`}><Icon size={18} aria-hidden="true" /></span>
80
+ <span className="activity-event-copy">
81
+ <span className="activity-description"><span className="activity-kind">{description.action}</span>{" "}
82
+ <span className="activity-target" title={event.target?.path?.join(" > ")}>{description.target}</span></span>
83
+ <span className="activity-event-meta"><time dateTime={new Date(event.ts).toISOString()} title={new Date(event.ts).toLocaleString()}>{clock(event.ts)}</time>
84
+ {event.kind === "api" ? <span>{event.status === 0 ? "Network failure" : `Status ${event.status}`}</span> : null}
85
+ {event.kind === "error" && event.source ? <span>{event.source}{event.line ? `:${event.line}` : ""}</span> : null}
86
+ </span>
87
+ </span>
88
+ <span className="activity-actor" title={actor}>{actor}</span>
89
+ {event.kind === "error" ? <CaretRight size={16} className={`activity-chevron${expanded ? " is-open" : ""}`} aria-hidden="true" /> : null}
90
+ </>;
91
+
92
+ return <li className={`activity-event${event.kind === "error" ? " activity-event-error" : ""}`}>
93
+ {event.kind === "error" ? <button type="button" id={buttonId} className="activity-entry activity-entry-toggle"
94
+ aria-expanded={expanded} aria-controls={expanded ? regionId : undefined} onClick={() => setExpanded(open => !open)}>{content}</button>
95
+ : <div className="activity-entry">{content}</div>}
96
  {expanded ? <div id={regionId} role="region" aria-labelledby={buttonId} className="activity-trail">
97
+ <h4 className="set-h4">Before the error</h4>
98
+ <p className="set-help">Up to {number(limits.movementWindowSeconds)} seconds and {number(limits.maxMovementSamples)} movement samples</p>
99
  {event.movementDropped ? <p className="set-notice" role="status">{number(event.movementDropped)} samples were omitted by the capture limits.</p> : null}
100
  {samples.length === 0 ? <p className="set-help">No movement was captured before this error.</p> : <>
101
  <ol className="activity-movement">
102
  {samples.slice(samplePage * sampleSize, (samplePage + 1) * sampleSize).map((sample, index) => <li key={`${sample.ts}-${index}`}>
103
  <span className="activity-number">{Math.max(0, (event.ts - sample.ts) / 1000).toFixed(1)} s before</span>
104
  <span className="activity-sample-kind">{sample.kind}</span>
105
+ <span className="activity-target" title={sample.target.path?.join(" > ")}>{activityTargetName(sample.target)}</span>
106
  <span className="activity-number">{movementDetail(sample)}</span>
107
  </li>)}
108
  </ol>
109
+ {samples.length > sampleSize ? <div className="activity-pagination">
110
  <span className="set-help">Samples {number(samplePage * sampleSize + 1)} to {number(Math.min((samplePage + 1) * sampleSize, samples.length))} of {number(samples.length)}</span>
111
+ <button type="button" className="set-secondary" disabled={samplePage === 0} onClick={() => setSamplePage(page => page - 1)}>Earlier samples</button>
112
+ <button type="button" className="set-secondary" disabled={(samplePage + 1) * sampleSize >= samples.length} onClick={() => setSamplePage(page => page + 1)}>Later samples</button>
113
  </div> : null}
114
  </>}
115
  </div> : null}
116
  </li>;
117
  }
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  function ActivityPane({ user }: { user: SessionUser }) {
120
  const admin = isAdmin(user);
 
 
121
  const [users, setUsers] = useState<string[]>([user.username]);
122
+ const [entries, setEntries] = useState<ActivityEntry[]>([]);
123
+ const [filters, setFilters] = useState<ActivityFilters>(DEFAULT_FILTERS);
124
+ const [page, setPage] = useState(0);
 
 
125
  const [revision, setRevision] = useState(0);
126
  const [loading, setLoading] = useState(true);
127
+ const [problem, setProblem] = useState("");
128
+ const [denied, setDenied] = useState(false);
129
+ const [omitted, setOmitted] = useState({ dropped: 0, pruned: 0 });
130
+ const resultsRef = useRef<HTMLDivElement>(null);
131
+ const headingId = useId();
132
+ const resultsId = useId();
133
 
134
  useEffect(() => {
 
135
  const controller = new AbortController();
136
+ const signal = controller.signal;
137
+ setLoading(true); setProblem(""); setDenied(false); setEntries([]); setPage(0);
138
+ const clearIdentity = () => {
139
+ controller.abort(); setEntries([]); setUsers([user.username]); setLoading(false); setProblem(""); setDenied(true);
140
+ };
141
+ window.addEventListener(UNAUTHORIZED_EVENT, clearIdentity);
142
+ async function load() {
143
+ try {
144
+ const owners = admin
145
+ ? Array.from(new Set([user.username, ...(await call<{ users: string[] }>("/activity/users", signal)).users]))
146
+ : [user.username];
147
+ if (signal.aborted) return;
148
+ setUsers(owners);
149
+ const sessions: Array<{ actor: string; row: SessionRow; limits: ActivityLimits }> = [];
150
+ await readEach(owners, async actor => {
151
+ const query = admin ? `?user=${encodeURIComponent(actor)}` : "";
152
+ const list = await call<SessionList>(`/activity/sessions${query}`, signal);
153
+ for (const row of list.sessions) sessions.push({ actor, row, limits: list.limits });
154
+ }, signal);
155
+ const loaded: ActivityEntry[] = [];
156
+ await readEach(sessions, async ({ actor, row, limits }) => {
157
+ let offset = 0;
158
+ let total: number | undefined;
159
+ const sessionEntries: ActivityEntry[] = [];
160
+ try {
161
+ while (!signal.aborted && (total === undefined || offset < total)) {
162
+ const query = new URLSearchParams({ limit: "100", offset: String(offset) });
163
+ if (admin) query.set("user", actor);
164
+ const timeline = await call<Timeline>(`/activity/sessions/${encodeURIComponent(row.sessionId)}?${query}`, signal);
165
+ for (const event of timeline.events) sessionEntries.push({ event, actor, sessionId: row.sessionId, limits: timeline.limits ?? limits });
166
+ // Advance over the server slice even if sanitization omitted an event.
167
+ const next = timeline.offset + timeline.limit;
168
+ if (next <= offset) throw new ActivityReadError("Activity could not be loaded.");
169
+ offset = next;
170
+ // Freeze the session's extent so live capture cannot extend this read forever.
171
+ total ??= timeline.total;
172
+ }
173
+ } catch (error) {
174
+ // Retention can remove a session between listing it and reading its pages.
175
+ if (error instanceof ActivityReadError && error.missing) return;
176
+ throw error;
177
+ }
178
+ for (const entry of sessionEntries) loaded.push(entry);
179
+ }, signal);
180
+ if (signal.aborted) return;
181
+ loaded.sort((a, b) => b.event.ts - a.event.ts || a.actor.localeCompare(b.actor));
182
+ setEntries(loaded);
183
+ setOmitted(sessions.reduce((sum, { row }) => ({ dropped: sum.dropped + row.dropped, pruned: sum.pruned + row.prunedEvents }), { dropped: 0, pruned: 0 }));
184
+ setLoading(false);
185
+ } catch (error) {
186
+ if (signal.aborted) return;
187
+ controller.abort();
188
+ setEntries([]); setLoading(false);
189
+ setDenied(error instanceof ActivityReadError && error.denied);
190
+ setProblem(error instanceof ActivityReadError ? error.message : "Activity could not be loaded.");
191
+ }
192
+ }
193
+ void load();
194
+ return () => { controller.abort(); window.removeEventListener(UNAUTHORIZED_EVENT, clearIdentity); };
195
  }, [admin, user.username, revision]);
196
 
197
+ const filtered = useMemo(() => filterActivity(entries, filters), [entries, filters]);
198
+ const lastPage = Math.max(0, Math.ceil(filtered.length / PAGE_SIZE) - 1);
199
+ const currentPage = Math.min(page, lastPage);
200
+ const offset = currentPage * PAGE_SIZE;
201
+ const groups = groupActivityByDay(filtered.slice(offset, offset + PAGE_SIZE));
202
+ const hasFilters = Boolean(filters.search || filters.actor || filters.action !== "all" || filters.timeframe !== "all");
203
+ useLayoutEffect(() => {
204
+ // Reset after rendering so scroll anchoring cannot hide the filters.
205
+ resultsRef.current?.closest(".set-body")?.scrollTo({ top: 0, behavior: "instant" });
206
+ }, [currentPage, filters]);
207
+ function updateFilters(patch: Partial<ActivityFilters>) { setFilters(value => ({ ...value, ...patch })); setPage(0); }
208
+ function changePage(next: number) {
209
+ setPage(next);
210
+ resultsRef.current?.focus({ preventScroll: true });
 
 
 
 
 
 
 
 
211
  }
212
 
213
  return <div className="set-pane activity-pane">
214
+ <h3 className="set-h" id={headingId}>Activity log</h3>
215
+ <div className="activity-toolbar" role="group" aria-label="Activity filters">
216
+ <label className="activity-search"><MagnifyingGlass size={16} aria-hidden="true" />
217
+ <input type="search" aria-label="Search activity" placeholder="Search activity" value={filters.search}
218
+ onChange={event => updateFilters({ search: event.target.value })} aria-controls={resultsId} />
219
+ </label>
220
+ <label className={`activity-filter${filters.actor ? " is-active" : ""}`}>
221
+ <select aria-label="User" value={admin ? filters.actor : user.username} disabled={!admin || denied} aria-controls={resultsId}
222
+ onChange={event => updateFilters({ actor: event.target.value })}>
223
+ {admin ? <option value="">User</option> : null}
224
+ {users.map(actor => <option key={actor} value={actor}>{`User: ${actor}`}</option>)}
225
+ </select><CaretDown size={12} aria-hidden="true" />
226
+ </label>
227
+ <label className={`activity-filter${filters.action !== "all" ? " is-active" : ""}`}>
228
+ <select aria-label="Action" value={filters.action} aria-controls={resultsId}
229
+ onChange={event => updateFilters({ action: event.target.value as ActivityFilters["action"] })}>
230
+ <option value="all">Action</option>
231
+ {Object.entries(ACTIVITY_ACTIONS).map(([value, label]) => <option key={value} value={value}>{label}</option>)}
232
+ </select><CaretDown size={12} aria-hidden="true" />
233
+ </label>
234
+ <label className={`activity-filter${filters.timeframe !== "all" ? " is-active" : ""}`}>
235
+ <select aria-label="Timeframe" value={filters.timeframe} aria-controls={resultsId}
236
+ onChange={event => updateFilters({ timeframe: event.target.value as ActivityTimeframe })}>
237
+ <option value="all">Timeframe</option><option value="today">Today</option>
238
+ <option value="week">Last 7 days</option><option value="month">Last 30 days</option>
239
+ </select><CaretDown size={12} aria-hidden="true" />
240
+ </label>
241
+ <button type="button" className="activity-clear" disabled={!hasFilters} onClick={() => { setFilters(DEFAULT_FILTERS); setPage(0); }}>Clear</button>
242
+ <button type="button" className="set-secondary activity-refresh" aria-label="Refresh activity" title="Refresh activity" disabled={loading}
243
+ onClick={() => setRevision(value => value + 1)}><ArrowClockwise size={16} aria-hidden="true" /></button>
244
+ </div>
245
+ <div className="activity-results" id={resultsId} ref={resultsRef} tabIndex={-1} role="region" aria-labelledby={headingId} aria-busy={loading}>
246
+ {denied ? <p className="set-notice" role="status">{problem || "You do not have access to this activity."}</p>
247
+ : problem ? <div className="set-error" role="alert"><span>{problem}</span>
248
+ <button type="button" className="set-secondary" onClick={() => setRevision(value => value + 1)}>Retry</button></div>
249
+ : loading ? <Skeleton />
250
+ : !filtered.length ? <div className="activity-empty" role="status"><p className="set-help">{hasFilters ? "No activity matches your filters." : "No activity yet."}</p>
251
+ {hasFilters ? <button type="button" className="set-secondary" onClick={() => { setFilters(DEFAULT_FILTERS); setPage(0); }}>Clear filters</button> : null}</div>
252
+ : <>
253
+ <div className="activity-groups">{groups.map(group => <section className="activity-day" key={group.day} aria-label={group.label}>
254
+ <h4 className="activity-day-heading"><time dateTime={group.day}>{group.label}</time></h4>
255
+ <ol className="activity-timeline" aria-label={`Activity for ${group.label}`}>
256
+ {group.entries.map((entry, index) => <EventRow key={`${entry.actor}:${entry.sessionId}:${entry.event.eventId || `${entry.event.ts}:${offset + index}`}`} entry={entry} />)}
257
+ </ol>
258
+ </section>)}</div>
259
+ <nav className="activity-pagination" aria-label="Activity pages">
260
+ <span className="set-help" role="status">{number(offset + 1)} to {number(Math.min(offset + PAGE_SIZE, filtered.length))} of {number(filtered.length)} entries</span>
261
+ <button type="button" className="set-secondary" disabled={currentPage === 0} onClick={() => changePage(currentPage - 1)}>Previous</button>
262
+ <button type="button" className="set-secondary" disabled={currentPage === lastPage} onClick={() => changePage(currentPage + 1)}>Next</button>
263
+ </nav>
264
+ </>}
265
+ {!loading && !problem && !denied && (omitted.dropped > 0 || omitted.pruned > 0) ? <p className="set-notice activity-retention" role="status">
266
+ {number(omitted.pruned)} older events are no longer available and {number(omitted.dropped)} events could not be captured.
267
+ </p> : null}
268
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  </div>;
270
  }
271
 
272
  export default function ActivityLogPage({ user }: { user: SessionUser }) {
273
+ // Remount fetched state when the signed-in identity changes.
274
  return <ActivityPane key={`${user.tenant}:${user.username}:${user.role}`} user={user} />;
275
  }
web/src/settings/ExternalAccessPane.tsx CHANGED
@@ -262,7 +262,21 @@ interface KeyDraft {
262
  expiryPreset: ExpiryPreset;
263
  }
264
 
265
- type VerifyResult = { state: "ok" | "failed"; message: string };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  type ConfirmedConnection = { clientId: ClientId } & (
267
  { kind: "mcp"; grantId: string } | { kind: "key"; keyId: string }
268
  );
@@ -790,53 +804,52 @@ export default function ExternalAccessPane({
790
  }
791
 
792
  async function verifyKey() {
793
- const secret = verificationReveal?.secret ?? verifySecret.trim();
794
- if (!secret || busy !== "" || connection !== "key" || !selectedKeyIsActive) return;
 
795
  setBusy("verify:key"); setConfirmedConnection(null); setVerifyResult(null); setCopied("");
796
  const version = ++verificationVersion.current;
797
- const report = (state: VerifyResult["state"], message: string) => {
798
  if (version !== verificationVersion.current) return;
799
- // A refusal may include server text; never let a reflected credential reach the result region.
800
- setVerifyResult({ state, message: message.split(secret).join("[redacted]") });
801
- if (state === "ok" && clientId) setConfirmedConnection({ clientId, kind: "key", keyId: selectedKeyId });
 
802
  };
803
  try {
804
- // Omit the cookie so success proves the bearer alone. Do not dispatch UNAUTHORIZED_EVENT
805
- // or call checkTenant: a refused key is expected here and must not end the browser session.
806
- const response = await fetch(`${API_V1}/external/whoami`, {
807
- credentials: "omit",
808
- headers: { Authorization: `Bearer ${secret}` },
809
  });
810
- setVerifySecret("");
811
- let body: unknown = null;
812
- try { body = await response.json(); } catch { /* Report unreadable successes and generic refusals. */ }
813
- if (!response.ok) {
814
- const cause = response.status === 401 ? "The key was refused. It may be wrong, expired or revoked. "
815
- : response.status === 403 ? "The key is valid, but the account has no Analyst module. " : "";
816
- const fallback = response.status >= 500
817
- ? "The AI API is unavailable right now." : "That request could not be completed.";
818
- report("failed", `${response.status}: ${cause}${messageFrom(body, fallback)}`);
 
 
819
  return;
820
  }
821
- const identity = body && typeof body === "object" && !Array.isArray(body)
822
- ? body as Record<string, unknown> : null;
823
- const name = typeof identity?.name === "string" && identity.name.trim() ? identity.name
824
- : typeof identity?.username === "string" ? identity.username : "";
825
- const databases = identity?.databases;
826
- if (!name || (identity?.access !== "read" && identity?.access !== "readwrite")
827
- || (databases !== "all" && (!Array.isArray(databases)
828
- || !databases.every((database) => typeof database === "string")))) {
829
  report("failed", "The AI API returned an unreadable response.");
830
  return;
831
  }
832
- if (identity?.key_id !== selectedKeyId) {
833
  report("failed", "This secret does not match the selected API key.");
834
  return;
835
  }
836
- const accessLabel = identity.access === "read" ? "Read only" : "Read and write";
837
- const scopeLabel = databases === "all" ? "All databases"
838
- : `${databases.length} ${databases.length === 1 ? "database" : "databases"}`;
839
- report("ok", `API key accepted for ${name}. ${accessLabel}. ${scopeLabel}.`);
 
840
  } catch {
841
  report("failed", "The server could not be reached.");
842
  } finally {
@@ -1138,7 +1151,7 @@ export default function ExternalAccessPane({
1138
  <p className="set-help">{view === "client" ? "No active API keys." : "No saved key is active. Create one or update its expiry below."}</p>
1139
  ) : null}
1140
  {canMint === false ? (
1141
- <p className="set-help">Creating API keys requires the Analyst module. Ask an administrator to grant it.</p>
1142
  ) : null}
1143
 
1144
  {(shownKeyChoice === "create" || view === "api") && canMint === true ? (
@@ -1171,7 +1184,7 @@ export default function ExternalAccessPane({
1171
  onChange={(event) => chooseKey(event.currentTarget.value)}>
1172
  <option value="">Choose a key</option>
1173
  {keys.map((key) => (
1174
- <option key={key.id} value={key.id} disabled={!keyIsActive(key)}>
1175
  {key.label} · {key.access === "read" ? "Read only" : "Read and write"} · {key.expires_at ? "Expires " + when(key.expires_at) : "No expiry"}
1176
  {key.revoked_at ? " (Revoked)" : !keyIsActive(key) ? " (Expired)" : ""}
1177
  </option>
@@ -1194,12 +1207,11 @@ export default function ExternalAccessPane({
1194
  {connection === "key" ? (
1195
  <>
1196
  {selectedKey ? <p className="set-help">Key: {selectedKey.label}</p> : null}
1197
- {!selectedKeyIsActive ? <p className="set-help">{sharedSteps.verification.activeKey}</p> : null}
1198
  {verificationReveal ? <p className="set-help">{sharedSteps.verification.newKey}</p> : (
1199
  <div className="set-field">
1200
  <label className="set-label" htmlFor="external-verify-key">Saved API key</label>
1201
  <input id="external-verify-key" className="set-input" type="password" value={verifySecret}
1202
- autoComplete="off" spellCheck={false} disabled={busy !== "" || !selectedKeyIsActive}
1203
  aria-describedby="external-verify-key-help"
1204
  onChange={(event) => { resetVerification(); setVerifySecret(event.currentTarget.value); }} />
1205
  <p id="external-verify-key-help" className="set-help">{sharedSteps.verification.savedKey}</p>
@@ -1213,7 +1225,7 @@ export default function ExternalAccessPane({
1213
  <div className="set-actions">
1214
  {connection === "key" ? (
1215
  <button type="button" className="set-primary" data-verify="key"
1216
- disabled={busy !== "" || !selectedKeyIsActive || !(verificationReveal?.secret || verifySecret.trim())}
1217
  onClick={() => void verifyKey()}>{busy === "verify:key" ? "Verifying…" : "Verify API key"}</button>
1218
  ) : (
1219
  <button type="button" className="set-primary" data-verify="mcp" disabled={busy !== ""}
@@ -1221,7 +1233,8 @@ export default function ExternalAccessPane({
1221
  )}
1222
  </div>
1223
  {verifyResult ? (
1224
- <p className="external-verification-result" role="status" data-verify-result data-verify-state={verifyResult.state}>
 
1225
  {verifyResult.message}
1226
  </p>
1227
  ) : null}
@@ -1234,6 +1247,37 @@ export default function ExternalAccessPane({
1234
 
1235
  {revealCard}
1236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1237
  {view !== "api" && visibleConnections.length > 0 ? (
1238
  /* HOTFIX 2026-09-24 (owner: the AI connections list "is really ugly, let's make it look
1239
  like a Grid view"). One row per connection in the Data grid's own look: row numbers,
 
262
  expiryPreset: ExpiryPreset;
263
  }
264
 
265
+ const KEY_REFUSALS = {
266
+ minted_elsewhere: "This key was minted on another Space. Create a key on this Space",
267
+ revoked: "This key was revoked. Create a new key",
268
+ expired: "This key expired. Update its expiry or create a new key",
269
+ unknown_key: "This key is unknown to your account on this Space. Paste your saved key or create a new one",
270
+ } as const;
271
+ type KeyRefusal = keyof typeof KEY_REFUSALS;
272
+ interface OwnerKeyVerification {
273
+ valid: boolean;
274
+ cause: KeyRefusal | null;
275
+ warning: "store_write_refused" | null;
276
+ origin_recorded: boolean | null;
277
+ key_id?: string;
278
+ }
279
+ type VerifyResult = { state: "ok" | "failed"; message: string; cause?: KeyRefusal | "store_write_refused" };
280
  type ConfirmedConnection = { clientId: ClientId } & (
281
  { kind: "mcp"; grantId: string } | { kind: "key"; keyId: string }
282
  );
 
804
  }
805
 
806
  async function verifyKey() {
807
+ const useSelectedKey = view === "client";
808
+ const secret = (useSelectedKey ? verificationReveal?.secret : undefined) ?? verifySecret.trim();
809
+ if (!secret || busy !== "" || canMint !== true) return;
810
  setBusy("verify:key"); setConfirmedConnection(null); setVerifyResult(null); setCopied("");
811
  const version = ++verificationVersion.current;
812
+ const report = (state: VerifyResult["state"], message: string, cause?: VerifyResult["cause"]) => {
813
  if (version !== verificationVersion.current) return;
814
+ setVerifyResult({ state, message, cause });
815
+ if (state === "ok" && useSelectedKey && clientId && selectedKeyIsActive) {
816
+ setConfirmedConnection({ clientId, kind: "key", keyId: selectedKeyId });
817
+ }
818
  };
819
  try {
820
+ const result = await call<OwnerKeyVerification>("/external/keys/verify", {
821
+ method: "POST",
822
+ headers: { "Content-Type": "application/json" },
823
+ body: JSON.stringify({ token: secret }),
 
824
  });
825
+ if (version !== verificationVersion.current) return;
826
+ if (!result.ok) {
827
+ report("failed", result.status === 403
828
+ ? "Verifying API keys requires AI apps access. Ask an administrator for access"
829
+ : "Could not verify the API key. Try again");
830
+ return;
831
+ }
832
+ const diagnosis = result.data;
833
+ if (diagnosis?.valid === false && diagnosis.cause
834
+ && Object.prototype.hasOwnProperty.call(KEY_REFUSALS, diagnosis.cause)) {
835
+ report("failed", KEY_REFUSALS[diagnosis.cause], diagnosis.cause);
836
  return;
837
  }
838
+ if (diagnosis?.valid !== true || diagnosis.cause !== null
839
+ || typeof diagnosis.origin_recorded !== "boolean" || typeof diagnosis.key_id !== "string"
840
+ || (diagnosis.warning !== null && diagnosis.warning !== "store_write_refused")) {
 
 
 
 
 
841
  report("failed", "The AI API returned an unreadable response.");
842
  return;
843
  }
844
+ if (useSelectedKey && selectedKeyId && diagnosis.key_id !== selectedKeyId) {
845
  report("failed", "This secret does not match the selected API key.");
846
  return;
847
  }
848
+ const warning = diagnosis.warning === "store_write_refused"
849
+ ? ". The last-used store write was refused. Retry Verify to save it" : "";
850
+ const origin = diagnosis.origin_recorded ? ""
851
+ : ". This legacy key has no recorded Space origin. Its minting Space cannot be proved";
852
+ report("ok", `API key accepted${warning}${origin}`, diagnosis.warning ?? undefined);
853
  } catch {
854
  report("failed", "The server could not be reached.");
855
  } finally {
 
1151
  <p className="set-help">{view === "client" ? "No active API keys." : "No saved key is active. Create one or update its expiry below."}</p>
1152
  ) : null}
1153
  {canMint === false ? (
1154
+ <p className="set-help">Creating API keys requires AI apps access. Ask an administrator for access</p>
1155
  ) : null}
1156
 
1157
  {(shownKeyChoice === "create" || view === "api") && canMint === true ? (
 
1184
  onChange={(event) => chooseKey(event.currentTarget.value)}>
1185
  <option value="">Choose a key</option>
1186
  {keys.map((key) => (
1187
+ <option key={key.id} value={key.id}>
1188
  {key.label} · {key.access === "read" ? "Read only" : "Read and write"} · {key.expires_at ? "Expires " + when(key.expires_at) : "No expiry"}
1189
  {key.revoked_at ? " (Revoked)" : !keyIsActive(key) ? " (Expired)" : ""}
1190
  </option>
 
1207
  {connection === "key" ? (
1208
  <>
1209
  {selectedKey ? <p className="set-help">Key: {selectedKey.label}</p> : null}
 
1210
  {verificationReveal ? <p className="set-help">{sharedSteps.verification.newKey}</p> : (
1211
  <div className="set-field">
1212
  <label className="set-label" htmlFor="external-verify-key">Saved API key</label>
1213
  <input id="external-verify-key" className="set-input" type="password" value={verifySecret}
1214
+ autoComplete="off" spellCheck={false} disabled={busy !== "" || canMint !== true}
1215
  aria-describedby="external-verify-key-help"
1216
  onChange={(event) => { resetVerification(); setVerifySecret(event.currentTarget.value); }} />
1217
  <p id="external-verify-key-help" className="set-help">{sharedSteps.verification.savedKey}</p>
 
1225
  <div className="set-actions">
1226
  {connection === "key" ? (
1227
  <button type="button" className="set-primary" data-verify="key"
1228
+ disabled={busy !== "" || canMint !== true || !(verificationReveal?.secret || verifySecret.trim())}
1229
  onClick={() => void verifyKey()}>{busy === "verify:key" ? "Verifying…" : "Verify API key"}</button>
1230
  ) : (
1231
  <button type="button" className="set-primary" data-verify="mcp" disabled={busy !== ""}
 
1233
  )}
1234
  </div>
1235
  {verifyResult ? (
1236
+ <p className={`external-verification-result ${verifyResult.state === "failed" ? "set-error" : "set-notice"}`}
1237
+ role="status" data-verify-result data-verify-state={verifyResult.state} data-verify-cause={verifyResult.cause}>
1238
  {verifyResult.message}
1239
  </p>
1240
  ) : null}
 
1247
 
1248
  {revealCard}
1249
 
1250
+ {view !== "client" ? <section className="set-card" data-owner-key-verify aria-labelledby="external-verify-title"
1251
+ aria-busy={busy === "verify:key"}>
1252
+ <h2 id="external-verify-title" className="set-h4">Verify</h2>
1253
+ {keysError ? <div className="set-error" role="alert">
1254
+ <span>Could not load API key access</span>
1255
+ <button type="button" className="set-secondary" onClick={() => void load()}>Retry</button>
1256
+ </div> : canMint === null ? <SettingsSkeleton label="Loading Verify access" />
1257
+ : canMint === false ? <p className="set-help">
1258
+ Verifying API keys requires AI apps access. Ask an administrator for access
1259
+ </p> : <form onSubmit={(event) => { event.preventDefault(); void verifyKey(); }}>
1260
+ <div className="set-field">
1261
+ <label className="set-label" htmlFor="external-owner-verify-key">API key</label>
1262
+ <input id="external-owner-verify-key" className="set-input" type="password" value={verifySecret}
1263
+ autoComplete="off" spellCheck={false} disabled={busy !== ""}
1264
+ aria-describedby="external-owner-verify-help"
1265
+ onChange={(event) => { resetVerification(); setVerifySecret(event.currentTarget.value); }} />
1266
+ <p id="external-owner-verify-help" className="set-help">Paste your saved key, including a revoked or expired key</p>
1267
+ </div>
1268
+ <div className="set-actions">
1269
+ <button type="submit" className="set-primary" data-verify="key"
1270
+ disabled={busy !== "" || !verifySecret.trim()}>
1271
+ {busy === "verify:key" ? "Verifying…" : "Verify API key"}
1272
+ </button>
1273
+ </div>
1274
+ </form>}
1275
+ {verifyResult ? <p className={verifyResult.state === "failed" ? "set-error" : "set-notice"}
1276
+ role="status" data-verify-result data-verify-state={verifyResult.state} data-verify-cause={verifyResult.cause}>
1277
+ {verifyResult.message}
1278
+ </p> : null}
1279
+ </section> : null}
1280
+
1281
  {view !== "api" && visibleConnections.length > 0 ? (
1282
  /* HOTFIX 2026-09-24 (owner: the AI connections list "is really ugly, let's make it look
1283
  like a Grid view"). One row per connection in the Data grid's own look: row numbers,
web/src/settings/NotifyDestinations.tsx CHANGED
@@ -1,6 +1,9 @@
1
  import { useCallback, useEffect, useId, useRef, useState } from "react";
2
  import type { FormEvent } from "react";
 
 
3
  import { Busy } from "../ui/Busy";
 
4
  import { connectorSetupRoute } from "./permsModel";
5
  import {
6
  getNotificationDestinations,
@@ -15,12 +18,36 @@ import type {
15
  NotificationSlackChannel,
16
  } from "./settingsApi";
17
  import "../styles/sdlc81/notify-destinations.css";
 
18
 
19
  type Picks = Record<string, NotificationDestinationPick>;
20
  type Editor = { server: NotificationDestinationsPayload | null; drafts: Picks };
21
  type Reason = { reason: string; message: string };
22
  type SourcedReason = Reason & { source: "current" | "lastAttempt" };
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  function picks(row: NotificationDestinationPick): NotificationDestinationPick {
25
  return { slackChannels: [...row.slackChannels], email: row.email?.trim() || null };
26
  }
@@ -36,7 +63,6 @@ function samePicks(a: NotificationDestinationPick, b: NotificationDestinationPic
36
  function refreshEditor(previous: Editor, server: NotificationDestinationsPayload, saved: Picks): Editor {
37
  const drafts = { ...previous.drafts };
38
  for (const row of server.destinations) {
39
- if (row.kind === "fixture") continue;
40
  const draft = drafts[row.kind];
41
  const baseline = previous.server?.destinations.find((item) => item.kind === row.kind);
42
  // Refresh clean or acknowledged drafts. Every other edit belongs to the user.
@@ -92,6 +118,7 @@ function DestinationReasons({ id, reasons, email = false }: { id: string; reason
92
  export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {}) {
93
  const id = useId();
94
  const [editor, setEditor] = useState<Editor>({ server: null, drafts: {} });
 
95
  const [channels, setChannels] = useState<NotificationSlackChannel[]>([]);
96
  const [slackConfigured, setSlackConfigured] = useState<boolean | null>(null);
97
  const [loading, setLoading] = useState(true);
@@ -102,6 +129,8 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
102
  const [saving, setSaving] = useState(false);
103
  const [needsRefresh, setNeedsRefresh] = useState(false);
104
  const [feedback, setFeedback] = useState("");
 
 
105
  const scope = useRef<AbortController | null>(null);
106
  const flight = useRef(false);
107
  const saved = useRef<Picks>({});
@@ -113,9 +142,11 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
113
  if (signal.aborted) return false;
114
  setLoading(false);
115
  if (result.ok === false) {
116
- setReadError(result.message);
 
117
  return false;
118
  }
 
119
  const acknowledged = saved.current;
120
  setEditor((previous) => refreshEditor(previous, result.data, acknowledged));
121
  saved.current = {};
@@ -132,7 +163,7 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
132
  const result = await getNotificationSlackChannels(after, signal);
133
  if (signal.aborted) return;
134
  if (result.ok === false) {
135
- setChannelsError(result.message);
136
  setChannelsLoading(false);
137
  return;
138
  }
@@ -145,7 +176,7 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
145
  return;
146
  }
147
  if (page.nextCursor <= after) {
148
- setChannelsError("Slack channels could not be loaded. Try again.");
149
  setChannelsLoading(false);
150
  return;
151
  }
@@ -164,8 +195,23 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
164
  };
165
  }, [loadChannels, refresh]);
166
 
167
- const kinds = editor.server?.destinations.filter((row) => row.kind !== "fixture") ?? [];
168
  const changed = kinds.filter((row) => editor.drafts[row.kind] && !samePicks(row, editor.drafts[row.kind]));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  function edit(kind: string, patch: Partial<NotificationDestinationPick>) {
171
  setEditor((previous) => ({
@@ -183,7 +229,7 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
183
  async function save(event: FormEvent<HTMLFormElement>) {
184
  event.preventDefault();
185
  const signal = scope.current?.signal;
186
- if (!signal || signal.aborted || flight.current || loading || needsRefresh || !changed.length) return;
187
  flight.current = true;
188
  setSaving(true);
189
  setFeedback("");
@@ -208,7 +254,12 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
208
  }
209
  if (Object.keys(saved.current).length) {
210
  const refreshed = await refresh(signal);
211
- if (!signal.aborted) setFeedback(refreshed ? (failed ? "" : "Saved") : "Saved picks need a status refresh.");
 
 
 
 
 
212
  }
213
  } finally {
214
  if (!signal.aborted) {
@@ -221,7 +272,7 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
221
  return (
222
  <div className="set-pane notify-destinations" aria-busy={loading || saving}>
223
  <h3 className="set-title set-h">Notifications</h3>
224
- {readError ? (
225
  <div className="notify-error" role="alert">
226
  <p>{readError}</p>
227
  <button type="button" className="set-secondary notify-retry" disabled={loading || saving}
@@ -229,12 +280,12 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
229
  </div>
230
  ) : null}
231
  {!editor.server && loading ? (
232
- <div className="set-skeleton" role="status" aria-label="Loading notifications">
233
- <span className="set-skeleton-line" aria-hidden="true" />
234
- <span className="set-skeleton-line" aria-hidden="true" />
235
- <span className="set-skeleton-line" aria-hidden="true" />
236
  </div>
237
- ) : editor.server ? (
238
  <form className="notify-form" onSubmit={(event) => void save(event)}>
239
  <div className="notify-slack-status">
240
  {slackConfigured === false ? (
@@ -243,7 +294,7 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
243
  <div className="set-skeleton notify-channel-loading" role="status" aria-label="Loading Slack channels">
244
  <span className="set-skeleton-line" aria-hidden="true" />
245
  </div>
246
- ) : !channelsError && channels.length === 0 ? <p>No Slack channels available</p> : null}
247
  {channelsError ? (
248
  <div className="notify-error" role="alert">
249
  <p>{channelsError}</p>
@@ -252,73 +303,113 @@ export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {
252
  </div>
253
  ) : null}
254
  </div>
255
- {kinds.map((row) => {
256
- const draft = editor.drafts[row.kind];
257
- const kindId = `${id}-${row.kind}`;
258
- const options = new Map(channels.map((channel) => [channel.channel, channel]));
259
- // Saved ids remain visible and removable even when discovery no longer lists them.
260
- const channelIds = [...new Set([...draft.slackChannels, ...row.slackChannels, ...options.keys()])];
261
- const emailDestination = `email:${draft.email?.trim() || ""}`;
262
- const emailReadiness = row.readiness[emailDestination] ?? editor.server?.email;
263
- const emailReasons = draft.email?.trim()
264
- ? destinationReasons(row, emailDestination, emailReadiness)
265
- : [];
266
- return (
267
- <section key={row.kind} className="notify-kind" data-kind={row.kind} aria-labelledby={`${kindId}-title`}>
268
- <h4 id={`${kindId}-title`} className="set-h4 notify-kind-title">{row.label}</h4>
269
- <fieldset className="notify-channels">
270
- <legend>Slack channels</legend>
271
- <ul className="notify-channel-list">
272
- {channelIds.map((channelId) => {
273
- const option = options.get(channelId);
274
- const selected = draft.slackChannels.includes(channelId);
275
- const unavailable = slackConfigured !== true || !option?.active;
276
- const destination = `slack:${channelId}`;
277
- let extra: Reason | undefined;
278
- if (slackConfigured === false) extra = { reason: "not_configured", message: "" };
279
- else if (option?.active === false) extra = { reason: "channel_inactive", message: "This channel is inactive." };
280
- else if (!option) extra = channelsLoading || channelsError
281
- ? { reason: "channel_unchecked", message: "Channel availability could not be checked." }
282
- : { reason: "channel_unavailable", message: "This channel is no longer available." };
283
- const reasons = destinationReasons(row, destination, row.readiness[destination], extra);
284
- const reasonId = `${kindId}-${channelId}-reason`;
285
- const label = option?.channelName
286
- ? (option.channelName.startsWith("#") ? option.channelName : `#${option.channelName}`)
287
- : option?.label || channelId;
288
- return (
289
- <li key={channelId} className="notify-channel" data-destination={destination}>
 
 
 
290
  <label className="notify-choice">
291
- <input type="checkbox" className="notify-channel-pick" value={channelId}
292
- checked={selected} disabled={saving || (!selected && unavailable)}
293
- aria-describedby={reasons.length ? reasonId : undefined}
294
- onChange={(event) => edit(row.kind, {
295
- slackChannels: event.target.checked
296
- ? [...draft.slackChannels, channelId]
297
- : draft.slackChannels.filter((value) => value !== channelId),
298
- })} />
299
- <span>{label}</span>
300
  </label>
301
- <DestinationReasons id={reasonId} reasons={reasons} />
302
- {selected && !row.slackChannels.includes(channelId) && reasons.length === 0
303
- ? <p className="notify-pending">Save to check delivery</p> : null}
304
- </li>
305
- );
306
- })}
307
- </ul>
308
- </fieldset>
309
- <div className="notify-email-field" data-destination={emailDestination}>
310
- <label htmlFor={`${kindId}-email`}>Email</label>
311
- <input id={`${kindId}-email`} className="notify-email" type="email" autoComplete="email"
312
- maxLength={254} value={draft.email ?? ""} disabled={saving}
313
- aria-describedby={emailReasons.length ? `${kindId}-email-reason` : undefined}
314
- onChange={(event) => edit(row.kind, { email: event.target.value || null })} />
315
- <DestinationReasons id={`${kindId}-email-reason`} reasons={emailReasons} email />
316
- </div>
317
- {writeErrors[row.kind] ? <p className="notify-write-error" role="alert">{writeErrors[row.kind]}</p> : null}
318
- </section>
319
- );
320
- })}
321
- {kinds.length === 0 ? <p className="notify-empty">No notifications available</p> : null}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  <div className="set-actions notify-actions">
323
  <button type="submit" className="set-primary notify-save"
324
  disabled={saving || loading || needsRefresh || changed.length === 0}>
 
1
  import { useCallback, useEffect, useId, useRef, useState } from "react";
2
  import type { FormEvent } from "react";
3
+ import { flushSync } from "react-dom";
4
+ import { CaretRight } from "@phosphor-icons/react";
5
  import { Busy } from "../ui/Busy";
6
+ import { TOAST_EVENT } from "../apiContract";
7
  import { connectorSetupRoute } from "./permsModel";
8
  import {
9
  getNotificationDestinations,
 
18
  NotificationSlackChannel,
19
  } from "./settingsApi";
20
  import "../styles/sdlc81/notify-destinations.css";
21
+ import "./channels/channels.css";
22
 
23
  type Picks = Record<string, NotificationDestinationPick>;
24
  type Editor = { server: NotificationDestinationsPayload | null; drafts: Picks };
25
  type Reason = { reason: string; message: string };
26
  type SourcedReason = Reason & { source: "current" | "lastAttempt" };
27
 
28
+ function category(kind: string): string {
29
+ if (kind === "account_health_new_items" || kind === "amazon_operator_weekly") return "Account";
30
+ if (kind === "restock_digest") return "Inventory";
31
+ if (kind === "fixture") return "Testing";
32
+ return "Workspace";
33
+ }
34
+
35
+ function channelLabel(channel: NotificationSlackChannel | undefined, channelId: string): string {
36
+ return channel?.channelName
37
+ ? (channel.channelName.startsWith("#") ? channel.channelName : `#${channel.channelName}`)
38
+ : channel?.label || channelId;
39
+ }
40
+
41
+ function deliveryState(row: NotificationDestination, destination: string, selected: boolean, pending: boolean,
42
+ reasons: SourcedReason[]): string {
43
+ if (!selected) return "Off";
44
+ if (pending) return "Save to check delivery";
45
+ if (reasons.some((reason) => reason.source === "current")) return "Unavailable";
46
+ if (row.delivery[destination]?.state === "refused") return "Last attempt refused";
47
+ if (row.delivery[destination]?.state === "sent") return "Last attempt sent";
48
+ return "No delivery yet";
49
+ }
50
+
51
  function picks(row: NotificationDestinationPick): NotificationDestinationPick {
52
  return { slackChannels: [...row.slackChannels], email: row.email?.trim() || null };
53
  }
 
63
  function refreshEditor(previous: Editor, server: NotificationDestinationsPayload, saved: Picks): Editor {
64
  const drafts = { ...previous.drafts };
65
  for (const row of server.destinations) {
 
66
  const draft = drafts[row.kind];
67
  const baseline = previous.server?.destinations.find((item) => item.kind === row.kind);
68
  // Refresh clean or acknowledged drafts. Every other edit belongs to the user.
 
118
  export function NotifyDestinations({ onConnect }: { onConnect?: () => void } = {}) {
119
  const id = useId();
120
  const [editor, setEditor] = useState<Editor>({ server: null, drafts: {} });
121
+ const [collapsedCategories, setCollapsedCategories] = useState<Record<string, boolean>>({});
122
  const [channels, setChannels] = useState<NotificationSlackChannel[]>([]);
123
  const [slackConfigured, setSlackConfigured] = useState<boolean | null>(null);
124
  const [loading, setLoading] = useState(true);
 
129
  const [saving, setSaving] = useState(false);
130
  const [needsRefresh, setNeedsRefresh] = useState(false);
131
  const [feedback, setFeedback] = useState("");
132
+ const [noPermission, setNoPermission] = useState(false);
133
+ const emailDrafts = useRef<Record<string, string>>({});
134
  const scope = useRef<AbortController | null>(null);
135
  const flight = useRef(false);
136
  const saved = useRef<Picks>({});
 
142
  if (signal.aborted) return false;
143
  setLoading(false);
144
  if (result.ok === false) {
145
+ setNoPermission(result.status === 401 || result.status === 403);
146
+ setReadError("Notifications could not be loaded.");
147
  return false;
148
  }
149
+ setNoPermission(false);
150
  const acknowledged = saved.current;
151
  setEditor((previous) => refreshEditor(previous, result.data, acknowledged));
152
  saved.current = {};
 
163
  const result = await getNotificationSlackChannels(after, signal);
164
  if (signal.aborted) return;
165
  if (result.ok === false) {
166
+ setChannelsError(result.status === 403 ? "You do not have permission to view Slack channels." : "Slack channels could not be loaded.");
167
  setChannelsLoading(false);
168
  return;
169
  }
 
176
  return;
177
  }
178
  if (page.nextCursor <= after) {
179
+ setChannelsError("Slack channels could not be loaded.");
180
  setChannelsLoading(false);
181
  return;
182
  }
 
195
  };
196
  }, [loadChannels, refresh]);
197
 
198
+ const kinds = editor.server?.destinations ?? [];
199
  const changed = kinds.filter((row) => editor.drafts[row.kind] && !samePicks(row, editor.drafts[row.kind]));
200
+ const groups = new Map<string, NotificationDestination[]>();
201
+ for (const row of kinds) {
202
+ const name = category(row.kind);
203
+ groups.set(name, [...(groups.get(name) ?? []), row]);
204
+ }
205
+ const options = new Map(channels.map((channel) => [channel.channel, channel]));
206
+ // Saved destinations remain removable when their connection or discovery is unavailable.
207
+ const channelIds = [...new Set([
208
+ ...(slackConfigured === true ? options.keys() : []),
209
+ ...kinds.flatMap((row) => [...row.slackChannels, ...(editor.drafts[row.kind]?.slackChannels ?? [])]),
210
+ ])];
211
+ const showEmail = !!editor.server && (editor.server.email.available
212
+ || editor.server.email.reason !== "email_not_configured"
213
+ || kinds.some((row) => row.email !== null || editor.drafts[row.kind]?.email != null));
214
+ const columnCount = 1 + Number(showEmail) + channelIds.length;
215
 
216
  function edit(kind: string, patch: Partial<NotificationDestinationPick>) {
217
  setEditor((previous) => ({
 
229
  async function save(event: FormEvent<HTMLFormElement>) {
230
  event.preventDefault();
231
  const signal = scope.current?.signal;
232
+ if (!signal || signal.aborted || flight.current || loading || noPermission || needsRefresh || !changed.length) return;
233
  flight.current = true;
234
  setSaving(true);
235
  setFeedback("");
 
254
  }
255
  if (Object.keys(saved.current).length) {
256
  const refreshed = await refresh(signal);
257
+ if (!signal.aborted) {
258
+ setFeedback(refreshed ? "" : "Saved picks need a status refresh.");
259
+ if (refreshed && !failed) window.dispatchEvent(new CustomEvent(TOAST_EVENT, {
260
+ detail: { text: "Notification preferences saved", tone: "info" },
261
+ }));
262
+ }
263
  }
264
  } finally {
265
  if (!signal.aborted) {
 
272
  return (
273
  <div className="set-pane notify-destinations" aria-busy={loading || saving}>
274
  <h3 className="set-title set-h">Notifications</h3>
275
+ {noPermission ? <p className="notify-empty" role="status">You do not have permission to view notifications</p> : readError ? (
276
  <div className="notify-error" role="alert">
277
  <p>{readError}</p>
278
  <button type="button" className="set-secondary notify-retry" disabled={loading || saving}
 
280
  </div>
281
  ) : null}
282
  {!editor.server && loading ? (
283
+ <div className="set-skeleton notify-matrix-skeleton" role="status" aria-label="Loading notifications">
284
+ {Array.from({ length: 6 }, (_, index) => <div className="notify-skeleton-row" key={index} aria-hidden="true">
285
+ <span className="set-skeleton-line" /><span className="set-skeleton-line" /><span className="set-skeleton-line" />
286
+ </div>)}
287
  </div>
288
+ ) : editor.server && !noPermission ? (
289
  <form className="notify-form" onSubmit={(event) => void save(event)}>
290
  <div className="notify-slack-status">
291
  {slackConfigured === false ? (
 
294
  <div className="set-skeleton notify-channel-loading" role="status" aria-label="Loading Slack channels">
295
  <span className="set-skeleton-line" aria-hidden="true" />
296
  </div>
297
+ ) : !channelsError && slackConfigured === true && channels.length === 0 ? <p>No Slack channels available</p> : null}
298
  {channelsError ? (
299
  <div className="notify-error" role="alert">
300
  <p>{channelsError}</p>
 
303
  </div>
304
  ) : null}
305
  </div>
306
+ <div className="notify-matrix-scroll" role="region" aria-label="Notification preferences" tabIndex={0}>
307
+ <table className="set-table notify-matrix" aria-label="Notification preferences by channel">
308
+ <thead><tr>
309
+ <th scope="col">Notification type</th>
310
+ {showEmail ? <th scope="col" className="notify-channel-heading">Email</th> : null}
311
+ {channelIds.map((channelId) => <th scope="col" className="notify-channel-heading" key={channelId}>
312
+ Slack<span className="notify-channel-name">{channelLabel(options.get(channelId), channelId)}</span>
313
+ </th>)}
314
+ </tr></thead>
315
+ {[...groups].map(([name, rows]) => <tbody key={name} onInvalidCapture={() => {
316
+ // Reveal invalid drafts before native form validation moves focus.
317
+ if (collapsedCategories[name]) flushSync(() => setCollapsedCategories((previous) => ({ ...previous, [name]: false })));
318
+ }}>
319
+ <tr className="notify-category"><th scope="rowgroup" colSpan={columnCount}>
320
+ <button type="button" className="notify-category-toggle"
321
+ aria-expanded={!collapsedCategories[name]}
322
+ aria-controls={rows.map((row) => `${id}-${row.kind}-row`).join(" ")}
323
+ onClick={() => setCollapsedCategories((previous) => ({ ...previous, [name]: !previous[name] }))}>
324
+ <CaretRight size={16} className="notify-category-caret" aria-hidden="true" />
325
+ {name}
326
+ </button>
327
+ </th></tr>
328
+ {rows.map((row) => {
329
+ const draft = editor.drafts[row.kind] ?? picks(row);
330
+ const kindId = `${id}-${row.kind}`;
331
+ const emailDestination = `email:${draft.email?.trim() || ""}`;
332
+ const emailReadiness = row.readiness[emailDestination] ?? editor.server?.email;
333
+ const emailReasons = destinationReasons(row, emailDestination, emailReadiness);
334
+ const emailState = deliveryState(row, emailDestination, draft.email !== null,
335
+ draft.email !== row.email, emailReasons);
336
+ return (
337
+ <tr key={row.kind} id={`${kindId}-row`} data-kind={row.kind} hidden={!!collapsedCategories[name]}>
338
+ <th scope="row" id={`${kindId}-title`} className="notify-event">
339
+ {row.label}
340
+ {writeErrors[row.kind] ? <p className="notify-write-error" role="alert">{writeErrors[row.kind]}</p> : null}
341
+ </th>
342
+ {showEmail ? <td className="notify-matrix-cell" data-destination={emailDestination}>
343
+ <div className="notify-cell-content">
344
  <label className="notify-choice">
345
+ <input type="checkbox" className="notify-channel-pick" checked={draft.email !== null}
346
+ disabled={saving || loading || needsRefresh || (draft.email === null && !editor.server?.email.available)}
347
+ aria-label={`${row.label}: email`}
348
+ aria-describedby={`${kindId}-email-state${emailReasons.length ? ` ${kindId}-email-reason` : ""}`}
349
+ onChange={(event) => {
350
+ if (draft.email) emailDrafts.current[row.kind] = draft.email;
351
+ edit(row.kind, { email: event.target.checked ? emailDrafts.current[row.kind] || row.email || "" : null });
352
+ }} />
 
353
  </label>
354
+ {draft.email !== null ? <input id={`${kindId}-email`} className="notify-email" type="email"
355
+ aria-label={`${row.label}: email address`} placeholder="Email address" autoComplete="email" required
356
+ maxLength={254} value={draft.email} disabled={saving || loading || needsRefresh}
357
+ aria-describedby={`${kindId}-email-state${emailReasons.length ? ` ${kindId}-email-reason` : ""}`}
358
+ onChange={(event) => edit(row.kind, { email: event.target.value })} /> : null}
359
+ <span id={`${kindId}-email-state`} className="notify-delivery-state"
360
+ data-state={emailState === "Last attempt sent" ? "sent" : undefined}>
361
+ {draft.email === "" ? "Enter an email address" : emailState}
362
+ </span>
363
+ <DestinationReasons id={`${kindId}-email-reason`} reasons={emailReasons} email />
364
+ </div>
365
+ </td> : null}
366
+ {channelIds.map((channelId) => {
367
+ const option = options.get(channelId);
368
+ const selected = draft.slackChannels.includes(channelId);
369
+ const unavailable = slackConfigured !== true || !option?.active;
370
+ const destination = `slack:${channelId}`;
371
+ let extra: Reason | undefined;
372
+ if (slackConfigured === false) extra = { reason: "not_configured", message: "" };
373
+ else if (option?.active === false) extra = { reason: "channel_inactive", message: "This channel is inactive." };
374
+ else if (!option) extra = channelsLoading || channelsError
375
+ ? { reason: "channel_unchecked", message: "Channel availability could not be checked." }
376
+ : { reason: "channel_unavailable", message: "This channel is no longer available." };
377
+ const reasons = destinationReasons(row, destination, row.readiness[destination], extra);
378
+ const reasonId = `${kindId}-${channelId}-reason`;
379
+ const label = channelLabel(option, channelId);
380
+ const state = deliveryState(row, destination, selected, !row.slackChannels.includes(channelId), reasons);
381
+ return (
382
+ <td key={channelId} className="notify-matrix-cell">
383
+ <ul className="notify-channel-list">
384
+ <li className="notify-channel notify-cell-content" data-destination={destination}>
385
+ <label className="notify-choice">
386
+ <input type="checkbox" className="notify-channel-pick" value={channelId}
387
+ checked={selected} disabled={saving || loading || needsRefresh || (!selected && unavailable)}
388
+ aria-label={`${row.label}: Slack ${label}`}
389
+ aria-describedby={`${kindId}-${channelId}-state${reasons.length ? ` ${reasonId}` : ""}`}
390
+ onChange={(event) => edit(row.kind, {
391
+ slackChannels: event.target.checked
392
+ ? [...draft.slackChannels, channelId]
393
+ : draft.slackChannels.filter((value) => value !== channelId),
394
+ })} />
395
+ </label>
396
+ <span id={`${kindId}-${channelId}-state`} className="notify-delivery-state"
397
+ data-state={state === "Last attempt sent" ? "sent" : undefined}>{state}</span>
398
+ <DestinationReasons id={reasonId} reasons={reasons} />
399
+ </li>
400
+ </ul>
401
+ </td>
402
+ );
403
+ })}
404
+ </tr>
405
+ );
406
+ })}
407
+ </tbody>)}
408
+ {kinds.length === 0 ? <tbody><tr><td colSpan={columnCount} className="notify-empty">No notifications available</td></tr></tbody> : null}
409
+ </table>
410
+ </div>
411
+ {columnCount === 1 && kinds.length > 0 && !channelsLoading && !channelsError
412
+ ? <p className="notify-empty" role="status">No notification channels connected</p> : null}
413
  <div className="set-actions notify-actions">
414
  <button type="submit" className="set-primary notify-save"
415
  disabled={saving || loading || needsRefresh || changed.length === 0}>
web/src/settings/SecurityPane.tsx CHANGED
@@ -15,8 +15,7 @@ import { API_V1, CREDENTIALS, checkTenant, currentTenant, signal, TOAST_EVENT, U
15
  import { useEscapeClose } from "../ui/useEscapeClose";
16
  import { CodeInput } from "../ui/CodeInput";
17
  import { encodeQr } from "../shell/qrcode";
18
- import "../styles/wave57/login-mfa.css";
19
- import "../styles/wave59/security.css";
20
 
21
  function SetupQr({ uri }: { uri: string }) {
22
  const drawing = useMemo(() => {
@@ -84,20 +83,20 @@ export function RecoveryCodes({ codes, onSaved, onError }: {
84
 
85
  return (
86
  <>
87
- <p className="login-mfa-help">This screen is the only place these recovery codes will ever be shown.</p>
88
- <p className="login-mfa-help">Save them somewhere safe; each code works once if you lose your authenticator.</p>
89
- <ul className="login-mfa-codes" aria-label="Recovery codes">
90
  {codes.map((code) => <li key={code}><code>{code}</code></li>)}
91
  </ul>
92
- <div className="login-mfa-copy-row">
93
- <button type="button" className="login-mfa-quiet" onClick={() => void copy()} disabled={copying}>
94
  {copying ? "Copying…" : "Copy codes"}
95
  </button>
96
- <span className="login-mfa-copy-status" role="status">{copied ? "Copied" : ""}</span>
97
  </div>
98
  {/* A type=button acknowledgement cannot be submitted by Enter left over
99
  from the previous code field. There is no Back or secondary exit. */}
100
- <button type="button" className="login-submit" onClick={onSaved} disabled={copying}>
101
  I have saved these codes
102
  </button>
103
  </>
@@ -163,12 +162,53 @@ export type EnrolmentGate = {
163
  onBack: () => void;
164
  };
165
 
166
- type PersonalRead = { user: string; tenant: string; table: string; column: string; at: string };
167
- const EVENTS_UNAVAILABLE = "Personal data reads could not be loaded.";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  function SecurityEvents() {
170
- const [user, setUser] = useState<SessionUser | null>(null);
171
- const [events, setEvents] = useState<PersonalRead[]>([]);
172
  const [loading, setLoading] = useState(true);
173
  const [problem, setProblem] = useState("");
174
  const [revision, setRevision] = useState(0);
@@ -179,36 +219,42 @@ function SecurityEvents() {
179
  const clear = () => {
180
  active = false;
181
  controller.abort();
182
- setUser(null); setEvents([]); setProblem("");
183
  };
184
- setUser(null); setEvents([]); setLoading(true); setProblem("");
185
  async function load() {
186
  try {
187
  const identity = await me();
188
- if (!active || !isAdmin(identity) || !identity?.tenant
189
- || (currentTenant() !== null && currentTenant() !== identity.tenant)) return;
190
- setUser(identity);
 
 
 
191
  const response = await fetch(`${API_V1}/security/events`, {
192
  credentials: CREDENTIALS, cache: "no-store", signal: controller.signal,
193
  });
194
  if (!active || checkTenant(response)) return;
195
  if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return; }
196
- if (response.status === 403) { setUser(null); return; }
197
  if (!response.ok) throw new Error(EVENTS_UNAVAILABLE);
198
  const body = await response.json();
199
  if (!active) return;
200
  if (!body || !Array.isArray(body.events)) throw new Error(EVENTS_UNAVAILABLE);
201
- const reads: PersonalRead[] = [];
202
  for (const event of body.events) {
203
- if (!event || event.kind !== "amazon.personal_read" || event.tenant !== identity.tenant) continue;
204
- if (typeof event.principal !== "string" || !event.principal.trim()
205
- || !event.detail || typeof event.detail.table !== "string" || typeof event.detail.column !== "string"
 
 
 
206
  || typeof event.ts !== "number" || !Number.isFinite(event.ts)
207
  || !Number.isFinite(new Date(event.ts * 1000).getTime())) throw new Error(EVENTS_UNAVAILABLE);
208
- reads.push({ user: event.principal, tenant: event.tenant, table: event.detail.table,
209
- column: event.detail.column, at: new Date(event.ts * 1000).toISOString() });
210
  }
211
- setEvents(reads.reverse());
212
  } catch {
213
  if (active) { setEvents([]); setProblem(EVENTS_UNAVAILABLE); }
214
  } finally {
@@ -226,31 +272,36 @@ function SecurityEvents() {
226
  };
227
  }, [revision]);
228
 
229
- if (!isAdmin(user)) return null;
230
- return <section className="security-compliance-group" aria-label="Personal data reads">
231
- <h4 className="set-h4">Personal data reads</h4>
232
- {loading ? <div className="set-skeleton" role="status" aria-label="Loading personal data reads" aria-busy="true">
233
- <span className="set-skeleton-line" aria-hidden="true" />
234
- <span className="set-skeleton-line" aria-hidden="true" />
235
- </div> : problem ? <div className="set-error" role="alert"><span>{problem}</span>
236
  <button type="button" className="set-secondary" onClick={() => setRevision((value) => value + 1)}>Retry</button>
237
- </div> : events.length ? <div style={{ overflowX: "auto" }}>
238
- <table className="set-table" aria-label="Personal data read events">
239
- <thead><tr><th>User</th><th>Tenant</th><th>Table</th><th>Column</th><th>Time (UTC)</th></tr></thead>
 
240
  <tbody>{events.map((event, index) => <tr key={`${event.at}:${index}`}>
241
- <td>{event.user}</td><td>{event.tenant}</td><td>{event.table}</td><td>{event.column}</td>
 
 
 
242
  <td><time dateTime={event.at}>{event.at.replace("T", " ").replace("Z", " UTC")}</time></td>
243
  </tr>)}</tbody>
244
  </table>
245
- </div> : <p className="set-help" role="status">No personal data reads</p>}
246
  </section>;
247
  }
248
 
249
  function BrowserSecurity() {
250
  const [user, setUser] = useState<SessionUser | null>(null);
251
  const [enabled, setEnabled] = useState<boolean | null>(null);
 
 
252
  const [busy, setBusy] = useState(false);
253
  const [problem, setProblem] = useState("");
 
 
 
254
  const [revision, setRevision] = useState(0);
255
  const scope = useRef<AbortController | null>(null);
256
  const flight = useRef(false);
@@ -260,30 +311,35 @@ function BrowserSecurity() {
260
  scope.current = controller;
261
  const clear = () => {
262
  controller.abort();
263
- setUser(null); setEnabled(null); setProblem(""); setBusy(false);
 
264
  flight.current = false;
265
  };
266
- setUser(null); setEnabled(null); setProblem("");
 
267
  async function load() {
268
  try {
269
  const identity = await me();
270
- if (controller.signal.aborted || !identity?.tenant
271
- || (currentTenant() !== null && currentTenant() !== identity.tenant)) return;
 
272
  setUser(identity);
273
- if (!isAdmin(identity)) return;
274
  const response = await fetch(`${API_V1}/security/trusted-browser`, {
275
  credentials: CREDENTIALS, cache: "no-store", signal: controller.signal,
276
  });
277
  if (controller.signal.aborted || checkTenant(response)) return;
278
  if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return; }
279
- if (response.status === 403) { setUser(null); return; }
280
  const body: unknown = await response.json().catch(() => null);
281
  if (controller.signal.aborted) return;
282
  if (!response.ok || !body || typeof (body as { enabled?: unknown }).enabled !== "boolean")
283
  throw new Error("policy");
284
  setEnabled((body as { enabled: boolean }).enabled);
285
  } catch {
286
- if (!controller.signal.aborted) setProblem("Browser sign in settings could not be loaded.");
 
 
287
  }
288
  }
289
  window.addEventListener(UNAUTHORIZED_EVENT, clear);
@@ -298,8 +354,8 @@ function BrowserSecurity() {
298
 
299
  async function save(next: boolean) {
300
  const request = scope.current?.signal;
301
- if (!request || request.aborted || flight.current || !isAdmin(user) || enabled === null) return;
302
- flight.current = true; setBusy(true); setProblem("");
303
  try {
304
  const response = await fetch(`${API_V1}/security/trusted-browser`, {
305
  method: "PUT", credentials: CREDENTIALS, cache: "no-store", signal: request,
@@ -307,14 +363,14 @@ function BrowserSecurity() {
307
  });
308
  if (request.aborted || checkTenant(response)) return;
309
  if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return; }
310
- if (response.status === 403) { setUser(null); return; }
311
  const body: unknown = await response.json().catch(() => null);
312
  if (request.aborted) return;
313
  if (!response.ok || !body || typeof (body as { enabled?: unknown }).enabled !== "boolean")
314
  throw new Error("policy");
315
  setEnabled((body as { enabled: boolean }).enabled);
316
  } catch {
317
- if (!request.aborted) setProblem("Browser sign in settings could not be saved.");
318
  } finally {
319
  if (!request.aborted) { flight.current = false; setBusy(false); }
320
  }
@@ -323,7 +379,7 @@ function BrowserSecurity() {
323
  async function signOutEverywhere() {
324
  const request = scope.current?.signal;
325
  if (!request || request.aborted || flight.current || !user) return;
326
- flight.current = true; setBusy(true); setProblem("");
327
  try {
328
  const response = await fetch(`${API_V1}/auth/logout-everywhere`, {
329
  method: "POST", credentials: CREDENTIALS, cache: "no-store", signal: request,
@@ -333,33 +389,52 @@ function BrowserSecurity() {
333
  if (!response.ok) throw new Error("sign out");
334
  signal(UNAUTHORIZED_EVENT);
335
  } catch {
336
- if (!request.aborted) setProblem("Sessions could not be signed out.");
337
  } finally {
338
  if (!request.aborted) { flight.current = false; setBusy(false); }
339
  }
340
  }
341
 
342
- if (!user) return null;
343
- return <section className="security-compliance-group set-member" aria-label="Browser sign in" data-testid="trusted-browser-settings">
344
- <h4 className="set-h4">Browser sign in</h4>
345
- {isAdmin(user) ? enabled === null && !problem ? (
346
- <div className="set-skeleton" role="status" aria-label="Loading browser sign in settings" aria-busy="true">
347
- <span className="set-skeleton-line" aria-hidden="true" />
 
 
 
 
 
 
348
  </div>
349
- ) : enabled !== null ? (
350
- <label className="set-member-master" htmlFor="security-trusted-browser">
351
- <span>Allow remembered browsers for 7 days</span>
352
- <input id="security-trusted-browser" type="checkbox" role="switch" checked={enabled}
353
- disabled={busy} onChange={(event) => void save(event.target.checked)} />
354
- </label>
355
- ) : null : null}
356
- <button type="button" className="set-secondary" disabled={busy} onClick={() => void signOutEverywhere()}>
357
- Sign out everywhere
358
- </button>
359
- {problem ? <div className="set-error" role="alert"><span>{problem}</span>
360
- <button type="button" className="set-secondary" disabled={busy} onClick={() => setRevision((value) => value + 1)}>Retry</button>
361
- </div> : null}
362
- </section>;
 
 
 
 
 
 
 
 
 
 
 
 
 
363
  }
364
 
365
  export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
@@ -371,6 +446,7 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
371
  const [codes, setCodes] = useState<string[] | null>(null);
372
  const [error, setError] = useState<string>("");
373
  const [device, setDevice] = useState<boolean>(false);
 
374
  const [setup, setSetup] = useState<MFASetup | null>(null);
375
  const [code, setCode] = useState<string>("");
376
  const [preparing, setPreparing] = useState<boolean>(false);
@@ -379,6 +455,7 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
379
  const flight = useRef<boolean>(false);
380
  const dialog = useRef<HTMLDialogElement>(null);
381
  const passwordField = useRef<HTMLInputElement>(null);
 
382
  const enrolForm = useRef<HTMLFormElement>(null);
383
  const focusCodeOnRetry = useRef<boolean>(false);
384
  const heading = useRef<HTMLHeadingElement>(null);
@@ -394,7 +471,7 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
394
  flight.current = false;
395
  focusCodeOnRetry.current = false;
396
  setPassword(""); setCode(""); setSetup(null); setCodes(null); setEnrolledUser(null);
397
- setError(""); setBusy(false); setPreparing(false); setDevice(false); setStatus(null);
398
  };
399
  clear();
400
  const invalidate = () => { scope.abort(); clear(); };
@@ -454,7 +531,7 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
454
  node.showModal();
455
  return () => {
456
  node.close();
457
- passwordField.current?.focus();
458
  };
459
  }, [locked]);
460
 
@@ -553,66 +630,13 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
553
  }
554
  }
555
 
556
- return (
557
- <div className="set-pane login-form login-mfa-security set-security">
558
- <h3 className="set-h" ref={heading} tabIndex={gate ? -1 : undefined}>Security</h3>
559
- {gate ? (
560
- <p className="login-mfa-help">Two step sign in is required before the rest of the app opens.</p>
561
- ) : loading ? <div className="set-skeleton" role="status" aria-label="Loading security status" aria-busy="true">
562
- <span className="set-skeleton-line" aria-hidden="true" />
563
- <span className="set-skeleton-line" aria-hidden="true" />
564
- <span className="set-skeleton-line" aria-hidden="true" />
565
- </div> : status ? (
566
- <>
567
- <dl className="login-mfa-status">
568
- <div><dt>Two step sign in</dt><dd>{status.enrolled ? "On" : "Off"}</dd></div>
569
- {status.enrolled ? (
570
- <div><dt>Turned on</dt><dd>{status.enrolledAt
571
- ? <time dateTime={status.enrolledAt}>{new Date(status.enrolledAt).toLocaleString()}</time>
572
- : "Date unavailable"}</dd></div>
573
- ) : null}
574
- <div><dt>Recovery codes left</dt><dd>{status.recoveryCodesRemaining}</dd></div>
575
- </dl>
576
- {status.enrolled ? (
577
- <>
578
- {!device && !locked ? (
579
- <button type="button" className="login-mfa-quiet set-secondary" onClick={() => {
580
- setDevice(true); setPassword(""); setCode(""); setSetup(null); setError("");
581
- }}>Enrol a new device</button>
582
- ) : null}
583
- {!device ? (
584
- <>
585
- <p className="login-mfa-help">Generating new recovery codes replaces the old codes and signs this account's other sessions out.</p>
586
- {!locked ? (
587
- <form className="login-form login-mfa-regenerate" onSubmit={generate}>
588
- <label className="login-mfa-label" htmlFor="security-password">Account password</label>
589
- <input id="security-password" className="login-input" type="password" autoComplete="current-password"
590
- ref={passwordField} value={password} disabled={busy} required
591
- aria-invalid={error ? true : undefined} aria-describedby="security-message"
592
- onChange={(event) => { setPassword(event.target.value); setError(""); }} />
593
- <button type="submit" className="login-submit set-primary" disabled={!password || busy}>
594
- Generate new recovery codes
595
- </button>
596
- </form>
597
- ) : null}
598
- </>
599
- ) : null}
600
- </>
601
- ) : <p className="login-mfa-help">Sign in again to set up your authenticator app.</p>}
602
- </>
603
- ) : (
604
- <div className="set-error" role="alert">
605
- <span>Security status could not be loaded.</span>
606
- <button type="button" className="set-secondary" onClick={() => setReload((value) => value + 1)}>Retry</button>
607
- </div>
608
- )}
609
- {(gate || device) && !locked ? (
610
- <form className="login-form" ref={enrolForm} onSubmit={enrol}>
611
  {!gate ? (
612
  <>
613
- <p className="login-mfa-help">Confirming a new device replaces your old recovery codes and signs this account's other sessions out.</p>
614
- <label className="login-mfa-label" htmlFor="security-device-password">Account password</label>
615
- <input id="security-device-password" className="login-input" type="password" autoComplete="current-password"
616
  ref={passwordField} value={password} disabled={preparing} required
617
  aria-invalid={error ? true : undefined} aria-describedby="security-message"
618
  onChange={(event) => { setPassword(event.target.value); setError(""); }} />
@@ -622,46 +646,100 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
622
  <>
623
  {/* The in-app encoder adds no dependency or external QR service.
624
  Keep manual setup available even when the QR cannot be drawn. */}
625
- <p className="login-mfa-help">Type this setup code into any authenticator app.</p>
626
- <span className="login-mfa-label">{setup.issuer} setup code</span>
627
  <code className="login-mfa-secret">{setup.secret.match(/.{1,4}/g)?.join(" ")}</code>
628
  <div className="security-qr-setup">
629
  <SetupQr uri={setup.otpauthUri} />
630
- <span className="login-mfa-label">Authenticator setup link</span>
631
  <code className="login-mfa-uri">{setup.otpauthUri}</code>
632
  </div>
633
- <label className="login-mfa-label" htmlFor="security-enrol-code">First authenticator code</label>
634
  <CodeInput id="security-enrol-code" value={code} disabled={busy || preparing}
635
  onChange={(value) => { setCode(value); setError(""); }}
636
  invalid={Boolean(error)} describedBy="security-message" />
637
  </>
638
- ) : preparing ? gate ? <p className="login-mfa-help" role="status">Preparing your setup code…</p> :
639
- <div className="set-skeleton" role="status" aria-label="Preparing your setup code" aria-busy="true">
640
- <span className="set-skeleton-line" aria-hidden="true" />
641
- <span className="set-skeleton-line" aria-hidden="true" />
642
- </div> : null}
643
- <button type="submit" className={gate ? "login-submit" : "login-submit set-primary"}
644
  disabled={preparing || (!gate && !password) || (setup !== null && !/^[0-9]{6}$/.test(code))}>
645
  {preparing ? "Preparing setup…" : setup ? "Confirm setup" : gate ? "Try setup again" : "Set up new device"}
646
  </button>
647
- <button type="button" className={gate ? "login-mfa-quiet" : "login-mfa-quiet set-secondary"} disabled={preparing} onClick={() => {
648
- if (gate) gate.onBack();
649
- else { setDevice(false); setPassword(""); setSetup(null); setCode(""); setError(""); }
650
- }}>{gate ? "Back to sign in" : "Cancel"}</button>
651
  </form>
652
- ) : null}
653
- <p className="login-msg" id="security-message" role="status" aria-live="polite">{locked || (!gate && !status) ? "" : error}</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
  {!gate ? <SecurityEvents /> : null}
655
- {!gate ? <BrowserSecurity /> : null}
656
 
657
  {/* R15/C12's explicit acknowledgement is the exception to ordinary
658
  dismissible dialogs. Native cancel is prevented, outside clicks have
659
  no close handler, and there is no close control or method=dialog form. */}
660
  {locked ? (
661
- <dialog ref={dialog} className="login-mfa-dialog" aria-labelledby="security-codes-heading"
662
  onCancel={(event) => event.preventDefault()}>
663
- <h4 id="security-codes-heading" className="login-mfa-heading" tabIndex={-1} autoFocus>Recovery codes</h4>
664
- <div className="login-form">
665
  {codes ? (
666
  <RecoveryCodes codes={codes} onError={setError} onSaved={() => {
667
  if (gate) {
@@ -673,17 +751,18 @@ export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
673
  }
674
  setCodes(null);
675
  setError("");
 
676
  // Count only, independently re-read after acknowledgement.
677
  setReload((value) => value + 1);
678
  }} />
679
  ) : (
680
  <>
681
- <p className="login-mfa-help" role="status">Generating your recovery codes…</p>
682
- <button type="button" className="login-submit" disabled>Generating codes…</button>
683
  </>
684
  )}
685
  </div>
686
- <p className="login-msg" role="status" aria-live="polite">{error}</p>
687
  </dialog>
688
  ) : null}
689
  </div>
 
15
  import { useEscapeClose } from "../ui/useEscapeClose";
16
  import { CodeInput } from "../ui/CodeInput";
17
  import { encodeQr } from "../shell/qrcode";
18
+ import "../styles/sdlc84/settings-security.css";
 
19
 
20
  function SetupQr({ uri }: { uri: string }) {
21
  const drawing = useMemo(() => {
 
83
 
84
  return (
85
  <>
86
+ <p className="security-help">These recovery codes are shown only once.</p>
87
+ <p className="security-help">Save them somewhere safe. Each code works once if you lose your authenticator.</p>
88
+ <ul className="security-codes" aria-label="Recovery codes">
89
  {codes.map((code) => <li key={code}><code>{code}</code></li>)}
90
  </ul>
91
+ <div className="security-copy-row">
92
+ <button type="button" className="set-secondary" onClick={() => void copy()} disabled={copying}>
93
  {copying ? "Copying…" : "Copy codes"}
94
  </button>
95
+ <span className="security-copy-status" role="status">{copied ? "Copied" : ""}</span>
96
  </div>
97
  {/* A type=button acknowledgement cannot be submitted by Enter left over
98
  from the previous code field. There is no Back or secondary exit. */}
99
+ <button type="button" className="set-primary" onClick={onSaved} disabled={copying}>
100
  I have saved these codes
101
  </button>
102
  </>
 
162
  onBack: () => void;
163
  };
164
 
165
+ type SecurityEvent = {
166
+ kind: string;
167
+ user: string;
168
+ tenant: string;
169
+ detail: Record<string, string | number | boolean>;
170
+ at: string;
171
+ };
172
+ const EVENTS_UNAVAILABLE = "Security events could not be loaded.";
173
+ const EVENT_LABELS: Record<string, string> = {
174
+ "auth.login": "Signed in",
175
+ "auth.login_failed": "Sign-in failed",
176
+ "auth.lockout": "Account locked",
177
+ "auth.mfa_enrolled": "Two-step sign-in set up",
178
+ "auth.mfa_failed": "Two-step sign-in failed",
179
+ "auth.logout": "Signed out",
180
+ "admin.grant": "Access changed",
181
+ "credential.added": "Credential added",
182
+ "credential.rotated": "Credential replaced",
183
+ "credential.revoked": "Credential revoked",
184
+ "amazon.sync": "Amazon sync",
185
+ "amazon.throttled": "Amazon request limited",
186
+ "amazon.authorization_lost": "Amazon authorization lost",
187
+ "amazon.deletion_scheduled": "Amazon deletion scheduled",
188
+ "amazon.personal_read": "Amazon personal data read",
189
+ "customer.personal_read": "Customer personal data read",
190
+ "data.purge": "Data deleted",
191
+ "data.restore": "Data restored",
192
+ "config.changed": "Setting changed",
193
+ "external.query": "External query",
194
+ "external.write": "External change",
195
+ };
196
+
197
+ function readableLabel(value: string) {
198
+ const words = value.replace(/[._-]+/g, " ").trim().toLowerCase();
199
+ return words ? words[0].toUpperCase() + words.slice(1) : "Security event";
200
+ }
201
+
202
+ function SecuritySkeleton({ label }: { label: string }) {
203
+ return <div className="set-skeleton security-skeleton" role="status" aria-label={label} aria-busy="true">
204
+ <span className="set-skeleton-line" aria-hidden="true" />
205
+ <span className="set-skeleton-line" aria-hidden="true" />
206
+ </div>;
207
+ }
208
 
209
  function SecurityEvents() {
210
+ const [denied, setDenied] = useState(false);
211
+ const [events, setEvents] = useState<SecurityEvent[]>([]);
212
  const [loading, setLoading] = useState(true);
213
  const [problem, setProblem] = useState("");
214
  const [revision, setRevision] = useState(0);
 
219
  const clear = () => {
220
  active = false;
221
  controller.abort();
222
+ setDenied(true); setEvents([]); setProblem(""); setLoading(false);
223
  };
224
+ setDenied(false); setEvents([]); setLoading(true); setProblem("");
225
  async function load() {
226
  try {
227
  const identity = await me();
228
+ if (!active) return;
229
+ if (!identity?.tenant) throw new Error(EVENTS_UNAVAILABLE);
230
+ if (currentTenant() !== null && currentTenant() !== identity.tenant) {
231
+ setDenied(true); return;
232
+ }
233
+ if (!isAdmin(identity)) { setDenied(true); return; }
234
  const response = await fetch(`${API_V1}/security/events`, {
235
  credentials: CREDENTIALS, cache: "no-store", signal: controller.signal,
236
  });
237
  if (!active || checkTenant(response)) return;
238
  if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return; }
239
+ if (response.status === 403) { setDenied(true); return; }
240
  if (!response.ok) throw new Error(EVENTS_UNAVAILABLE);
241
  const body = await response.json();
242
  if (!active) return;
243
  if (!body || !Array.isArray(body.events)) throw new Error(EVENTS_UNAVAILABLE);
244
+ const entries: SecurityEvent[] = [];
245
  for (const event of body.events) {
246
+ if (!event || event.tenant !== identity.tenant) continue;
247
+ if (typeof event.kind !== "string" || !event.kind.trim()
248
+ || typeof event.principal !== "string" || !event.principal.trim()
249
+ || !event.detail || typeof event.detail !== "object" || Array.isArray(event.detail)
250
+ || !Object.values(event.detail).every((value) => typeof value === "string"
251
+ || typeof value === "boolean" || (typeof value === "number" && Number.isFinite(value)))
252
  || typeof event.ts !== "number" || !Number.isFinite(event.ts)
253
  || !Number.isFinite(new Date(event.ts * 1000).getTime())) throw new Error(EVENTS_UNAVAILABLE);
254
+ entries.push({ kind: event.kind, user: event.principal, tenant: event.tenant,
255
+ detail: event.detail, at: new Date(event.ts * 1000).toISOString() });
256
  }
257
+ setEvents(entries.reverse());
258
  } catch {
259
  if (active) { setEvents([]); setProblem(EVENTS_UNAVAILABLE); }
260
  } finally {
 
272
  };
273
  }, [revision]);
274
 
275
+ return <section className="security-events" aria-labelledby="security-events-heading">
276
+ <h4 className="set-h4" id="security-events-heading">Security events</h4>
277
+ {loading ? <SecuritySkeleton label="Loading security events" /> : problem ? <div className="set-error" role="alert"><span>{problem}</span>
 
 
 
 
278
  <button type="button" className="set-secondary" onClick={() => setRevision((value) => value + 1)}>Retry</button>
279
+ </div> : denied ? <p className="security-help" role="status">Only administrators can view security events</p>
280
+ : events.length ? <div className="security-event-scroll" role="region" aria-label="Security event log" tabIndex={0}>
281
+ <table className="set-table" aria-label="Security events">
282
+ <thead><tr><th scope="col">Event</th><th scope="col">User</th><th scope="col">Details</th><th scope="col">Time (UTC)</th></tr></thead>
283
  <tbody>{events.map((event, index) => <tr key={`${event.at}:${index}`}>
284
+ <td><span>{Object.hasOwn(EVENT_LABELS, event.kind) ? EVENT_LABELS[event.kind] : readableLabel(event.kind)}</span>
285
+ <span className="security-event-kind">{event.kind}</span></td>
286
+ <td>{event.user}</td>
287
+ <td>{Object.entries(event.detail).map(([key, value]) => `${readableLabel(key)}: ${String(value)}`).join(" · ") || "No details"}</td>
288
  <td><time dateTime={event.at}>{event.at.replace("T", " ").replace("Z", " UTC")}</time></td>
289
  </tr>)}</tbody>
290
  </table>
291
+ </div> : <p className="security-help" role="status">No security events yet</p>}
292
  </section>;
293
  }
294
 
295
  function BrowserSecurity() {
296
  const [user, setUser] = useState<SessionUser | null>(null);
297
  const [enabled, setEnabled] = useState<boolean | null>(null);
298
+ const [loading, setLoading] = useState(true);
299
+ const [denied, setDenied] = useState(false);
300
  const [busy, setBusy] = useState(false);
301
  const [problem, setProblem] = useState("");
302
+ const [signOutProblem, setSignOutProblem] = useState("");
303
+ const [operation, setOperation] = useState<"load" | "save" | "logout">("load");
304
+ const retryValue = useRef<boolean | null>(null);
305
  const [revision, setRevision] = useState(0);
306
  const scope = useRef<AbortController | null>(null);
307
  const flight = useRef(false);
 
311
  scope.current = controller;
312
  const clear = () => {
313
  controller.abort();
314
+ setUser(null); setEnabled(null); setProblem(""); setSignOutProblem("");
315
+ setBusy(false); setLoading(false); setDenied(true);
316
  flight.current = false;
317
  };
318
+ setUser(null); setEnabled(null); setProblem(""); setSignOutProblem("");
319
+ setLoading(true); setDenied(false); setOperation("load"); retryValue.current = null;
320
  async function load() {
321
  try {
322
  const identity = await me();
323
+ if (controller.signal.aborted) return;
324
+ if (!identity?.tenant) throw new Error("identity");
325
+ if (currentTenant() !== null && currentTenant() !== identity.tenant) { setDenied(true); return; }
326
  setUser(identity);
327
+ if (!isAdmin(identity)) { setDenied(true); return; }
328
  const response = await fetch(`${API_V1}/security/trusted-browser`, {
329
  credentials: CREDENTIALS, cache: "no-store", signal: controller.signal,
330
  });
331
  if (controller.signal.aborted || checkTenant(response)) return;
332
  if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return; }
333
+ if (response.status === 403) { setDenied(true); return; }
334
  const body: unknown = await response.json().catch(() => null);
335
  if (controller.signal.aborted) return;
336
  if (!response.ok || !body || typeof (body as { enabled?: unknown }).enabled !== "boolean")
337
  throw new Error("policy");
338
  setEnabled((body as { enabled: boolean }).enabled);
339
  } catch {
340
+ if (!controller.signal.aborted) setProblem("Remembered browser settings could not be loaded.");
341
+ } finally {
342
+ if (!controller.signal.aborted) setLoading(false);
343
  }
344
  }
345
  window.addEventListener(UNAUTHORIZED_EVENT, clear);
 
354
 
355
  async function save(next: boolean) {
356
  const request = scope.current?.signal;
357
+ if (!request || request.aborted || flight.current || !isAdmin(user) || denied || enabled === null) return;
358
+ flight.current = true; setBusy(true); setProblem(""); setOperation("save"); retryValue.current = next;
359
  try {
360
  const response = await fetch(`${API_V1}/security/trusted-browser`, {
361
  method: "PUT", credentials: CREDENTIALS, cache: "no-store", signal: request,
 
363
  });
364
  if (request.aborted || checkTenant(response)) return;
365
  if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return; }
366
+ if (response.status === 403) { setDenied(true); return; }
367
  const body: unknown = await response.json().catch(() => null);
368
  if (request.aborted) return;
369
  if (!response.ok || !body || typeof (body as { enabled?: unknown }).enabled !== "boolean")
370
  throw new Error("policy");
371
  setEnabled((body as { enabled: boolean }).enabled);
372
  } catch {
373
+ if (!request.aborted) setProblem("Remembered browser settings could not be saved.");
374
  } finally {
375
  if (!request.aborted) { flight.current = false; setBusy(false); }
376
  }
 
379
  async function signOutEverywhere() {
380
  const request = scope.current?.signal;
381
  if (!request || request.aborted || flight.current || !user) return;
382
+ flight.current = true; setBusy(true); setSignOutProblem(""); setOperation("logout");
383
  try {
384
  const response = await fetch(`${API_V1}/auth/logout-everywhere`, {
385
  method: "POST", credentials: CREDENTIALS, cache: "no-store", signal: request,
 
389
  if (!response.ok) throw new Error("sign out");
390
  signal(UNAUTHORIZED_EVENT);
391
  } catch {
392
+ if (!request.aborted) setSignOutProblem("Your account could not be signed out everywhere.");
393
  } finally {
394
  if (!request.aborted) { flight.current = false; setBusy(false); }
395
  }
396
  }
397
 
398
+ return <>
399
+ <section className="security-control" aria-labelledby="security-browsers-heading" data-testid="trusted-browser-settings">
400
+ <div className="security-control-head">
401
+ <div className="security-control-copy">
402
+ <h4 className="set-h4" id="security-browsers-heading">Remembered browsers</h4>
403
+ <p className="security-help">Allow browsers to skip two-step sign-in for 7 days</p>
404
+ </div>
405
+ <button id="security-trusted-browser" type="button" role="switch" aria-checked={enabled === true}
406
+ aria-label="Allow remembered browsers for 7 days" className="set-secondary security-control-action"
407
+ disabled={busy || loading || denied || enabled === null} onClick={() => void save(!enabled)}>
408
+ {loading ? "Loading…" : busy && operation === "save" ? "Saving…" : enabled === null ? "Unavailable" : enabled ? "On" : "Off"}
409
+ </button>
410
  </div>
411
+ {loading ? <SecuritySkeleton label="Loading remembered browser settings" />
412
+ : denied ? <p className="security-help" role="status">Only administrators can change this setting</p> : null}
413
+ {problem ? <div className="set-error" role="alert"><span>{problem}</span>
414
+ <button type="button" className="set-secondary" disabled={busy} onClick={() => {
415
+ if (retryValue.current !== null) void save(retryValue.current);
416
+ else setRevision((value) => value + 1);
417
+ }}>Retry</button>
418
+ </div> : null}
419
+ </section>
420
+ <section className="security-control" aria-labelledby="security-signout-heading">
421
+ <div className="security-control-head">
422
+ <div className="security-control-copy">
423
+ <h4 className="set-h4" id="security-signout-heading">Sign out everywhere</h4>
424
+ <p className="security-help">End all sessions for your account, including this one</p>
425
+ </div>
426
+ <button type="button" className="set-secondary security-control-action security-destructive"
427
+ disabled={busy || loading || !user} onClick={() => void signOutEverywhere()}>
428
+ {busy && operation === "logout" ? "Signing out…" : "Sign out everywhere"}
429
+ </button>
430
+ </div>
431
+ {loading ? <SecuritySkeleton label="Loading sign-out settings" />
432
+ : !user ? <p className="security-help" role="status">Sign-out controls are unavailable until your account loads</p> : null}
433
+ {signOutProblem ? <div className="set-error" role="alert"><span>{signOutProblem}</span>
434
+ <button type="button" className="set-secondary" disabled={busy} onClick={() => void signOutEverywhere()}>Retry</button>
435
+ </div> : null}
436
+ </section>
437
+ </>;
438
  }
439
 
440
  export default function SecurityPane({ gate }: { gate?: EnrolmentGate } = {}) {
 
446
  const [codes, setCodes] = useState<string[] | null>(null);
447
  const [error, setError] = useState<string>("");
448
  const [device, setDevice] = useState<boolean>(false);
449
+ const [recovering, setRecovering] = useState(false);
450
  const [setup, setSetup] = useState<MFASetup | null>(null);
451
  const [code, setCode] = useState<string>("");
452
  const [preparing, setPreparing] = useState<boolean>(false);
 
455
  const flight = useRef<boolean>(false);
456
  const dialog = useRef<HTMLDialogElement>(null);
457
  const passwordField = useRef<HTMLInputElement>(null);
458
+ const recoveryAction = useRef<HTMLButtonElement>(null);
459
  const enrolForm = useRef<HTMLFormElement>(null);
460
  const focusCodeOnRetry = useRef<boolean>(false);
461
  const heading = useRef<HTMLHeadingElement>(null);
 
471
  flight.current = false;
472
  focusCodeOnRetry.current = false;
473
  setPassword(""); setCode(""); setSetup(null); setCodes(null); setEnrolledUser(null);
474
+ setError(""); setBusy(false); setPreparing(false); setDevice(false); setRecovering(false); setStatus(null);
475
  };
476
  clear();
477
  const invalidate = () => { scope.abort(); clear(); };
 
531
  node.showModal();
532
  return () => {
533
  node.close();
534
+ (passwordField.current ?? recoveryAction.current)?.focus();
535
  };
536
  }, [locked]);
537
 
 
630
  }
631
  }
632
 
633
+ const enrollmentForm = (gate || device) && !locked ? (
634
+ <form className="security-form" ref={enrolForm} onSubmit={enrol}>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
  {!gate ? (
636
  <>
637
+ <p className="security-help">A new device replaces your recovery codes and signs out your other sessions.</p>
638
+ <label className="security-label" htmlFor="security-device-password">Account password</label>
639
+ <input id="security-device-password" className="security-input" type="password" autoComplete="current-password"
640
  ref={passwordField} value={password} disabled={preparing} required
641
  aria-invalid={error ? true : undefined} aria-describedby="security-message"
642
  onChange={(event) => { setPassword(event.target.value); setError(""); }} />
 
646
  <>
647
  {/* The in-app encoder adds no dependency or external QR service.
648
  Keep manual setup available even when the QR cannot be drawn. */}
649
+ <p className="security-help">Type this setup code into any authenticator app.</p>
650
+ <span className="security-label">{setup.issuer} setup code</span>
651
  <code className="login-mfa-secret">{setup.secret.match(/.{1,4}/g)?.join(" ")}</code>
652
  <div className="security-qr-setup">
653
  <SetupQr uri={setup.otpauthUri} />
654
+ <span className="security-label">Authenticator setup link</span>
655
  <code className="login-mfa-uri">{setup.otpauthUri}</code>
656
  </div>
657
+ <label className="security-label" htmlFor="security-enrol-code">First authenticator code</label>
658
  <CodeInput id="security-enrol-code" value={code} disabled={busy || preparing}
659
  onChange={(value) => { setCode(value); setError(""); }}
660
  invalid={Boolean(error)} describedBy="security-message" />
661
  </>
662
+ ) : preparing ? <SecuritySkeleton label="Preparing your setup code" /> : null}
663
+ <button type="submit" className="set-primary"
 
 
 
 
664
  disabled={preparing || (!gate && !password) || (setup !== null && !/^[0-9]{6}$/.test(code))}>
665
  {preparing ? "Preparing setup…" : setup ? "Confirm setup" : gate ? "Try setup again" : "Set up new device"}
666
  </button>
667
+ {gate ? <button type="button" className="set-secondary" disabled={preparing} onClick={gate.onBack}>
668
+ Back to sign in
669
+ </button> : null}
 
670
  </form>
671
+ ) : null;
672
+
673
+ return (
674
+ <div className="set-pane set-security">
675
+ <h3 className="set-h" ref={heading} tabIndex={gate ? -1 : undefined}>Security</h3>
676
+ {gate ? <>
677
+ <p className="security-help">Two-step sign-in is required before the rest of the app opens.</p>
678
+ {enrollmentForm}
679
+ </> : <div className="security-controls">
680
+ <section className="security-control" aria-labelledby="security-factor-heading">
681
+ <div className="security-control-head">
682
+ <div className="security-control-copy">
683
+ <h4 className="set-h4" id="security-factor-heading">Two-step sign-in</h4>
684
+ <p className="security-help">{status && !status.enrolled
685
+ ? "Sign in again to set up your authenticator app" : "Use an authenticator app to protect your account"}</p>
686
+ </div>
687
+ <button type="button" className="set-secondary security-control-action"
688
+ disabled={loading || locked || preparing || !status?.enrolled} aria-expanded={device}
689
+ aria-controls={device ? "security-device-setup" : undefined} onClick={() => {
690
+ setDevice(!device); setRecovering(false); setPassword(""); setCode(""); setSetup(null); setError("");
691
+ }}>{device ? "Cancel" : "Enrol a new device"}</button>
692
+ </div>
693
+ {loading ? <SecuritySkeleton label="Loading two-step sign-in status" />
694
+ : status ? <p className={`security-control-status ${status.enrolled ? "is-positive" : "is-attention"}`}>
695
+ {status.enrolled ? "On" : "Not set up"}
696
+ {status.enrolled && status.enrolledAt ? <> · Since <time dateTime={status.enrolledAt}>
697
+ {new Date(status.enrolledAt).toLocaleDateString()}</time></> : null}
698
+ </p> : <div className="set-error" role="alert">
699
+ <span>Security status could not be loaded.</span>
700
+ <button type="button" className="set-secondary" onClick={() => setReload((value) => value + 1)}>Retry</button>
701
+ </div>}
702
+ {device ? <div id="security-device-setup">{enrollmentForm}</div> : null}
703
+ </section>
704
+ <section className="security-control" aria-labelledby="security-recovery-heading">
705
+ <div className="security-control-head">
706
+ <div className="security-control-copy">
707
+ <h4 className="set-h4" id="security-recovery-heading">Recovery codes</h4>
708
+ <p className="security-help">Use a saved code if you lose your authenticator</p>
709
+ </div>
710
+ <button type="button" ref={recoveryAction} className="set-secondary security-control-action"
711
+ disabled={loading || locked || preparing || !status?.enrolled} aria-expanded={recovering}
712
+ aria-controls={recovering && !locked ? "security-recovery-setup" : undefined} onClick={() => {
713
+ setRecovering(!recovering); setDevice(false); setPassword(""); setCode(""); setSetup(null); setError("");
714
+ }}>{recovering ? "Cancel" : "Generate codes"}</button>
715
+ </div>
716
+ {loading ? <SecuritySkeleton label="Loading recovery code status" />
717
+ : status?.enrolled ? <p className={`security-control-status ${status.recoveryCodesRemaining === 0 ? "is-attention" : ""}`}>
718
+ {status.recoveryCodesRemaining === 0 ? "No recovery codes left" : `${status.recoveryCodesRemaining} codes left`}
719
+ </p> : <p className="security-help">{status ? "Set up two-step sign-in to use recovery codes" : "Recovery code status is unavailable"}</p>}
720
+ {recovering && !locked ? <form id="security-recovery-setup" className="security-form" onSubmit={generate}>
721
+ <p className="security-help">New codes replace your old codes and sign out your other sessions.</p>
722
+ <label className="security-label" htmlFor="security-password">Account password</label>
723
+ <input id="security-password" className="security-input" type="password" autoComplete="current-password"
724
+ ref={passwordField} value={password} disabled={busy} required autoFocus
725
+ aria-invalid={error ? true : undefined} aria-describedby="security-message"
726
+ onChange={(event) => { setPassword(event.target.value); setError(""); }} />
727
+ <button type="submit" className="set-primary" disabled={!password || busy}>Generate new recovery codes</button>
728
+ </form> : null}
729
+ </section>
730
+ <BrowserSecurity />
731
+ </div>}
732
+ <p className="security-message" id="security-message" role="status" aria-live="polite">{locked || (!gate && !status) ? "" : error}</p>
733
  {!gate ? <SecurityEvents /> : null}
 
734
 
735
  {/* R15/C12's explicit acknowledgement is the exception to ordinary
736
  dismissible dialogs. Native cancel is prevented, outside clicks have
737
  no close handler, and there is no close control or method=dialog form. */}
738
  {locked ? (
739
+ <dialog ref={dialog} className="security-codes-dialog" aria-labelledby="security-codes-heading"
740
  onCancel={(event) => event.preventDefault()}>
741
+ <h4 id="security-codes-heading" className="set-h4" tabIndex={-1} autoFocus>Recovery codes</h4>
742
+ <div className="security-form">
743
  {codes ? (
744
  <RecoveryCodes codes={codes} onError={setError} onSaved={() => {
745
  if (gate) {
 
751
  }
752
  setCodes(null);
753
  setError("");
754
+ setRecovering(false);
755
  // Count only, independently re-read after acknowledgement.
756
  setReload((value) => value + 1);
757
  }} />
758
  ) : (
759
  <>
760
+ <p className="security-help" role="status">Generating your recovery codes…</p>
761
+ <button type="button" className="set-primary" disabled>Generating codes…</button>
762
  </>
763
  )}
764
  </div>
765
+ <p className="security-message" role="status" aria-live="polite">{error}</p>
766
  </dialog>
767
  ) : null}
768
  </div>
web/src/settings/SettingsModal.tsx CHANGED
@@ -33,23 +33,20 @@
33
  // would not open.
34
  // ---------------------------------------------------------------------------
35
 
36
- import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
37
  // ⭐ W37-T05 — the shared in-flight label; see `ui/Busy.tsx` for why it is a component.
38
  import { Busy } from "../ui/Busy";
39
- import { AnchoredOverlay, BodyPortal, useOverlayLayer } from "../customer-grid/OverlaySurface";
40
- import { MenuLabel } from "../customer-grid/icons";
41
- import { ConfirmDialog } from "../ui/ConfirmDialog";
42
  import type { ReactNode } from "react";
43
  import type { SessionUser } from "../shell/session";
44
  import { isAdmin, parseUserEnvelope } from "../shell/session";
45
- import type { AdminUser, SettingsPayload, TestAccountPreview } from "./settingsApi";
46
  // ⚠ `patchUser` and `setPassword` left this file with `EditUser` (R7) — they are
47
  // imported by `PermsEditor` now, where the form that writes them lives, and it
48
  // spells the patch shape `Parameters<typeof patchUser>[1]` rather than importing
49
  // `UserPatch` separately. `noUnusedLocals` is what walks a move like this down to
50
  // the last orphaned import, which is the tsconfig earning its keep again.
51
  import { clearAvatar, createUser, getSettings, listUsers, setAvatar } from "./settingsApi";
52
- import { createTestAccount, previewTestAccount, removeTestAccount } from "./settingsApi";
53
  import { TOAST_EVENT, UNAUTHORIZED_EVENT } from "../apiContract";
54
  import "../styles/sdlc80/connector-setup.css";
55
  import { PermsEditor } from "./PermsEditor";
@@ -194,6 +191,7 @@ const IconTenants = (
194
  function UsersPane() {
195
  const [users, setUsers] = useState<AdminUser[] | null>(null);
196
  const [error, setError] = useState("");
 
197
  const [notice, setNotice] = useState("");
198
  /**
199
  * ⚠ WAVE 17 R7 — ONE STATE, WHERE THERE WERE TWO. This pane held `editing`
@@ -212,32 +210,21 @@ function UsersPane() {
212
  const reload = useCallback(() => {
213
  setUsers(null);
214
  setError("");
 
215
  void listUsers().then((r) => {
216
  if (r.ok) {
217
  setUsers(r.data.users ?? []);
218
  setError("");
219
  } else {
220
  setUsers([]);
221
- setError(r.message);
 
222
  }
223
  });
224
  }, []);
225
 
226
  useEffect(reload, [reload]);
227
 
228
- // S79-T64: the test account flow open over the list, if any.
229
- const [flow, setFlow] = useState<TestFlow | null>(null);
230
- // Keeps the list on screen while it refetches, so the row opener survives for focus return.
231
- const refresh = useCallback(() => {
232
- void listUsers().then((r) => {
233
- if (r.ok) {
234
- setUsers(r.data.users ?? []);
235
- setError("");
236
- } else setError(r.message);
237
- });
238
- }, []);
239
- const closeFlow = useCallback(() => setFlow(null), []);
240
-
241
  // wave17 item 3 (R6). ⚠ `.pg-empty` was the WRONG BOX as well as the wrong
242
  // words: it is the dashed empty-state frame, so a list that was merely still
243
  // arriving was drawn in the chrome that means "there is nothing here".
@@ -253,6 +240,14 @@ function UsersPane() {
253
  </div>
254
  );
255
 
 
 
 
 
 
 
 
 
256
  // The FULL-PANE TAKEOVER: the users pane IS the modal body, so returning the
257
  // page here replaces the whole body and nothing else has to know. The rail
258
  // stays — it is chrome, not body, and an admin who wants out of this page
@@ -357,7 +352,7 @@ function UsersPane() {
357
  // R252 supersedes the card layout. Each row keeps one keyboard destination and a full-row pointer target.
358
  <div className="set-users-table-wrap">
359
  <table className="set-table set-users-table" role="table" aria-label="Manage users">
360
- <thead role="rowgroup"><tr role="row"><th scope="col">Name</th><th scope="col">Role</th><th scope="col">Access summary</th><th scope="col">Last active</th><th scope="col"><span className="lp-sr-only">Actions</span></th></tr></thead>
361
  <tbody role="rowgroup">
362
  {users.map((u) => (
363
  <tr key={u.username} role="row" className={u.active ? "" : "is-inactive"} onClick={() => setOpen(u.username)}>
@@ -373,8 +368,6 @@ function UsersPane() {
373
  <span className="set-acct-top">
374
  <span className="set-acct-name">{u.name || u.username}</span>
375
  {u.active ? null : <span className="set-chip set-chip--off">Deactivated</span>}
376
- {u.test ? <span className="set-chip" data-test-account=""
377
- title={u.test.copiedFrom ? `Copied from ${u.test.copiedFrom}` : undefined}>Test</span> : null}
378
  </span>
379
  <span className="set-acct-sub">{u.username}</span>
380
  {/* ⚠ An admin BYPASSES perms entirely (C-PERM amendment 4) —
@@ -401,442 +394,16 @@ function UsersPane() {
401
  put two tab stops on one destination: the confusion the
402
  ruling names, rebuilt in the accessibility tree. */}
403
  <td role="cell" data-label="Last active">{memberActivity(u)}</td>
404
- {/* The menu portals to <body>, and React still bubbles its clicks here: stop them
405
- before the row opens the member page. */}
406
- <td role="cell" className="is-numeric" onClick={(event) => event.stopPropagation()}>
407
- {testAction(u) ? <MemberActions user={u} onPick={setFlow} /> : null}
408
- </td>
409
  </tr>
410
  ))}
411
  </tbody>
412
  </table>
413
  </div>
414
  )}
415
- {flow?.kind === "create" ? (
416
- <TestAccountDialog
417
- user={flow.user}
418
- onClose={closeFlow}
419
- onCreated={() => {
420
- setNotice("");
421
- refresh();
422
- }}
423
- />
424
- ) : null}
425
- {flow?.kind === "remove" ? (
426
- <RemoveTestAccount
427
- user={flow.user}
428
- onClose={closeFlow}
429
- onRemoved={() => {
430
- setFlow(null);
431
- setNotice("Test account removed.");
432
- refresh();
433
- }}
434
- />
435
- ) : null}
436
  </div>
437
  );
438
  }
439
 
440
- // ── S79-T64 (R42): a test account that copies one member's access ─────────────────────────
441
- // Admin only: this pane is reached by an Administrator alone, and every route 403s anyone else.
442
-
443
- type TestFlow = { kind: "create" | "remove"; user: AdminUser };
444
-
445
- /** The one action a row offers: remove a test account, or copy an active member who is not an admin. */
446
- function testAction(user: AdminUser): TestFlow["kind"] | null {
447
- if (user.test) return "remove";
448
- return user.active && user.role !== "admin" ? "create" : null;
449
- }
450
-
451
- /** Fades and lifts an overlay in with the feedback tokens. Reduced motion skips it. */
452
- function revealMotion(element: Element | null | undefined): Animation | undefined {
453
- if (!(element instanceof HTMLElement) || typeof element.animate !== "function") return undefined;
454
- if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return undefined;
455
- const tokens = getComputedStyle(document.documentElement);
456
- const raw = tokens.getPropertyValue("--lp-motion-feedback-duration").trim();
457
- const ms = raw.endsWith("ms") ? parseFloat(raw) : raw.endsWith("s") ? parseFloat(raw) * 1000 : NaN;
458
- const easing = tokens.getPropertyValue("--lp-motion-feedback-easing").trim() || "ease-out";
459
- try {
460
- return element.animate(
461
- [{ opacity: 0, transform: "translateY(-4px) scale(0.98)" }, { opacity: 1, transform: "none" }],
462
- { duration: Number.isFinite(ms) ? ms : 160, easing }
463
- );
464
- } catch {
465
- return undefined;
466
- }
467
- }
468
-
469
- function MemberActions({ user, onPick }: { user: AdminUser; onPick(flow: TestFlow): void }) {
470
- const trigger = useRef<HTMLButtonElement>(null);
471
- const [open, setOpen] = useState(false);
472
- const kind = testAction(user);
473
- const name = user.name || user.username;
474
- const close = () => {
475
- setOpen(false);
476
- trigger.current?.focus({ preventScroll: true });
477
- };
478
- if (!kind) return null;
479
- return (
480
- <>
481
- <button
482
- ref={trigger}
483
- type="button"
484
- className="set-secondary"
485
- data-member-actions={user.username}
486
- aria-label={`Actions for ${name}`}
487
- aria-haspopup="menu"
488
- aria-expanded={open}
489
- onClick={() => setOpen((was) => !was)}
490
- >
491
- <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false">
492
- <circle cx="3" cy="8" r="1.2" fill="currentColor" />
493
- <circle cx="8" cy="8" r="1.2" fill="currentColor" />
494
- <circle cx="13" cy="8" r="1.2" fill="currentColor" />
495
- </svg>
496
- </button>
497
- {open ? (
498
- <AnchoredOverlay
499
- anchor={trigger.current}
500
- className="cg-view-menu"
501
- placement="bottom-end"
502
- role="menu"
503
- ariaLabel={`Actions for ${name}`}
504
- dataKind="member-actions-menu"
505
- onDismiss={close}
506
- >
507
- <MemberActionItem
508
- kind={kind}
509
- onPick={() => {
510
- // Focus goes to the opener first, so the dialog returns focus there on close.
511
- close();
512
- onPick({ kind, user });
513
- }}
514
- />
515
- </AnchoredOverlay>
516
- ) : null}
517
- </>
518
- );
519
- }
520
-
521
- function MemberActionItem({ kind, onPick }: { kind: TestFlow["kind"]; onPick(): void }) {
522
- const item = useRef<HTMLButtonElement>(null);
523
- useLayoutEffect(() => {
524
- const motion = revealMotion(item.current?.closest(".cg-overlay"));
525
- return () => motion?.cancel();
526
- }, []);
527
- return kind === "remove" ? (
528
- <button ref={item} type="button" role="menuitem" className="is-danger" onClick={onPick}>
529
- <MenuLabel icon="trash" text="Remove test account" />
530
- </button>
531
- ) : (
532
- <button ref={item} type="button" role="menuitem" onClick={onPick}>
533
- <MenuLabel icon="duplicate" text="Create test account" />
534
- </button>
535
- );
536
- }
537
-
538
- // Unit scope reaches this preview as a row lock from the server (R1: no unit picker or `bus` here).
539
- const GRANT_KINDS: Record<string, string> = {
540
- view: "View",
541
- folder: "Folder",
542
- database: "Database",
543
- field: "Field",
544
- cohort: "Locked view",
545
- workbook: "Sheet",
546
- };
547
-
548
- /** A list that scrolls on its own, so 200 rows keep the dialog's actions in view. */
549
- const LIST_BOX = { maxHeight: "min(30vh, 240px)", overflowY: "auto" } as const;
550
- const NOWRAP = { whiteSpace: "nowrap" } as const;
551
- const BREAK = { overflowWrap: "anywhere" } as const;
552
- /** On a short screen the preview body scrolls and the action row stays in view. */
553
- const SCROLL_BODY = { flex: "1 1 auto", minHeight: 0, overflowY: "auto" } as const;
554
- const PINNED = { flexShrink: 0 } as const;
555
-
556
- function TestAccountDialog({
557
- user,
558
- onClose,
559
- onCreated,
560
- }: {
561
- user: AdminUser;
562
- onClose(): void;
563
- onCreated(): void;
564
- }) {
565
- const panelRef = useRef<HTMLDivElement>(null);
566
- const doneRef = useRef<HTMLButtonElement>(null);
567
- const alive = useRef(true);
568
- const titleId = useId();
569
- const [phase, setPhase] = useState<"loading" | "error" | "ready" | "creating" | "created">("loading");
570
- const [preview, setPreview] = useState<TestAccountPreview | null>(null);
571
- const [error, setError] = useState("");
572
- const [attempt, setAttempt] = useState(0);
573
- const [made, setMade] = useState<{ username: string; password: string } | null>(null);
574
- const [copied, setCopied] = useState(false);
575
- const busy = phase === "creating";
576
- const name = user.name || user.username;
577
- const dismiss = useCallback(() => {
578
- if (!busy) onClose();
579
- }, [busy, onClose]);
580
-
581
- useOverlayLayer({
582
- panelRef,
583
- onDismiss: dismiss,
584
- dismissOnOutside: false,
585
- initialFocus: "[data-overlay-autofocus]",
586
- restoreFocus: true,
587
- trapFocus: true,
588
- });
589
-
590
- useLayoutEffect(() => {
591
- const motion = revealMotion(panelRef.current);
592
- return () => motion?.cancel();
593
- }, []);
594
-
595
- useEffect(() => {
596
- alive.current = true;
597
- return () => {
598
- alive.current = false;
599
- };
600
- }, []);
601
-
602
- useEffect(() => {
603
- let live = true;
604
- setPhase("loading");
605
- setError("");
606
- void previewTestAccount(user.username).then((r) => {
607
- if (!live) return;
608
- if (r.ok) {
609
- setPreview(r.data);
610
- setPhase("ready");
611
- } else {
612
- setError(r.message);
613
- setPhase("error");
614
- }
615
- });
616
- return () => {
617
- live = false;
618
- };
619
- }, [user.username, attempt]);
620
-
621
- useEffect(() => {
622
- if (phase === "created") doneRef.current?.focus();
623
- }, [phase]);
624
-
625
- const create = async () => {
626
- if (phase !== "ready") return;
627
- setPhase("creating");
628
- setError("");
629
- const r = await createTestAccount(user.username);
630
- if (!alive.current) return;
631
- if (r.ok) {
632
- setMade({ username: r.data.user?.username ?? preview?.testUsername ?? "", password: r.data.password });
633
- setPhase("created");
634
- onCreated();
635
- } else {
636
- // Nothing was created, so the list stays as it was.
637
- setError(r.message);
638
- setPhase("ready");
639
- }
640
- };
641
-
642
- const copyPassword = () => {
643
- if (!made) return;
644
- void navigator.clipboard?.writeText(made.password).then(
645
- () => alive.current && setCopied(true),
646
- () => alive.current && setCopied(false)
647
- );
648
- };
649
-
650
- const copy = preview?.copy;
651
- return (
652
- <BodyPortal>
653
- <div className="lp-dialog-scrim">
654
- <div
655
- ref={panelRef}
656
- className="lp-dialog"
657
- role="dialog"
658
- aria-modal="true"
659
- aria-labelledby={titleId}
660
- aria-busy={phase === "loading" || busy || undefined}
661
- data-test-account-dialog={phase}
662
- >
663
- <div className="lp-dialog-head">
664
- <h2 className="lp-dialog-title" id={titleId}>
665
- {phase === "created" ? "Test account ready" : "Create test account"}
666
- </h2>
667
- </div>
668
- {phase === "loading" ? (
669
- <div className="set-skeleton" role="status" aria-label="Loading preview">
670
- <span className="set-skeleton-line" aria-hidden="true" />
671
- <span className="set-skeleton-line" aria-hidden="true" />
672
- <span className="set-skeleton-line" aria-hidden="true" />
673
- </div>
674
- ) : null}
675
- {phase === "error" ? (
676
- <div className="set-error" role="alert">
677
- <span>{error}</span>
678
- <button type="button" className="set-secondary" onClick={() => setAttempt((n) => n + 1)}>
679
- Retry
680
- </button>
681
- </div>
682
- ) : null}
683
- {(phase === "ready" || phase === "creating") && preview && copy ? (
684
- <div className="lp-dialog-body" style={SCROLL_BODY}>
685
- <p>
686
- Copies the access of {name}. Signs in as <strong>{preview.testUsername}</strong>.
687
- </p>
688
- <table className="set-table" aria-label="Access to copy">
689
- <tbody>
690
- <tr><th scope="row">Role</th><td>{copy.role === "admin" ? "Admin" : "Member"}</td></tr>
691
- {/* Per-database access governs once it exists, so the legacy module list would mislead. */}
692
- {copy.databaseAccess?.length ? null : (
693
- <tr><th scope="row">Modules</th><td>{moduleWords(copy.modules)}</td></tr>
694
- )}
695
- </tbody>
696
- </table>
697
- {copy.databaseAccess?.length ? (
698
- <>
699
- <h3 className="set-help">Databases ({copy.databaseAccess.length})</h3>
700
- <div style={LIST_BOX}>
701
- <table className="set-table" aria-label="Database access to copy">
702
- <tbody>
703
- {copy.databaseAccess.map((db) => (
704
- <tr key={db.key}>
705
- <td>{db.label || db.key}</td>
706
- <td style={NOWRAP}>{db.access ? "Open" : "No access"}</td>
707
- </tr>
708
- ))}
709
- </tbody>
710
- </table>
711
- </div>
712
- </>
713
- ) : null}
714
- <h3 className="set-help">Grants ({preview.counts.grants})</h3>
715
- {copy.grants.length ? (
716
- <div style={LIST_BOX}>
717
- <table className="set-table" aria-label="Grants to copy">
718
- <tbody>
719
- {copy.grants.map((g) => (
720
- <tr key={`${g.kind}:${g.oid}:${g.role}`}>
721
- <td style={BREAK}>{g.label}</td>
722
- <td>{GRANT_KINDS[g.kind] ?? g.kind}</td>
723
- <td style={NOWRAP}>{g.role === "edit" ? "Can edit" : "Can view"}</td>
724
- </tr>
725
- ))}
726
- </tbody>
727
- </table>
728
- </div>
729
- ) : (
730
- <p>No shared items</p>
731
- )}
732
- <h3 className="set-help">Locks ({preview.counts.locks})</h3>
733
- {copy.locks.length ? (
734
- <div style={LIST_BOX}>
735
- <table className="set-table" aria-label="Locks to copy">
736
- <tbody>
737
- {copy.locks.map((lock, i) => (
738
- <tr key={`${lock.kind}:${i}`}><td>{lock.label}</td></tr>
739
- ))}
740
- </tbody>
741
- </table>
742
- </div>
743
- ) : (
744
- <p>No locks</p>
745
- )}
746
- {error ? (
747
- <div className="set-error" role="alert">
748
- <span>{error}</span>
749
- </div>
750
- ) : null}
751
- </div>
752
- ) : null}
753
- {phase === "created" && made ? (
754
- <div className="lp-dialog-body">
755
- <p>
756
- <strong>{made.username}</strong> is listed as Test.
757
- </p>
758
- <div className="set-actions">
759
- <code data-test-account-password="">{made.password}</code>
760
- <button type="button" className="set-secondary" onClick={copyPassword}>
761
- {copied ? "Copied" : "Copy"}
762
- </button>
763
- </div>
764
- <p className="set-help">This password will not show again.</p>
765
- </div>
766
- ) : null}
767
- <div className="lp-dialog-actions" style={PINNED}>
768
- {phase === "created" ? (
769
- <button ref={doneRef} type="button" className="set-primary" onClick={onClose}>
770
- Done
771
- </button>
772
- ) : (
773
- <>
774
- <button
775
- type="button"
776
- className="set-secondary"
777
- data-overlay-autofocus=""
778
- disabled={busy}
779
- onClick={dismiss}
780
- >
781
- Cancel
782
- </button>
783
- <button
784
- type="button"
785
- className="set-primary"
786
- disabled={phase !== "ready"}
787
- onClick={() => void create()}
788
- >
789
- <Busy busy={busy} idle="Create test account" doing="Creating" />
790
- </button>
791
- </>
792
- )}
793
- </div>
794
- </div>
795
- </div>
796
- </BodyPortal>
797
- );
798
- }
799
-
800
- function RemoveTestAccount({
801
- user,
802
- onClose,
803
- onRemoved,
804
- }: {
805
- user: AdminUser;
806
- onClose(): void;
807
- onRemoved(): void;
808
- }) {
809
- const [busy, setBusy] = useState(false);
810
- const [error, setError] = useState("");
811
- const name = user.name || user.username;
812
- const remove = async () => {
813
- if (busy) return;
814
- setBusy(true);
815
- setError("");
816
- const r = await removeTestAccount(user.username);
817
- setBusy(false);
818
- if (r.ok) onRemoved();
819
- else setError(r.message);
820
- };
821
- return (
822
- <ConfirmDialog
823
- open
824
- title="Remove test account"
825
- body={
826
- <>
827
- <p>Remove {name}? Its sign in and grants go with it.</p>
828
- {error ? <p className="set-error" role="alert">{error}</p> : null}
829
- </>
830
- }
831
- confirmLabel={busy ? "Removing…" : error ? "Retry" : "Remove"}
832
- tone="destructive"
833
- busy={busy}
834
- onConfirm={() => void remove()}
835
- onCancel={onClose}
836
- />
837
- );
838
- }
839
-
840
  function memberActivity(user: AdminUser) {
841
  const stamp = (user as AdminUser & { last_active?: string | null }).last_active;
842
  const date = stamp ? new Date(stamp) : null;
 
33
  // would not open.
34
  // ---------------------------------------------------------------------------
35
 
36
+ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
37
  // ⭐ W37-T05 — the shared in-flight label; see `ui/Busy.tsx` for why it is a component.
38
  import { Busy } from "../ui/Busy";
39
+ import { useOverlayLayer } from "../customer-grid/OverlaySurface";
 
 
40
  import type { ReactNode } from "react";
41
  import type { SessionUser } from "../shell/session";
42
  import { isAdmin, parseUserEnvelope } from "../shell/session";
43
+ import type { AdminUser, SettingsPayload } from "./settingsApi";
44
  // ⚠ `patchUser` and `setPassword` left this file with `EditUser` (R7) — they are
45
  // imported by `PermsEditor` now, where the form that writes them lives, and it
46
  // spells the patch shape `Parameters<typeof patchUser>[1]` rather than importing
47
  // `UserPatch` separately. `noUnusedLocals` is what walks a move like this down to
48
  // the last orphaned import, which is the tsconfig earning its keep again.
49
  import { clearAvatar, createUser, getSettings, listUsers, setAvatar } from "./settingsApi";
 
50
  import { TOAST_EVENT, UNAUTHORIZED_EVENT } from "../apiContract";
51
  import "../styles/sdlc80/connector-setup.css";
52
  import { PermsEditor } from "./PermsEditor";
 
191
  function UsersPane() {
192
  const [users, setUsers] = useState<AdminUser[] | null>(null);
193
  const [error, setError] = useState("");
194
+ const [denied, setDenied] = useState(false);
195
  const [notice, setNotice] = useState("");
196
  /**
197
  * ⚠ WAVE 17 R7 — ONE STATE, WHERE THERE WERE TWO. This pane held `editing`
 
210
  const reload = useCallback(() => {
211
  setUsers(null);
212
  setError("");
213
+ setDenied(false);
214
  void listUsers().then((r) => {
215
  if (r.ok) {
216
  setUsers(r.data.users ?? []);
217
  setError("");
218
  } else {
219
  setUsers([]);
220
+ if (r.status === 403) setDenied(true);
221
+ else setError(r.message);
222
  }
223
  });
224
  }, []);
225
 
226
  useEffect(reload, [reload]);
227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  // wave17 item 3 (R6). ⚠ `.pg-empty` was the WRONG BOX as well as the wrong
229
  // words: it is the dashed empty-state frame, so a list that was merely still
230
  // arriving was drawn in the chrome that means "there is nothing here".
 
240
  </div>
241
  );
242
 
243
+ if (denied)
244
+ return (
245
+ <div className="set-pane set-users">
246
+ <h3 className="set-h">Manage users</h3>
247
+ <p className="set-notice" role="status">You do not have permission to manage users.</p>
248
+ </div>
249
+ );
250
+
251
  // The FULL-PANE TAKEOVER: the users pane IS the modal body, so returning the
252
  // page here replaces the whole body and nothing else has to know. The rail
253
  // stays — it is chrome, not body, and an admin who wants out of this page
 
352
  // R252 supersedes the card layout. Each row keeps one keyboard destination and a full-row pointer target.
353
  <div className="set-users-table-wrap">
354
  <table className="set-table set-users-table" role="table" aria-label="Manage users">
355
+ <thead role="rowgroup"><tr role="row"><th scope="col">Name</th><th scope="col">Role</th><th scope="col">Access summary</th><th scope="col">Last active</th></tr></thead>
356
  <tbody role="rowgroup">
357
  {users.map((u) => (
358
  <tr key={u.username} role="row" className={u.active ? "" : "is-inactive"} onClick={() => setOpen(u.username)}>
 
368
  <span className="set-acct-top">
369
  <span className="set-acct-name">{u.name || u.username}</span>
370
  {u.active ? null : <span className="set-chip set-chip--off">Deactivated</span>}
 
 
371
  </span>
372
  <span className="set-acct-sub">{u.username}</span>
373
  {/* ⚠ An admin BYPASSES perms entirely (C-PERM amendment 4) —
 
394
  put two tab stops on one destination: the confusion the
395
  ruling names, rebuilt in the accessibility tree. */}
396
  <td role="cell" data-label="Last active">{memberActivity(u)}</td>
 
 
 
 
 
397
  </tr>
398
  ))}
399
  </tbody>
400
  </table>
401
  </div>
402
  )}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
  </div>
404
  );
405
  }
406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
  function memberActivity(user: AdminUser) {
408
  const stamp = (user as AdminUser & { last_active?: string | null }).last_active;
409
  const date = stamp ? new Date(stamp) : null;
web/src/settings/activityLog.css CHANGED
@@ -1,53 +1,131 @@
1
- .activity-pane { min-width: 0; }
2
- .activity-pane .set-notice { font-variant-numeric: tabular-nums; }
3
- .activity-button {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  border: 0;
 
5
  background: transparent;
 
6
  font: inherit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  color: inherit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  cursor: pointer;
9
- border-radius: var(--lp-r-sm);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  text-align: left;
11
  }
12
- .activity-button:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: 2px; }
13
- .activity-button:disabled { opacity: 0.5; cursor: not-allowed; }
14
- .activity-toolbar { display: flex; align-items: center; gap: var(--set-space-2); flex-wrap: wrap; margin: var(--set-space-4) 0; }
15
- .activity-user { display: flex; align-items: center; gap: 8px; min-width: 0; }
16
- .activity-user .pg-select { min-height: 32px; max-width: 100%; }
17
- .activity-user .set-label { margin: 0; }
18
- .activity-retention { font-variant-numeric: tabular-nums; }
19
- .activity-heading { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
20
- .activity-sessions, .activity-timeline, .activity-movement { list-style: none; margin: 0; padding: 0; }
21
- .activity-sessions > li, .activity-event { border-bottom: 1px solid var(--lp-line); }
22
- .activity-session { display: grid; grid-template-columns: minmax(108px, 1.3fr) minmax(72px, 1fr) auto auto auto; align-items: center; gap: 12px; padding: 12px 4px; width: 100%; font-size: var(--lp-fs-xs); }
23
- .activity-session-route, .activity-target { overflow-wrap: anywhere; min-width: 0; }
 
 
 
 
 
24
  .activity-number { font-variant-numeric: tabular-nums; text-align: right; }
25
- .activity-negative { color: var(--lp-red-deep); }
26
- .activity-action, .activity-error-toggle { display: grid; grid-template-columns: 80px minmax(0, 1fr) auto; align-items: start; gap: 12px; padding: 12px 4px; font-size: var(--lp-fs-xs); }
27
- .activity-error-toggle { width: 100%; color: var(--lp-red-deep); }
28
- .activity-event-error { background: var(--lp-red-tint); }
29
- .activity-event-copy { display: flex; flex-direction: column; gap: 4px; min-width: 0; overflow-wrap: anywhere; }
30
- .activity-kind { font-weight: 500; }
31
- .activity-metrics { display: flex; flex-direction: column; gap: 4px; color: var(--lp-muted); }
32
- .activity-source { font-size: var(--lp-fs-2xs); }
33
- .activity-chevron { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.5; transition: transform var(--lp-motion-feedback-duration) var(--lp-motion-feedback-easing); }
34
  .activity-chevron.is-open { transform: rotate(90deg); }
35
- .activity-trail { padding: 8px 12px 16px; background: var(--lp-surface); color: var(--lp-ink); }
36
- .activity-movement > li { display: grid; grid-template-columns: 84px 64px minmax(0, 1fr) auto; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--lp-line); font-size: var(--lp-fs-2xs); }
 
37
  .activity-sample-kind { text-transform: capitalize; }
38
- .activity-pagination .set-help { margin-right: auto; font-variant-numeric: tabular-nums; }
39
- .activity-empty { display: flex; align-items: center; gap: 12px; padding: 16px 0; }
 
 
40
  .activity-empty p { margin: 0; }
41
  @media (hover: hover) and (pointer: fine) {
42
- .activity-button:hover:not(:disabled) { background: var(--lp-blue-tint); }
43
- .activity-error-toggle:hover:not(:disabled) { background: var(--lp-red-tint); }
44
  }
45
  @media (max-width: 760px) {
46
- .activity-session { grid-template-columns: minmax(100px, 1fr) minmax(64px, 1fr) auto; gap: 8px; }
47
- .activity-action, .activity-error-toggle { grid-template-columns: 72px minmax(0, 1fr); gap: 8px; }
48
- .activity-metrics { grid-column: 2; flex-direction: row; justify-content: flex-end; }
49
- .activity-chevron { display: none; }
50
- .activity-movement > li { grid-template-columns: 80px 64px minmax(0, 1fr); }
51
  .activity-movement > li > :last-child { grid-column: 3; }
52
  }
53
  @media (prefers-reduced-motion: reduce) { .activity-chevron { transition: none; } }
 
1
+ .activity-pane { min-width: 0; overflow-anchor: none; }
2
+ .set-modal .activity-pane > :not(:last-child) { margin-bottom: var(--lp-section-gap); }
3
+ .activity-toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: var(--set-space-2); }
4
+ .activity-search {
5
+ display: flex;
6
+ align-items: center;
7
+ flex: 1 1 180px;
8
+ max-width: 240px;
9
+ min-width: 0;
10
+ height: var(--set-control-height);
11
+ gap: var(--set-space-2);
12
+ padding-inline: var(--set-space-2);
13
+ border: var(--lp-hairline-width) solid var(--lp-line);
14
+ border-radius: var(--lp-r-md);
15
+ background: var(--lp-surface);
16
+ color: var(--lp-muted);
17
+ }
18
+ .activity-search input {
19
+ width: 100%;
20
+ min-width: 0;
21
+ padding: 0;
22
  border: 0;
23
+ outline: none;
24
  background: transparent;
25
+ color: var(--lp-ink);
26
  font: inherit;
27
+ font-size: var(--lp-fs-xs);
28
+ }
29
+ .activity-search input::placeholder { color: var(--lp-muted); }
30
+ .activity-search:focus-within { outline: 2px solid var(--lp-blue-deep); outline-offset: 2px; }
31
+ .activity-filter { display: inline-flex; align-items: center; position: relative; min-width: 0; max-width: 100%; color: var(--lp-muted); }
32
+ .activity-filter select {
33
+ appearance: none;
34
+ field-sizing: content;
35
+ min-height: var(--set-control-height);
36
+ max-width: 220px;
37
+ padding: var(--set-space-1) var(--lp-content-inset) var(--set-space-1) var(--set-space-2);
38
+ border: var(--lp-hairline-width) dashed var(--lp-line);
39
+ border-radius: var(--lp-r-md);
40
+ background: var(--lp-surface);
41
  color: inherit;
42
+ font: inherit;
43
+ font-size: var(--lp-fs-xs);
44
+ cursor: pointer;
45
+ text-overflow: ellipsis;
46
+ }
47
+ .activity-filter > svg { position: absolute; right: var(--set-space-2); pointer-events: none; }
48
+ .activity-filter.is-active { color: var(--lp-blue-deep); }
49
+ .activity-filter.is-active select { border-style: solid; border-color: var(--lp-blue-deep); background: var(--lp-blue-tint); }
50
+ .activity-filter select:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: 2px; }
51
+ .activity-filter select:disabled { opacity: 0.5; cursor: not-allowed; }
52
+ .activity-pane .activity-clear {
53
+ border: 0;
54
+ background: transparent;
55
+ font: inherit;
56
+ color: var(--lp-blue-deep);
57
+ min-height: var(--set-control-height);
58
+ padding: var(--set-space-1) var(--set-space-2);
59
+ border-radius: var(--lp-r-md);
60
  cursor: pointer;
61
+ }
62
+ .activity-clear:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: 2px; }
63
+ .activity-clear:disabled { opacity: 0.5; cursor: not-allowed; }
64
+ .activity-pane .activity-refresh { margin-left: auto; padding-inline: var(--set-space-2); }
65
+ .activity-results { min-width: 0; outline: none; }
66
+ .activity-results:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: var(--set-space-1); }
67
+ .activity-groups { display: grid; gap: var(--lp-section-gap); }
68
+ .activity-day-heading {
69
+ margin: 0;
70
+ padding-block: var(--set-space-3);
71
+ border-bottom: var(--lp-hairline-width) solid var(--lp-line);
72
+ color: var(--lp-muted);
73
+ font-size: var(--lp-fs-2xs);
74
+ font-weight: var(--lp-fw-semibold);
75
+ line-height: var(--lp-lh-tight);
76
+ }
77
+ .activity-timeline, .activity-movement { list-style: none; margin: 0; padding: 0; }
78
+ .activity-event { border-bottom: var(--lp-hairline-width) solid var(--lp-line); }
79
+ .activity-entry {
80
+ display: grid;
81
+ grid-template-columns: 32px minmax(0, 1fr) minmax(72px, 22%) 16px;
82
+ align-items: start;
83
+ gap: var(--set-space-3);
84
+ padding: var(--set-space-4) 0;
85
+ width: 100%;
86
+ color: var(--lp-ink);
87
+ font-size: var(--lp-fs-xs);
88
  text-align: left;
89
  }
90
+ .activity-pane .activity-entry-toggle {
91
+ border: 0;
92
+ background: transparent;
93
+ font: inherit;
94
+ color: inherit;
95
+ display: grid;
96
+ border-radius: var(--lp-r-md);
97
+ cursor: pointer;
98
+ }
99
+ .activity-entry-toggle:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: 2px; }
100
+ .activity-event-icon { display: grid; place-items: center; width: 32px; height: 32px; border-radius: var(--lp-r-md); color: var(--lp-muted); background: var(--lp-surface-2); }
101
+ .activity-event-icon.is-error { color: var(--lp-red-deep); background: var(--lp-red-tint); }
102
+ .activity-event-copy { display: flex; flex-direction: column; gap: var(--set-space-1); min-width: 0; overflow-wrap: anywhere; }
103
+ .activity-kind { font-weight: var(--lp-fw-semibold); }
104
+ .activity-target { color: var(--lp-muted); overflow-wrap: anywhere; min-width: 0; }
105
+ .activity-event-meta { display: flex; flex-wrap: wrap; gap: var(--set-space-2); color: var(--lp-muted); font-size: var(--lp-fs-2xs); font-variant-numeric: tabular-nums; }
106
+ .activity-actor { min-width: 0; text-align: right; overflow-wrap: anywhere; }
107
  .activity-number { font-variant-numeric: tabular-nums; text-align: right; }
108
+ .activity-chevron { margin-top: var(--set-space-1); transition: transform var(--lp-motion-feedback-duration) var(--lp-motion-feedback-easing); }
 
 
 
 
 
 
 
 
109
  .activity-chevron.is-open { transform: rotate(90deg); }
110
+ .activity-trail { margin-left: calc(32px + var(--set-space-3)); padding: var(--set-space-3); background: var(--lp-surface-2); color: var(--lp-ink); }
111
+ .activity-trail .set-h4 { margin-bottom: var(--set-space-2); }
112
+ .activity-movement > li { display: grid; grid-template-columns: 84px 64px minmax(0, 1fr) auto; gap: var(--set-space-2); padding: var(--set-space-2) 0; border-bottom: var(--lp-hairline-width) solid var(--lp-line); font-size: var(--lp-fs-2xs); }
113
  .activity-sample-kind { text-transform: capitalize; }
114
+ .activity-pagination { display: flex; align-items: center; flex-wrap: wrap; gap: var(--set-space-2); margin-top: var(--lp-section-gap); }
115
+ .activity-pagination .set-help { margin: 0 auto 0 0; font-variant-numeric: tabular-nums; }
116
+ .activity-retention { margin-top: var(--lp-section-gap); font-variant-numeric: tabular-nums; }
117
+ .activity-empty { display: flex; align-items: center; flex-wrap: wrap; gap: var(--set-space-3); padding-block: var(--lp-section-gap); }
118
  .activity-empty p { margin: 0; }
119
  @media (hover: hover) and (pointer: fine) {
120
+ .activity-pane .activity-entry-toggle:hover, .activity-pane .activity-clear:hover:enabled { background: var(--lp-wash); }
121
+ .activity-filter select:hover:enabled { border-color: var(--lp-blue-deep); }
122
  }
123
  @media (max-width: 760px) {
124
+ .activity-search { max-width: none; flex-basis: 100%; }
125
+ .activity-entry { grid-template-columns: 24px minmax(0, 1fr) minmax(56px, 24%) 12px; gap: var(--set-space-2); }
126
+ .activity-event-icon { width: 24px; height: 24px; }
127
+ .activity-trail { margin-left: 0; }
128
+ .activity-movement > li { grid-template-columns: 72px 56px minmax(0, 1fr); }
129
  .activity-movement > li > :last-child { grid-column: 3; }
130
  }
131
  @media (prefers-reduced-motion: reduce) { .activity-chevron { transition: none; } }
web/src/settings/activityLog.ts CHANGED
@@ -41,6 +41,91 @@ export interface ActivityEvent {
41
  movementDropped?: number;
42
  }
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  const WINDOW_MS = 30_000;
45
  const MAX_MOVEMENT = 500;
46
  const MAX_ACTIONS = 200;
 
41
  movementDropped?: number;
42
  }
43
 
44
+ export interface ActivityLimits {
45
+ retentionDays: number;
46
+ maxSessions: number;
47
+ maxEventsPerSession: number;
48
+ maxMovementSamples: number;
49
+ movementWindowSeconds: number;
50
+ pageSize: number;
51
+ }
52
+
53
+ export interface ActivityEntry {
54
+ event: ActivityEvent;
55
+ actor: string;
56
+ sessionId: string;
57
+ limits: ActivityLimits;
58
+ }
59
+
60
+ export type ActivityTimeframe = "all" | "today" | "week" | "month";
61
+ export interface ActivityFilters {
62
+ search: string;
63
+ actor: string;
64
+ action: ActivityEvent["kind"] | "all";
65
+ timeframe: ActivityTimeframe;
66
+ }
67
+
68
+ export const ACTIVITY_ACTIONS: Record<ActivityEvent["kind"], string> = {
69
+ route: "Navigation",
70
+ click: "Click",
71
+ api: "Request",
72
+ error: "Error",
73
+ };
74
+
75
+ export function activityTargetName(target?: ActivityTarget): string {
76
+ if (!target) return "Element";
77
+ if (target["aria-label"]) return target["aria-label"];
78
+ const section = target.data?.["data-section"];
79
+ if (section && section !== "present") return `${section[0].toUpperCase()}${section.slice(1)}`;
80
+ if (target.id) return `${target.tagName}#${target.id}`;
81
+ if (target.role) return `${target.tagName} (${target.role})`;
82
+ if (target.className) return `${target.tagName}.${target.className.split(" ").join(".")}`;
83
+ return target.tagName;
84
+ }
85
+
86
+ export function activityDescription(event: ActivityEvent): { action: string; target: string } {
87
+ switch (event.kind) {
88
+ case "route": return { action: "Opened", target: event.to || event.route || "Page" };
89
+ case "click": return { action: "Clicked", target: activityTargetName(event.target) };
90
+ case "api": return { action: "Requested", target: `${event.method || "GET"} ${event.url || event.route}` };
91
+ case "error": return { action: event.name || "Error", target: event.message || "Application error." };
92
+ }
93
+ }
94
+
95
+ export function filterActivity(entries: ActivityEntry[], filters: ActivityFilters, now = Date.now()): ActivityEntry[] {
96
+ const start = new Date(now);
97
+ start.setHours(0, 0, 0, 0);
98
+ if (filters.timeframe === "week") start.setDate(start.getDate() - 6);
99
+ if (filters.timeframe === "month") start.setDate(start.getDate() - 29);
100
+ const terms = filters.search.trim().toLocaleLowerCase().split(/\s+/).filter(Boolean);
101
+ return entries.filter(entry => {
102
+ const { event, actor } = entry;
103
+ if (filters.actor && actor !== filters.actor) return false;
104
+ if (filters.action !== "all" && event.kind !== filters.action) return false;
105
+ if (filters.timeframe !== "all" && (event.ts < start.getTime() || event.ts > now)) return false;
106
+ if (!terms.length) return true;
107
+ const description = activityDescription(event);
108
+ const searchable = [actor, ACTIVITY_ACTIONS[event.kind], description.action, description.target,
109
+ event.route, event.source, event.status].join(" ").toLocaleLowerCase();
110
+ return terms.every(term => searchable.includes(term));
111
+ });
112
+ }
113
+
114
+ export function groupActivityByDay(entries: ActivityEntry[]): Array<{ day: string; label: string; entries: ActivityEntry[] }> {
115
+ const groups = new Map<string, { day: string; label: string; entries: ActivityEntry[] }>();
116
+ for (const entry of entries) {
117
+ const date = new Date(entry.event.ts);
118
+ const day = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
119
+ let group = groups.get(day);
120
+ if (!group) {
121
+ group = { day, label: date.toLocaleDateString([], { weekday: "long", month: "long", day: "numeric", year: "numeric" }), entries: [] };
122
+ groups.set(day, group);
123
+ }
124
+ group.entries.push(entry);
125
+ }
126
+ return Array.from(groups.values());
127
+ }
128
+
129
  const WINDOW_MS = 30_000;
130
  const MAX_MOVEMENT = 500;
131
  const MAX_ACTIONS = 200;
web/src/settings/channels/channels.css CHANGED
@@ -150,3 +150,127 @@
150
  .slack-connect .external-wizard-body,
151
  .slack-connect .external-wizard-foot { padding: var(--lp-home-tile-inset); }
152
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  .slack-connect .external-wizard-body,
151
  .slack-connect .external-wizard-foot { padding: var(--lp-home-tile-inset); }
152
  }
153
+
154
+ .set-pane.notify-destinations {
155
+ display: grid;
156
+ grid-template-columns: minmax(0, 1fr);
157
+ gap: var(--lp-section-gap);
158
+ }
159
+ .set-modal .notify-destinations > :not(:last-child),
160
+ .notify-destinations > .set-h { margin: 0; }
161
+ .notify-matrix-scroll {
162
+ min-width: 0;
163
+ max-width: 100%;
164
+ overflow-x: auto;
165
+ border: var(--lp-hairline-width) solid var(--lp-line);
166
+ border-radius: var(--lp-r-md);
167
+ }
168
+ .notify-destinations .notify-matrix { margin: 0; }
169
+ .notify-destinations .notify-matrix th,
170
+ .notify-destinations .notify-matrix td {
171
+ padding: calc(var(--lp-content-inset) / 2);
172
+ vertical-align: middle;
173
+ }
174
+ .notify-destinations .notify-matrix .notify-event {
175
+ min-width: calc(var(--lp-content-inset) * 9);
176
+ width: 100%;
177
+ background: var(--lp-surface);
178
+ color: var(--lp-ink);
179
+ font-size: var(--lp-fs-xs);
180
+ font-weight: 400;
181
+ overflow-wrap: anywhere;
182
+ }
183
+ .notify-destinations .notify-matrix .notify-channel-heading,
184
+ .notify-destinations .notify-matrix .notify-matrix-cell {
185
+ min-width: calc(var(--lp-content-inset) * 6);
186
+ max-width: calc(var(--lp-content-inset) * 10);
187
+ text-align: center;
188
+ }
189
+ .notify-channel-name {
190
+ display: block;
191
+ margin-top: var(--set-space-1);
192
+ font-weight: 400;
193
+ overflow-wrap: anywhere;
194
+ }
195
+ .notify-destinations .notify-matrix .notify-category th {
196
+ padding-block: 0;
197
+ color: var(--lp-ink);
198
+ }
199
+ .notify-category-toggle {
200
+ appearance: none;
201
+ display: flex;
202
+ align-items: center;
203
+ gap: var(--set-space-2);
204
+ width: 100%;
205
+ min-height: var(--set-control-height);
206
+ padding: var(--set-space-2) 0;
207
+ border: 0;
208
+ border-radius: var(--lp-r-sm);
209
+ background: transparent;
210
+ font: inherit;
211
+ color: inherit;
212
+ text-align: left;
213
+ cursor: pointer;
214
+ }
215
+ .notify-category-toggle:focus-visible {
216
+ outline: calc(var(--lp-hairline-width) * 2) solid var(--lp-blue-deep);
217
+ outline-offset: calc(var(--lp-hairline-width) * -2);
218
+ }
219
+ .notify-category-caret {
220
+ flex: none;
221
+ transition: transform var(--lp-motion-feedback-duration) var(--lp-motion-feedback-easing);
222
+ }
223
+ .notify-category-toggle[aria-expanded="true"] .notify-category-caret { transform: rotate(90deg); }
224
+ .notify-matrix tr[hidden] { display: none; }
225
+ @media (hover: hover) and (pointer: fine) {
226
+ .notify-category-toggle:hover { background: var(--lp-wash); }
227
+ }
228
+ @media (prefers-reduced-motion: reduce) {
229
+ .notify-category-caret { transition: none; }
230
+ }
231
+ .notify-matrix tbody:last-child tr:last-child > * { border-bottom: 0; }
232
+ .notify-matrix .notify-channel-list { display: block; margin: 0; }
233
+ .notify-cell-content {
234
+ display: grid;
235
+ justify-items: center;
236
+ gap: var(--set-space-1);
237
+ min-width: 0;
238
+ }
239
+ .notify-matrix .notify-choice {
240
+ align-items: center;
241
+ justify-content: center;
242
+ width: var(--set-control-height);
243
+ min-height: var(--set-control-height);
244
+ }
245
+ .notify-matrix .notify-channel-pick { margin: 0; }
246
+ .notify-matrix .notify-email { font-size: var(--lp-fs-2xs); text-align: center; }
247
+ .notify-delivery-state {
248
+ color: var(--lp-muted);
249
+ font-size: var(--lp-fs-2xs);
250
+ overflow-wrap: anywhere;
251
+ }
252
+ .notify-delivery-state[data-state="sent"] { color: var(--lp-green-deep); }
253
+ .notify-matrix .notify-channel > .notify-destination-reasons { padding-left: 0; }
254
+ .notify-matrix .notify-destination-reason { color: var(--lp-ink); }
255
+ .notify-destinations .notify-slack-status:empty,
256
+ .notify-destinations .notify-save-status:empty { display: none; }
257
+ .notify-destinations .notify-empty { color: var(--lp-ink); }
258
+ .notify-matrix-scroll:focus-visible {
259
+ outline: calc(var(--lp-hairline-width) * 2) solid var(--lp-blue-deep);
260
+ outline-offset: calc(var(--lp-hairline-width) * 2);
261
+ }
262
+ .notify-destinations .notify-matrix-skeleton {
263
+ gap: 0;
264
+ padding: 0;
265
+ border: var(--lp-hairline-width) solid var(--lp-line);
266
+ border-radius: var(--lp-r-md);
267
+ }
268
+ .notify-skeleton-row {
269
+ display: grid;
270
+ grid-template-columns: minmax(0, 1fr) repeat(2, var(--set-control-height));
271
+ align-items: center;
272
+ gap: var(--lp-section-gap);
273
+ padding: calc(var(--lp-content-inset) / 2);
274
+ }
275
+ .notify-skeleton-row + .notify-skeleton-row { border-top: var(--lp-hairline-width) solid var(--lp-line); }
276
+ .notify-skeleton-row .set-skeleton-line:first-child { width: 70%; min-height: var(--lp-fs-md); }