fsanyoto commited on
Commit
62a1265
·
verified ·
1 Parent(s): 86d34c4

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "current": "85131cbfb",
3
  "releases": []
4
  }
 
1
  {
2
+ "current": "0bbd8afab",
3
  "releases": []
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- 85131cbfb
 
1
+ 0bbd8afab
api/routes_aged_stock.py CHANGED
@@ -155,15 +155,7 @@ def write_settings(adsFromDays: int | None = Body(None), priceDownFromDays: int
155
 
156
  @router.post("/refresh")
157
  def refresh(session: Session = Depends(_app_writer)):
158
- identity, stamp = uuid.uuid4().hex, time.time()
159
-
160
- def enqueue(doc):
161
- doc = doc if isinstance(doc, dict) else {}
162
- if doc.get("state") in ("queued", "running") and stamp - float(doc.get("requestedAt") or 0) < 600:
163
- return doc
164
- return {"state": "queued", "requestId": identity, "requestedAt": stamp}
165
-
166
- queued = _call(session.runtime.update, _module().KEY_REFRESH, enqueue)
167
  return JSONResponse(status_code=202, content=queued)
168
 
169
 
 
155
 
156
  @router.post("/refresh")
157
  def refresh(session: Session = Depends(_app_writer)):
158
+ queued = _call(_module().queue_refresh, session.runtime)
 
 
 
 
 
 
 
 
159
  return JSONResponse(status_code=202, content=queued)
160
 
161
 
platform/harness/amazon_store.py CHANGED
@@ -199,7 +199,7 @@ def _refresh_account_status(con, entries, rt):
199
  enabled = read_database_enablement(rt)["databases"]
200
  tables = [t for t in TABLES if enabled.get(amazon_schema.DATABASE_PREFIX + t)]
201
  for table in TABLES:
202
- for family in ("", "sync_tick_", WATERMARK_PREFIX, BACKFILL_FLOOR_PREFIX, "refused_"):
203
  key = entity_key(family + table)
204
  states = [_read_state(con, _account_prefix(e["connection_id"]) + key) for e in entries]
205
  present = [s for s in states if s]
@@ -1943,6 +1943,52 @@ def _record_brand_analytics_limit(con):
1943
  detail=json.dumps({"reason": BRAND_ANALYTICS_LIMIT}))
1944
 
1945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1946
  def sqp_batches(asins, max_chars=200):
1947
  """Stable, lossless space-joined options within Amazon's character ceiling."""
1948
  if type(max_chars) is not int or max_chars < 1:
@@ -4371,7 +4417,7 @@ def _record_state(con, key, phase, rows=0, detail=None):
4371
  "INSERT OR REPLACE INTO _sync_state "
4372
  "(entity, phase, last_id, cursor_wd, rows, updated_at) VALUES (?,?,?,?,?,?)",
4373
  [str(key), str(phase), None, (str(detail) if payload_key.startswith(("amzn_reconcile_", "amzn_limit_", "amzn_refused_", "amzn_pending_report_",
4374
- "amzn_resync_"))
4375
  else str(detail)[:300]) if detail else None, int(rows or 0),
4376
  _iso(datetime.now(timezone.utc))])
4377
 
@@ -5800,6 +5846,15 @@ def sync(tenant_key="royal-imports", tables=None, since=None, log=print, rt=None
5800
  _refuse_unreconciled(tenant_key, rt, report, log)
5801
  if getattr(_PUBLISHED, "flag", False):
5802
  _after_sync(tenant_key, report, log)
 
 
 
 
 
 
 
 
 
5803
  return report
5804
 
5805
 
@@ -6284,7 +6339,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
6284
  prov = amazon_schema.PROVENANCE.get(table, "measured_at_sync")
6285
  root = cx.REPORT_TYPES[table].get("json_root")
6286
  # History completion does not prove that legacy rows carry a report period.
6287
- period_repair_mark = None
6288
  if not first_load and cx.REPORT_TYPES[table].get("weekly"):
6289
  names = set(_columns_of(con, mirror))
6290
  dated = ({"_period_start", "_period_end"} <= names and con.execute(
@@ -6293,8 +6348,10 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
6293
  if not dated:
6294
  period_repair_mark = read_watermark(con, table)
6295
  repair_start, repair_end = _weekly_period_repair(con, table, end)
6296
- plan = {**plan, "start": repair_start, "end": repair_end,
6297
- "cause": "repair missing period fields from a completed earlier week"}
 
 
6298
  log(f"{table}: {plan['cause']}")
6299
  sibling = SHARED_REPORT_SIBLINGS.get(table)
6300
  sibling_plan = None
@@ -6488,16 +6545,31 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
6488
  # Lane F/combine own the vocabulary decision; no credential or row enters C2.
6489
  documents += 1
6490
  probe = _DocumentProbe(table, rtype)
6491
- with _report_resume(
6492
- con, table, rtype, start=_iso(w0),
6493
- end=_iso(w1 - timedelta(seconds=1)) if _periodic(cx.REPORT_TYPES[table]) else _iso(w1), max_wait=max_wait,
6494
- log=lambda message: log(f"{table}: {message}"),
6495
- **({"options": {**REPORT_OPTIONS.get(table, {}), "reportPeriod": cx.REPORT_TYPES[table]["period"]}}
6496
- if cx.REPORT_TYPES[table].get("period") else
6497
- {"options": dict(REPORT_OPTIONS[table])} if table in REPORT_OPTIONS else {})) as resume_kwargs:
6498
- _sync_event("amazon.sync", tenant_key, table,
6499
- {"state": "access", "operation": "report_document"}, rt, report, log)
6500
- text, meta = cx.report_document(fields, rtype, **resume_kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6501
  _DocumentProbe.downloaded(text)
6502
  landed_window = True
6503
  if root:
@@ -6578,6 +6650,8 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
6578
  if _periodic(cx.REPORT_TYPES[table]):
6579
  _record_reconciliation(con, table, {"start": _iso(w0), "end": _iso(w1),
6580
  "count": len(rows)})
 
 
6581
  probe.finish(len(rows) if cols else 0)
6582
  spend_rows(len(rows) if cols else 0)
6583
  if first_load:
@@ -6666,13 +6740,24 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
6666
  "rows": total_rows, "columns": column_count, "provenance": prov,
6667
  "report": rtype, "window": plan["cause"], "incremental": plan["incremental"],
6668
  "operations": operations}
 
 
 
 
 
6669
  _record_state(con, key, "done", total_rows)
6670
  _record_state(con, entity_key("sync_tick_" + table), "done", total_rows)
 
 
 
6671
  if not any(v["state"] in ("refused", "throttled") for v in operations.values()):
6672
  mark = settlement_mark if table == "settlements" else plan["end"]
6673
  if mark is not None and period_repair_mark is not None:
6674
  # Repairing an earlier week cannot rewind completed history.
6675
  mark = max(mark, period_repair_mark)
 
 
 
6676
  has_window = (cx.REPORT_TYPES[table].get("snapshot") or table in ("settlements", SQP_TABLE)
6677
  or landed_window or (first_load and floors[table] is not None))
6678
  if mark is not None and has_window:
@@ -9462,10 +9547,186 @@ def selftest(log=print):
9462
  ck("NC-AZ34 a spooled document parses and counts exactly as its text does", same, detail)
9463
  bounded, detail = _bounded_document_selftest()
9464
  ck("NC-AZ35 a large synthetic report parses and writes in bounded memory", bounded, detail)
 
9465
  log(f"\n{'GREEN' if not fails else 'RED'}: {checks - len(fails)}/{checks}")
9466
  return 0 if not fails else 1
9467
 
9468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9469
  def _spool_fixture(text, *, gz=False, encoding="utf-8"):
9470
  """A `ReportDocument` made by the client's own spool path from local bytes, no network."""
9471
  import gzip
 
199
  enabled = read_database_enablement(rt)["databases"]
200
  tables = [t for t in TABLES if enabled.get(amazon_schema.DATABASE_PREFIX + t)]
201
  for table in TABLES:
202
+ for family in ("", "sync_tick_", WATERMARK_PREFIX, BACKFILL_FLOOR_PREFIX, "refused_", "waiting_"):
203
  key = entity_key(family + table)
204
  states = [_read_state(con, _account_prefix(e["connection_id"]) + key) for e in entries]
205
  present = [s for s in states if s]
 
1943
  detail=json.dumps({"reason": BRAND_ANALYTICS_LIMIT}))
1944
 
1945
 
1946
+ def _week_not_ready(exc):
1947
+ """Amazon's FATAL for a week it has not finished processing, which is a wait and never a refusal."""
1948
+ return (getattr(exc, "status", None) == "FATAL"
1949
+ and bool(re.search(r"\bnot available yet\b", str(getattr(exc, "vendor_reason", "") or exc), re.I)))
1950
+
1951
+
1952
+ def _vendor_error_details(exc):
1953
+ """Amazon's `errorDetails` text from a FATAL error document, else its whole reason."""
1954
+ text = str(getattr(exc, "vendor_reason", "") or exc)
1955
+ try:
1956
+ parsed = json.loads(text)
1957
+ except (ValueError, TypeError):
1958
+ return text[:500]
1959
+ return str(parsed.get("errorDetails") or text)[:500] if isinstance(parsed, dict) else text[:500]
1960
+
1961
+
1962
+ def _record_week_wait(con, table, w0, w1, exc):
1963
+ """B2 contract: `waiting_<table>` names the unfinished week; `weekEnd` is its Saturday, as Amazon names it."""
1964
+ _record_state(con, entity_key("waiting_" + table), "waiting", detail=json.dumps({
1965
+ "weekStart": _utc(w0).date().isoformat(), "weekEnd": (_utc(w1) - timedelta(days=1)).date().isoformat(),
1966
+ "error": _vendor_error_details(exc)}))
1967
+
1968
+
1969
+ def _clear_week_wait(con, table, w0):
1970
+ """A landed week at or after the waited one deletes its wait: B2's readers do not filter on phase."""
1971
+ key = entity_key("waiting_" + table)
1972
+ saved = _read_state(con, key)
1973
+ if not saved or saved.get("state") != "waiting":
1974
+ return
1975
+ try:
1976
+ week = json.loads(saved.get("detail") or "{}").get("weekStart") or ""
1977
+ except (ValueError, TypeError, AttributeError):
1978
+ week = ""
1979
+ if not week or week <= _utc(w0).date().isoformat():
1980
+ con.execute("DELETE FROM _sync_state WHERE entity=?", [key])
1981
+
1982
+
1983
+ def _newest_week(con, mirror):
1984
+ """The newest ISO `_period_start` date the mirror holds, or None."""
1985
+ if "_period_start" not in set(_columns_of(con, mirror)):
1986
+ return None
1987
+ day = _weekly_date_sql('"_period_start"')
1988
+ row = con.execute(f"SELECT MAX({day}) FROM {mirror}").fetchone()
1989
+ return row[0] if row else None
1990
+
1991
+
1992
  def sqp_batches(asins, max_chars=200):
1993
  """Stable, lossless space-joined options within Amazon's character ceiling."""
1994
  if type(max_chars) is not int or max_chars < 1:
 
4417
  "INSERT OR REPLACE INTO _sync_state "
4418
  "(entity, phase, last_id, cursor_wd, rows, updated_at) VALUES (?,?,?,?,?,?)",
4419
  [str(key), str(phase), None, (str(detail) if payload_key.startswith(("amzn_reconcile_", "amzn_limit_", "amzn_refused_", "amzn_pending_report_",
4420
+ "amzn_resync_", "amzn_waiting_"))
4421
  else str(detail)[:300]) if detail else None, int(rows or 0),
4422
  _iso(datetime.now(timezone.utc))])
4423
 
 
5846
  _refuse_unreconciled(tenant_key, rt, report, log)
5847
  if getattr(_PUBLISHED, "flag", False):
5848
  _after_sync(tenant_key, report, log)
5849
+ # A weekly history repair publishes each week on its own, so after-sync callbacks see every week.
5850
+ for table, row in list((report.get("tables") or {}).items()):
5851
+ week = row.get("nextWeek") if isinstance(row, dict) else None
5852
+ if not week:
5853
+ continue
5854
+ following = sync(tenant_key, tables=[table], since=_utc(week), log=log, rt=rt, max_wait=max_wait)
5855
+ report["tables"][table] = (following.get("tables") or {}).get(table, row)
5856
+ for key in ("problems", "audit_problems"):
5857
+ report.setdefault(key, []).extend(following.get(key) or [])
5858
  return report
5859
 
5860
 
 
6339
  prov = amazon_schema.PROVENANCE.get(table, "measured_at_sync")
6340
  root = cx.REPORT_TYPES[table].get("json_root")
6341
  # History completion does not prove that legacy rows carry a report period.
6342
+ period_repair_mark = period_repair_next = waited_start = None
6343
  if not first_load and cx.REPORT_TYPES[table].get("weekly"):
6344
  names = set(_columns_of(con, mirror))
6345
  dated = ({"_period_start", "_period_end"} <= names and con.execute(
 
6348
  if not dated:
6349
  period_repair_mark = read_watermark(con, table)
6350
  repair_start, repair_end = _weekly_period_repair(con, table, end)
6351
+ # Two finished weeks, oldest first: this pass lands the older, `sync` publishes the newer next.
6352
+ period_repair_next = repair_start
6353
+ plan = {**plan, "start": repair_start - timedelta(days=7), "end": repair_end,
6354
+ "cause": "repair missing period fields from the two completed earlier weeks"}
6355
  log(f"{table}: {plan['cause']}")
6356
  sibling = SHARED_REPORT_SIBLINGS.get(table)
6357
  sibling_plan = None
 
6545
  # Lane F/combine own the vocabulary decision; no credential or row enters C2.
6546
  documents += 1
6547
  probe = _DocumentProbe(table, rtype)
6548
+ try:
6549
+ with _report_resume(
6550
+ con, table, rtype, start=_iso(w0),
6551
+ end=_iso(w1 - timedelta(seconds=1)) if _periodic(cx.REPORT_TYPES[table]) else _iso(w1), max_wait=max_wait,
6552
+ log=lambda message: log(f"{table}: {message}"),
6553
+ **({"options": {**REPORT_OPTIONS.get(table, {}), "reportPeriod": cx.REPORT_TYPES[table]["period"]}}
6554
+ if cx.REPORT_TYPES[table].get("period") else
6555
+ {"options": dict(REPORT_OPTIONS[table])} if table in REPORT_OPTIONS else {})) as resume_kwargs:
6556
+ _sync_event("amazon.sync", tenant_key, table,
6557
+ {"state": "access", "operation": "report_document"}, rt, report, log)
6558
+ text, meta = cx.report_document(fields, rtype, **resume_kwargs)
6559
+ except cx.AmazonReportError as exc:
6560
+ if not (weekly and _week_not_ready(exc)):
6561
+ raise
6562
+ # An unfinished week is a wait, never a refusal: ask the week before it, and this one next pass.
6563
+ probe._halt.set()
6564
+ waited_start = w0 if waited_start is None else min(waited_start, w0)
6565
+ _record_week_wait(con, table, w0, w1, exc)
6566
+ prior = (w0 - timedelta(days=7), w0)
6567
+ held = _newest_week(con, mirror)
6568
+ log(f"{table}: Amazon has not finished the week starting {w0.date().isoformat()}; "
6569
+ "sync asks for it again next pass")
6570
+ if prior not in windows and (held is None or held < prior[0].date()):
6571
+ windows.append(prior)
6572
+ continue
6573
  _DocumentProbe.downloaded(text)
6574
  landed_window = True
6575
  if root:
 
6650
  if _periodic(cx.REPORT_TYPES[table]):
6651
  _record_reconciliation(con, table, {"start": _iso(w0), "end": _iso(w1),
6652
  "count": len(rows)})
6653
+ if weekly:
6654
+ _clear_week_wait(con, table, w0)
6655
  probe.finish(len(rows) if cols else 0)
6656
  spend_rows(len(rows) if cols else 0)
6657
  if first_load:
 
6740
  "rows": total_rows, "columns": column_count, "provenance": prov,
6741
  "report": rtype, "window": plan["cause"], "incremental": plan["incremental"],
6742
  "operations": operations}
6743
+ if waited_start is not None:
6744
+ report["tables"][table]["waiting"] = json.loads(
6745
+ (_read_state(con, entity_key("waiting_" + table)) or {}).get("detail") or "{}")
6746
+ elif period_repair_next is not None and landed_window:
6747
+ report["tables"][table]["nextWeek"] = _iso(period_repair_next)
6748
  _record_state(con, key, "done", total_rows)
6749
  _record_state(con, entity_key("sync_tick_" + table), "done", total_rows)
6750
+ if weekly and landed_window:
6751
+ # A landed week closes any earlier refusal of this table.
6752
+ con.execute("DELETE FROM _sync_state WHERE entity=?", [entity_key("refused_" + table)])
6753
  if not any(v["state"] in ("refused", "throttled") for v in operations.values()):
6754
  mark = settlement_mark if table == "settlements" else plan["end"]
6755
  if mark is not None and period_repair_mark is not None:
6756
  # Repairing an earlier week cannot rewind completed history.
6757
  mark = max(mark, period_repair_mark)
6758
+ if mark is not None and waited_start is not None:
6759
+ # The week Amazon has not finished stays owed, so the next pass asks for it again.
6760
+ mark = min(mark, waited_start)
6761
  has_window = (cx.REPORT_TYPES[table].get("snapshot") or table in ("settlements", SQP_TABLE)
6762
  or landed_window or (first_load and floors[table] is not None))
6763
  if mark is not None and has_window:
 
9547
  ck("NC-AZ34 a spooled document parses and counts exactly as its text does", same, detail)
9548
  bounded, detail = _bounded_document_selftest()
9549
  ck("NC-AZ35 a large synthetic report parses and writes in bounded memory", bounded, detail)
9550
+ ck("NC-AZ38 brand analytics weekly wait and two week history fixtures", weekly_wait_selftest(log) == 0)
9551
  log(f"\n{'GREEN' if not fails else 'RED'}: {checks - len(fails)}/{checks}")
9552
  return 0 if not fails else 1
9553
 
9554
 
9555
+ def weekly_wait_selftest(log=print, check=None):
9556
+ """Brand analytics: an unfinished week is a wait with a fallback, and an undated mirror loads two weeks."""
9557
+ from contextlib import contextmanager
9558
+ from types import SimpleNamespace
9559
+ from unittest.mock import patch
9560
+ import duckdb
9561
+ cx, module, failures = _client(), sys.modules[__name__], []
9562
+
9563
+ def ck(name, probe):
9564
+ try:
9565
+ ok = bool(probe())
9566
+ except Exception as exc: # noqa: BLE001
9567
+ ok = False
9568
+ log(f" {name}: {type(exc).__name__}: {exc}")
9569
+ if check:
9570
+ check("[weekly wait] " + name, ok)
9571
+ else:
9572
+ log(f" {'ok' if ok else 'FAIL'} {name}")
9573
+ if not ok:
9574
+ failures.append(name)
9575
+
9576
+ class Clock(datetime):
9577
+ instant = datetime(2026, 10, 25, 8, 27, tzinfo=timezone.utc)
9578
+
9579
+ @classmethod
9580
+ def now(cls, tz=None):
9581
+ return cls.instant.astimezone(tz)
9582
+
9583
+ pending = ("Reporting data for end date [2026-10-24] and time period [weekly] is not available yet. "
9584
+ "This date may not have finished processing, or may fall outside the available data range.")
9585
+
9586
+ def fixture(*, undated=False, refusal=None):
9587
+ con = duckdb.connect(":memory:")
9588
+ con.execute("CREATE TABLE _sync_state (entity VARCHAR PRIMARY KEY, phase VARCHAR, last_id BIGINT, "
9589
+ "cursor_wd VARCHAR, rows BIGINT, updated_at VARCHAR)")
9590
+ state = SimpleNamespace(con=con, requests=[], unready={"2026-10-18"}, refusal=refusal, callbacks=[])
9591
+ if undated:
9592
+ con.execute("CREATE TABLE amzn_brand_analytics AS SELECT 'legacy' AS search_term, "
9593
+ "'Home' AS department_name, 'B0LEGACY01' AS clicked_asin, 1::BIGINT AS click_share_rank, "
9594
+ "NULL::VARCHAR AS _period_start, NULL::VARCHAR AS _period_end")
9595
+ record_watermark(con, "brand_analytics", datetime(2026, 10, 1, tzinfo=timezone.utc), 1)
9596
+ record_backfill_floor(con, "brand_analytics", datetime(2026, 8, 1, tzinfo=timezone.utc))
9597
+ _record_state(con, entity_key("brand_analytics"), "refused", 1, "fixture prior not available yet")
9598
+ _record_state(con, entity_key("refused_brand_analytics"), "refused",
9599
+ detail=json.dumps({"reason": pending, "amazonStatus": "FATAL", "attempts": 1}))
9600
+ _record_reconciliation(con, "brand_analytics", {
9601
+ "start": "2026-09-01T00:00:00Z", "end": "2026-10-01T00:00:00Z", "count": 3521339})
9602
+ _record_state(con, entity_key(FIRST_LOAD_END_KEY), "done", detail="2026-10-01T00:00:00Z")
9603
+ return state
9604
+
9605
+ def document(state):
9606
+ def answer(_fields, report_type, **kwargs):
9607
+ week = kwargs["start"][:10]
9608
+ state.requests.append(week)
9609
+ if state.refusal:
9610
+ raise cx.AmazonReportError(f"Amazon ended the {report_type} report as FATAL (reportId f)",
9611
+ state.refusal, "FATAL")
9612
+ if week in state.unready:
9613
+ raise cx.AmazonReportError(f"Amazon ended the {report_type} report as FATAL (reportId f)",
9614
+ json.dumps({"errorDetails": pending}), "FATAL")
9615
+ body = json.dumps({"dataByDepartmentAndSearchTerm": [
9616
+ {"departmentName": "Home", "searchTerm": term, "clickedAsin": "B0WEEK0001",
9617
+ "clickShareRank": 1, "searchFrequencyRank": 3} for term in ("legacy", "term " + week)]})
9618
+ return body, {"reportDocumentId": "week-" + week}
9619
+ return answer
9620
+
9621
+ def patches(state):
9622
+ @contextmanager
9623
+ def published(*_args, **_kwargs):
9624
+ yield
9625
+ _note_published()
9626
+
9627
+ def after(_tenant, report, _log):
9628
+ if "brand_analytics" in _synced_tables(report):
9629
+ state.callbacks.append(_newest_week(state.con, "amzn_brand_analytics").isoformat())
9630
+
9631
+ def accounts(tenant_key, **options):
9632
+ return _sync_candidate(tenant_key, **options)
9633
+ return [patch.object(module, "datetime", Clock), patch.object(module, "_sync_credential", return_value={}),
9634
+ patch.object(module, "account_entries", return_value=[{"connection_id": "c", "label": "c"}]),
9635
+ patch.object(module, "restore_mirror", return_value={"dirty": set(), "problems": []}),
9636
+ patch.object(module, "_connect_tenant_mirror", side_effect=lambda *_: (state.con.cursor(), None)),
9637
+ patch.object(module, "retention_check", return_value={"earliestTriggerAt": None}),
9638
+ patch.object(module, "read_database_enablement", return_value={"available": True, "databases": {
9639
+ amazon_schema.DATABASE_PREFIX + t: t == "brand_analytics" for t in TABLES}}),
9640
+ patch.object(module, "_sync_event"), patch.object(module, "persist_mirror"),
9641
+ patch.object(module, "watched_terms", return_value=({"legacy"}, None)),
9642
+ patch.object(module, "_sync_accounts_candidate", side_effect=accounts),
9643
+ patch.object(module, "_refuse_unreconciled"), patch.object(module, "_after_sync", side_effect=after),
9644
+ patch.object(_mirrors, "sync_mirror", published),
9645
+ patch.object(cx, "report_document", side_effect=document(state))]
9646
+
9647
+ def run(state, call, **kwargs):
9648
+ from contextlib import ExitStack
9649
+ with ExitStack() as stack:
9650
+ for item in patches(state):
9651
+ stack.enter_context(item)
9652
+ return call("fixture", tables=["brand_analytics"], rt=SimpleNamespace(key="fixture"),
9653
+ max_wait=1, log=lambda *_: None, **kwargs)
9654
+
9655
+ def marker(state, name):
9656
+ return _read_state(state.con, entity_key(name)) or {}
9657
+
9658
+ def waited(state):
9659
+ detail = json.loads(marker(state, "waiting_brand_analytics").get("detail") or "{}")
9660
+ return (marker(state, "waiting_brand_analytics").get("state") == "waiting"
9661
+ and detail == {"weekStart": "2026-10-18", "weekEnd": "2026-10-24", "error": pending}
9662
+ and marker(state, "brand_analytics").get("state") == "done"
9663
+ and not marker(state, "refused_brand_analytics")
9664
+ and not _refused_reports(state.con))
9665
+
9666
+ # Fix 1: a fresh first load asks the newest closed week, waits on it, and lands the week before.
9667
+ fresh = fixture()
9668
+ first = run(fresh, _sync_candidate, first_load=True, report_budget=6)
9669
+ ck("BA-W1 an unfinished week records a wait and the preceding week lands in the same pass", lambda:
9670
+ fresh.requests == ["2026-10-18", "2026-10-11"] and waited(fresh) and not first["problems"]
9671
+ and first["tables"]["brand_analytics"]["waiting"]["weekStart"] == "2026-10-18"
9672
+ and _newest_week(fresh.con, "amzn_brand_analytics").isoformat() == "2026-10-11"
9673
+ and read_watermark(fresh.con, "brand_analytics") == datetime(2026, 10, 18, tzinfo=timezone.utc))
9674
+ Clock.instant += timedelta(days=1)
9675
+ fresh.requests.clear()
9676
+ run(fresh, _sync_candidate)
9677
+ ck("BA-W2 the next pass asks the waited week again and skips a fallback the mirror holds", lambda:
9678
+ fresh.requests == ["2026-10-18"] and waited(fresh))
9679
+ fresh.unready.clear()
9680
+ fresh.requests.clear()
9681
+ _record_state(fresh.con, entity_key("brand_analytics"), "refused", 0, "fixture stale refusal")
9682
+ _record_state(fresh.con, entity_key("refused_brand_analytics"), "refused", detail="{}")
9683
+ landed = run(fresh, _sync_candidate)
9684
+ ck("BA-W3 the waited week lands, its wait is deleted and a stale refusal clears", lambda:
9685
+ fresh.requests == ["2026-10-18"] and "waiting" not in landed["tables"]["brand_analytics"]
9686
+ and not marker(fresh, "waiting_brand_analytics")
9687
+ and marker(fresh, "brand_analytics").get("state") == "done" and not marker(fresh, "refused_brand_analytics")
9688
+ and read_watermark(fresh.con, "brand_analytics") == datetime(2026, 10, 25, tzinfo=timezone.utc))
9689
+ Clock.instant -= timedelta(days=1)
9690
+ # Negative controls: another FATAL stays a refusal, and the old classifier stores the wait as one.
9691
+ refused = fixture(refusal="Brand Registry enrollment is required for this report.")
9692
+ run(refused, _sync_candidate, first_load=True, report_budget=6)
9693
+ ck("BA-W4 NC a FATAL that is not a wait still stores the refusal", lambda:
9694
+ refused.requests == ["2026-10-18"] and marker(refused, "brand_analytics").get("state") == "refused"
9695
+ and marker(refused, "refused_brand_analytics") and not marker(refused, "waiting_brand_analytics"))
9696
+ blind = fixture()
9697
+ with patch.object(module, "_week_not_ready", return_value=False):
9698
+ run(blind, _sync_candidate, first_load=True, report_budget=6)
9699
+ ck("BA-W5 NC without the wait classifier the same answer reads refused", lambda:
9700
+ blind.requests == ["2026-10-18"] and not waited(blind)
9701
+ and marker(blind, "brand_analytics").get("state") == "refused")
9702
+
9703
+ # Fix 2: an undated mirror loads the two newest finished weeks, oldest first, one publication each.
9704
+ staging = fixture(undated=True)
9705
+ result = run(staging, sync)
9706
+ periods = staging.con.execute("SELECT _period_start, count(*) FROM amzn_brand_analytics "
9707
+ "WHERE _period_start IS NOT NULL GROUP BY 1 ORDER BY 1").fetchall()
9708
+ ck("BA-W6 an undated mirror loads two weeks oldest first and the callbacks see each week", lambda:
9709
+ staging.requests == ["2026-10-04", "2026-10-11"] and staging.callbacks == ["2026-10-04", "2026-10-11"]
9710
+ and periods == [("2026-10-04", 1), ("2026-10-11", 2)] and not result["problems"]
9711
+ and marker(staging, "brand_analytics").get("state") == "done"
9712
+ and not marker(staging, "refused_brand_analytics")
9713
+ and staging.con.execute("SELECT count(*) FROM amzn_brand_analytics WHERE _period_start IS NULL").fetchone()[0] == 1
9714
+ and read_watermark(staging.con, "brand_analytics") == datetime(2026, 10, 18, tzinfo=timezone.utc))
9715
+ staging.requests.clear()
9716
+ run(staging, sync)
9717
+ ck("BA-W7 the following pass asks the newest closed week and waits on it without a refusal", lambda:
9718
+ staging.requests == ["2026-10-18"] and waited(staging) and len(staging.callbacks) == 3
9719
+ and read_watermark(staging.con, "brand_analytics") == datetime(2026, 10, 18, tzinfo=timezone.utc))
9720
+ single, publish = fixture(undated=True), sync
9721
+ with patch.object(module, "sync", side_effect=lambda *_a, **_k: {"tables": {}}):
9722
+ run(single, publish)
9723
+ ck("BA-W8 NC one publication per pass alone gives the callbacks one week", lambda:
9724
+ single.requests == ["2026-10-04"] and single.callbacks == ["2026-10-04"])
9725
+ for state in (fresh, refused, blind, staging, single):
9726
+ state.con.close()
9727
+ return len(failures)
9728
+
9729
+
9730
  def _spool_fixture(text, *, gz=False, encoding="utf-8"):
9731
  """A `ReportDocument` made by the client's own spool path from local bytes, no network."""
9732
  import gzip
platform/modules/aged_stock.py CHANGED
@@ -26,6 +26,7 @@ import hashlib
26
  import json
27
  import re
28
  import time
 
29
  from pathlib import Path
30
 
31
  APP_LABEL = "Aged stock"
@@ -41,6 +42,10 @@ DECISIONS = {"approved": "Accepted", "dismissed": "Dismissed"}
41
  DEFAULTS = {"ads_from_days": 91, "price_down_from_days": 181, "remove_from_days": 271,
42
  "fee_horizon_days": 90, "due_days": 7}
43
  NOT_READY = "The aged stock view starts after the next Amazon storage fee sync."
 
 
 
 
44
  LIMIT = ("Each aged stock document is limited to 128 MB by the store. About 40,000 SKUs with two years "
45
  "of monthly fees fit in the items document, and history splits by month. Split the items "
46
  "document by action when it nears that size.")
@@ -868,6 +873,33 @@ def _page_size(value):
868
  return min(size, MAX_PAGE_SIZE)
869
 
870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
871
  def summary(tenant_key, *, offset=0, limit=24, st=None):
872
  """Aggregate fees and counts in SQL and return one month page without SKU rows."""
873
  st = _rt(tenant_key, st)
@@ -875,8 +907,11 @@ def summary(tenant_key, *, offset=0, limit=24, st=None):
875
  out = _select(st, SUMMARY_SQL, lambda key: (key(KEY_ITEMS), offset // limit * limit, limit, limit, limit))
876
  doc = out.pop("meta") or {}
877
  ready = doc.get("state") == "ready"
 
 
 
878
  return {**out, "trend": out.get("trend") or {}, "totals": out.get("totals") or {}, "monthLimit": limit,
879
- "ready": ready, "notReady": None if ready else (doc.get("message") or NOT_READY),
880
  "notices": list(doc.get("notices") or ()), "primaryCurrency": doc.get("primaryCurrency"),
881
  "statusCounts": {s: int((out.get("statusCounts") or {}).get(s, 0)) for s in STATUSES},
882
  "actionCounts": {a: int((out.get("actionCounts") or {}).get(a, 0)) for a in ACTIONS},
 
26
  import json
27
  import re
28
  import time
29
+ import uuid
30
  from pathlib import Path
31
 
32
  APP_LABEL = "Aged stock"
 
42
  DEFAULTS = {"ads_from_days": 91, "price_down_from_days": 181, "remove_from_days": 271,
43
  "fee_horizon_days": 90, "due_days": 7}
44
  NOT_READY = "The aged stock view starts after the next Amazon storage fee sync."
45
+ #: The App opened before any refresh: the summary read queued one, so the mirror is being read now.
46
+ READING = "Reading your Amazon storage fees now. The view fills in when they are read."
47
+ #: A queued or running refresh younger than this is kept, never queued twice.
48
+ REFRESH_HOLD_SECONDS = 600
49
  LIMIT = ("Each aged stock document is limited to 128 MB by the store. About 40,000 SKUs with two years "
50
  "of monthly fees fit in the items document, and history splits by month. Split the items "
51
  "document by action when it nears that size.")
 
873
  return min(size, MAX_PAGE_SIZE)
874
 
875
 
876
+ def queue_refresh(st, *, now=None):
877
+ """Queue one durable refresh intent for the sync worker, or keep the one already queued or running."""
878
+ identity, stamp = uuid.uuid4().hex, time.time() if now is None else now
879
+
880
+ def enqueue(doc):
881
+ doc = doc if isinstance(doc, dict) else {}
882
+ if (doc.get("state") in ("queued", "running")
883
+ and stamp - float(doc.get("requestedAt") or 0) < REFRESH_HOLD_SECONDS):
884
+ return doc
885
+ return {"state": "queued", "requestId": identity, "requestedAt": stamp}
886
+ return st.update(KEY_REFRESH, enqueue)
887
+
888
+
889
+ def _first_open(st, now=None):
890
+ """The waiting line when no refresh ever wrote the items doc. Queues the refresh POST /refresh queues."""
891
+ stamp = time.time() if now is None else now
892
+ try:
893
+ pending = _doc(st, KEY_REFRESH)
894
+ if (pending.get("state") == "failed"
895
+ and stamp - float(pending.get("finishedAt") or 0) < REFRESH_HOLD_SECONDS):
896
+ return pending.get("message") or NOT_READY
897
+ queued = queue_refresh(st, now=stamp)
898
+ except Exception: # noqa: BLE001
899
+ return NOT_READY
900
+ return READING if (queued or {}).get("state") in ("queued", "running") else NOT_READY
901
+
902
+
903
  def summary(tenant_key, *, offset=0, limit=24, st=None):
904
  """Aggregate fees and counts in SQL and return one month page without SKU rows."""
905
  st = _rt(tenant_key, st)
 
907
  out = _select(st, SUMMARY_SQL, lambda key: (key(KEY_ITEMS), offset // limit * limit, limit, limit, limit))
908
  doc = out.pop("meta") or {}
909
  ready = doc.get("state") == "ready"
910
+ waiting = None if ready else (doc.get("message") or NOT_READY)
911
+ if not ready and not doc.get("state"):
912
+ waiting = _first_open(st)
913
  return {**out, "trend": out.get("trend") or {}, "totals": out.get("totals") or {}, "monthLimit": limit,
914
+ "ready": ready, "notReady": waiting,
915
  "notices": list(doc.get("notices") or ()), "primaryCurrency": doc.get("primaryCurrency"),
916
  "statusCounts": {s: int((out.get("statusCounts") or {}).get(s, 0)) for s in STATUSES},
917
  "actionCounts": {a: int((out.get("actionCounts") or {}).get(a, 0)) for a in ACTIONS},
platform/modules/opportunity_finder.py CHANGED
@@ -22,8 +22,10 @@ import contextlib
22
  import contextvars
23
  import datetime as dt
24
  import hashlib
 
25
  import logging
26
  import os
 
27
  import threading
28
  from pathlib import Path
29
 
@@ -48,15 +50,25 @@ BA_NEED = ("department_name", "search_term", "search_frequency_rank", "_period_s
48
  TRIGGER = "brand_analytics"
49
 
50
  NO_MIRROR = "The Amazon mirror is not on this server yet. It appears after the first sync."
51
- NOT_SYNCED = "ut_amzn_brand_analytics has not synced yet, so no search term can be compared."
 
 
 
 
 
 
 
 
 
 
 
 
52
  #: The mirror holds the table but this read failed or was busy. `{cause}` is the error class.
53
  READ_FAILED = ("The Amazon mirror could not be read just now ({cause}), so no search term can be compared. "
54
  "Try again in a minute.")
55
  BAD_PERIOD = ("The newest brand analytics week start reads {value}, which is not an ISO date, so no week "
56
  "can be compared.")
57
- NO_DEPARTMENTS = ("No brand analytics row shows one of your ASINs yet, so the finder has no department to "
58
- "search. It reads departments from ut_amzn_brand_analytics and your ASINs from "
59
- "ut_amzn_search_query_performance and ut_amzn_listings.")
60
  NOT_READY = "The finder compares two weekly syncs. One week is held so far."
61
  NO_HISTORY = "No weekly history yet. The first week is written at the next Amazon sync."
62
  READ_ONLY = "Your account can view the opportunity finder but not change it."
@@ -339,6 +351,81 @@ def _derived_departments(cur, ours):
339
  return sorted(_text(r[0]) for r in rows if _text(r[0]))
340
 
341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
  def _synced(facts):
343
  return set(BA_NEED) <= facts["columns"] and facts["period"] is not None
344
 
@@ -385,7 +472,10 @@ def _source(cur, st, fresh=False):
385
  return out
386
  out["periodType"] = facts.get("period_type")
387
  if not _synced(facts):
388
- out["problems"].append(NOT_SYNCED)
 
 
 
389
  return out
390
  if not iso_period(facts["period"]):
391
  out["problems"].append(BAD_PERIOD.format(value=facts["period"]))
 
22
  import contextvars
23
  import datetime as dt
24
  import hashlib
25
+ import json
26
  import logging
27
  import os
28
+ import re
29
  import threading
30
  from pathlib import Path
31
 
 
50
  TRIGGER = "brand_analytics"
51
 
52
  NO_MIRROR = "The Amazon mirror is not on this server yet. It appears after the first sync."
53
+ #: Brand analytics holds no rows. Each cause below gets its own sentence, never this one over held rows.
54
+ NOT_SYNCED = "Brand analytics has not synced yet, so no search term can be compared."
55
+ #: Rows synced before weeks were dated. The next pass on such a table loads the two newest weeks.
56
+ NO_WEEK = "Brand analytics rows carry no week date, so the next Amazon sync loads the two newest weeks."
57
+ NO_FIELDS = "Brand analytics rows miss a field the finder reads, so no search term can be compared."
58
+ #: Amazon has not finished the newest week. `{day}` is its end date in words, such as October 24.
59
+ WAITING = "Amazon has not finished the week ending {day} yet, so the finder reads it at the next sync."
60
+ WAITING_NEXT = "Amazon has not finished the newest week yet, so the finder reads it at the next sync."
61
+ REFUSED = ("Amazon refused the brand analytics report. Check Brand Registry and the Brand Analytics role "
62
+ "in Seller Central.")
63
+ #: `_sync_state` markers of the brand analytics pass, matched bare or behind an account prefix.
64
+ WAITING_KEY = "amzn_waiting_brand_analytics"
65
+ REFUSED_KEY = "amzn_refused_brand_analytics"
66
  #: The mirror holds the table but this read failed or was busy. `{cause}` is the error class.
67
  READ_FAILED = ("The Amazon mirror could not be read just now ({cause}), so no search term can be compared. "
68
  "Try again in a minute.")
69
  BAD_PERIOD = ("The newest brand analytics week start reads {value}, which is not an ISO date, so no week "
70
  "can be compared.")
71
+ NO_DEPARTMENTS = "No brand analytics row shows one of your ASINs yet, so the finder has no department to search."
 
 
72
  NOT_READY = "The finder compares two weekly syncs. One week is held so far."
73
  NO_HISTORY = "No weekly history yet. The first week is written at the next Amazon sync."
74
  READ_ONLY = "Your account can view the opportunity finder but not change it."
 
351
  return sorted(_text(r[0]) for r in rows if _text(r[0]))
352
 
353
 
354
+ _MONTHS = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
355
+ "November", "December")
356
+
357
+
358
+ def _day_words(value):
359
+ """`2026-10-24` as `October 24`, or None when the text holds no ISO date."""
360
+ match = re.search(r"\d{4}-\d{2}-\d{2}", _text(value))
361
+ try:
362
+ day = dt.date.fromisoformat(match.group(0)) if match else None
363
+ except ValueError:
364
+ day = None
365
+ return f"{_MONTHS[day.month - 1]} {day.day}" if day else None
366
+
367
+
368
+ def ba_marker(rows):
369
+ """("waiting", day words or None), ("refused", None) or None from (entity, detail, updated_at) rows.
370
+
371
+ The newest of the waiting and refused markers wins. A refusal whose text says the week is not
372
+ available yet is Amazon still building that week, not a refusal."""
373
+ found = []
374
+ for entity, detail, updated in rows or ():
375
+ entity = _text(entity)
376
+ for key in (WAITING_KEY, REFUSED_KEY):
377
+ if entity == key or entity.endswith("__" + key):
378
+ found.append((_text(updated), key, _text(detail)))
379
+ if not found:
380
+ return None
381
+ _updated, key, detail = max(found)
382
+ try:
383
+ body = json.loads(detail)
384
+ except ValueError:
385
+ body = None
386
+ body = body if isinstance(body, dict) else {}
387
+ said = " ".join(_text(body.get(k)) for k in ("error", "reason", "diagnostic")) or detail
388
+ # A detail cut short is not JSON, so the end date is also read straight from the text.
389
+ end = (_text(body.get("weekEnd")) or _found(r'"weekEnd"\s*:\s*"(\d{4}-\d{2}-\d{2})', detail)
390
+ or _found(r"end date \[(\d{4}-\d{2}-\d{2})\]", said))
391
+ if key == WAITING_KEY or "not available yet" in said.lower():
392
+ return "waiting", _day_words(end)
393
+ return "refused", None
394
+
395
+
396
+ def _found(pattern, text):
397
+ match = re.search(pattern, text or "")
398
+ return match.group(1) if match else ""
399
+
400
+
401
+ def _markers(cur):
402
+ """The brand analytics marker rows in the mirror's `_sync_state`. A missing table or a failed read is none."""
403
+ try:
404
+ cols = {str(r[0]) for r in _bounded(
405
+ cur, "SELECT column_name FROM information_schema.columns WHERE table_name = '_sync_state'")}
406
+ if not {"entity", "cursor_wd"} <= cols:
407
+ return []
408
+ when = "CAST(updated_at AS VARCHAR)" if "updated_at" in cols else "NULL"
409
+ return _bounded(cur, f"SELECT entity, CAST(cursor_wd AS VARCHAR), {when} FROM _sync_state "
410
+ "WHERE ends_with(entity, ?) OR ends_with(entity, ?)", [WAITING_KEY, REFUSED_KEY])
411
+ except _ReadFailed:
412
+ return []
413
+
414
+
415
+ def not_synced(cur, columns):
416
+ """One plain sentence per cause that keeps brand analytics from comparing a week. A failed row probe raises."""
417
+ held = bool(columns) and bool(_bounded(cur, f"SELECT 1 FROM {BA_TABLE} LIMIT 1"))
418
+ marker = ba_marker(_markers(cur))
419
+ if marker and marker[0] == "refused":
420
+ return [REFUSED]
421
+ out = []
422
+ if marker:
423
+ out.append(WAITING.format(day=marker[1]) if marker[1] else WAITING_NEXT)
424
+ if held:
425
+ out.append(NO_FIELDS if set(BA_NEED) - set(columns) - {"_period_start"} else NO_WEEK)
426
+ return out or [NOT_SYNCED]
427
+
428
+
429
  def _synced(facts):
430
  return set(BA_NEED) <= facts["columns"] and facts["period"] is not None
431
 
 
472
  return out
473
  out["periodType"] = facts.get("period_type")
474
  if not _synced(facts):
475
+ try:
476
+ out["problems"].extend(not_synced(cur, facts["columns"]))
477
+ except _ReadFailed as exc:
478
+ out["problems"].append(READ_FAILED.format(cause=exc))
479
  return out
480
  if not iso_period(facts["period"]):
481
  out["problems"].append(BAD_PERIOD.format(value=facts["period"]))
platform/modules/ranking_diagnostic.py CHANGED
@@ -96,15 +96,33 @@ LIMIT = ("Each week of history is one store document, limited to 128 MB. About 3
96
  #: Accounts that sign in for a process, never a person who can own an item.
97
  SERVICE_ACCOUNTS = ("admin", "automation", "scheduler", "qa-runner", "leadership")
98
 
99
- LABELS = {PRICING: "ut_amzn_pricing", CATALOG: "ut_amzn_catalog_items",
100
- BRAND_ANALYTICS: "ut_amzn_brand_analytics", ECONOMICS: "ut_amzn_economics",
101
- FBA_INVENTORY: "ut_amzn_fba_inventory", SUPPRESSED: "ut_amzn_suppressed_listings",
102
- STRANDED: "ut_amzn_stranded_inventory", PLANNING: "ut_amzn_inventory_planning"}
 
103
  #: What a missing source stops the App from checking.
104
  MISSING = {PRICING: "sessions, units and Buy Box share", CATALOG: "sales rank",
105
  BRAND_ANALYTICS: "search-term share", ECONOMICS: "ad spend",
106
  FBA_INVENTORY: "out of stock", SUPPRESSED: "suppressed listings",
107
  STRANDED: "stranded inventory", PLANNING: "price against the featured offer"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
 
110
  def _now():
@@ -357,12 +375,15 @@ def _catalog(cur):
357
  def _search_share(cur):
358
  """({asin: summed click share}, period) from the newest brand analytics week."""
359
  cols = _columns(cur, BRAND_ANALYTICS)
360
- if not {"clicked_asin", "click_share"} <= cols:
361
  return None, None
362
  where = _newest(cols, "_period_start", BRAND_ANALYTICS)
363
  period = None
364
  if "_period_start" in cols:
365
  period = cur.execute(f'SELECT MAX(CAST("_period_start" AS VARCHAR)) FROM {BRAND_ANALYTICS}').fetchone()[0]
 
 
 
366
  rows = cur.execute(f"SELECT {_col(cols, 'clicked_asin')}, SUM({_col(cols, 'click_share', 'DOUBLE')}) "
367
  f"FROM {BRAND_ANALYTICS} {where} GROUP BY 1").fetchall()
368
  return {a: round(float(s or 0.0), 6) for a, s in rows if a}, (str(period) if period else None)
@@ -432,6 +453,83 @@ def _missing(table):
432
  return f"{LABELS[table]} has not synced yet, so {MISSING[table]} is not checked."
433
 
434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
  def snapshot(cur, carry=None):
436
  """This sync's point per ASIN in Sales and Traffic, the P10 totals, and each source it could not read.
437
 
@@ -460,7 +558,7 @@ def snapshot(cur, carry=None):
460
  for table, got in ((CATALOG, catalog), (BRAND_ANALYTICS, shares), (ECONOMICS, spend),
461
  (FBA_INVENTORY, stock), (SUPPRESSED, suppressed), (STRANDED, stranded), (PLANNING, prices)):
462
  if got is None:
463
- problems.append(_missing(table))
464
  points = {}
465
  for asin in sorted(pricing):
466
  p = pricing[asin]
@@ -852,10 +950,32 @@ def _list_page(st, names, population, *, cursor=None, limit=DEFAULT_LIMIT, statu
852
  return chosen, nxt, counts, int(total)
853
 
854
 
855
- def _problems(st):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
856
  held = _index(st)
857
- out = list(_paths(st, WEEK_PREFIX + held[-1], [("problems",)])[0] or ()) if held else [
858
  "No weekly history yet. The first point is written at the next Amazon sync."]
 
859
  if len(held) == 1:
860
  out.append(NOT_READY)
861
  error = _doc(st, KEY_INDEX).get("last_error")
@@ -874,7 +994,7 @@ def items(tenant_key, user=None, *, status=None, cursor=None, limit=DEFAULT_LIMI
874
  chosen, nxt, counts, total = _list_page(st, names, "items", cursor=cursor, limit=limit, status=status, asin=asin, q=q)
875
  page = [{"id": iid, **{k: v for k, v in row.items() if k != "history"}} for iid, row in chosen]
876
  return {"items": page, "cursor": nxt, "counts": {s: counts.get(s, 0) for s in STATUSES}, "total": total,
877
- "problems": _problems(st),
878
  "week": week or (held[-1] if held else None), "weeks": held}
879
 
880
 
 
96
  #: Accounts that sign in for a process, never a person who can own an item.
97
  SERVICE_ACCOUNTS = ("admin", "automation", "scheduler", "qa-runner", "leadership")
98
 
99
+ #: The plain name of each source on screen. Never the internal `ut_amzn_*` key.
100
+ LABELS = {PRICING: "Sales and traffic", CATALOG: "Catalog items",
101
+ BRAND_ANALYTICS: "Brand analytics", ECONOMICS: "Ad and fee economics",
102
+ FBA_INVENTORY: "FBA inventory", SUPPRESSED: "Suppressed listings",
103
+ STRANDED: "Stranded inventory", PLANNING: "Inventory planning"}
104
  #: What a missing source stops the App from checking.
105
  MISSING = {PRICING: "sessions, units and Buy Box share", CATALOG: "sales rank",
106
  BRAND_ANALYTICS: "search-term share", ECONOMICS: "ad spend",
107
  FBA_INVENTORY: "out of stock", SUPPRESSED: "suppressed listings",
108
  STRANDED: "stranded inventory", PLANNING: "price against the featured offer"}
109
+ #: Why brand analytics gives no search-term share, one sentence per cause. Held rows never read as not synced.
110
+ BA_NO_WEEK = "Brand analytics rows carry no week date, so search-term share is checked after the next Amazon sync."
111
+ BA_NO_FIELDS = "Brand analytics rows miss a field the diagnostic reads, so search-term share is not checked."
112
+ BA_WAITING = ("Amazon has not finished the week ending {day} yet, so search-term share is checked after the "
113
+ "next sync.")
114
+ BA_WAITING_NEXT = "Amazon has not finished the newest week yet, so search-term share is checked after the next sync."
115
+ BA_REFUSED = ("Amazon refused the brand analytics report, so search-term share is not checked. Check Brand "
116
+ "Registry and the Brand Analytics role in Seller Central.")
117
+ #: `_sync_state` markers of the brand analytics pass, matched bare or behind an account prefix.
118
+ WAITING_KEY = "amzn_waiting_brand_analytics"
119
+ REFUSED_KEY = "amzn_refused_brand_analytics"
120
+ #: The columns a search-term share reads.
121
+ BA_NEED = ("clicked_asin", "click_share")
122
+ #: Stored lines that name the brand analytics source, read again from the mirror on screen.
123
+ _BA_PREFIXES = ("Brand analytics ", "Amazon has not finished the ", "Amazon refused the brand analytics",
124
+ "ut_amzn_brand_analytics ")
125
+ _OLD_MISSING = re.compile(r"^ut_(amzn_[a-z_]+) has not synced yet, so (.+) is not checked\.$")
126
 
127
 
128
  def _now():
 
375
  def _search_share(cur):
376
  """({asin: summed click share}, period) from the newest brand analytics week."""
377
  cols = _columns(cur, BRAND_ANALYTICS)
378
+ if not set(BA_NEED) <= cols:
379
  return None, None
380
  where = _newest(cols, "_period_start", BRAND_ANALYTICS)
381
  period = None
382
  if "_period_start" in cols:
383
  period = cur.execute(f'SELECT MAX(CAST("_period_start" AS VARCHAR)) FROM {BRAND_ANALYTICS}').fetchone()[0]
384
+ if period is None:
385
+ # No dated week: a zero share per ASIN would read as a fall, so the source is not checked.
386
+ return None, None
387
  rows = cur.execute(f"SELECT {_col(cols, 'clicked_asin')}, SUM({_col(cols, 'click_share', 'DOUBLE')}) "
388
  f"FROM {BRAND_ANALYTICS} {where} GROUP BY 1").fetchall()
389
  return {a: round(float(s or 0.0), 6) for a, s in rows if a}, (str(period) if period else None)
 
453
  return f"{LABELS[table]} has not synced yet, so {MISSING[table]} is not checked."
454
 
455
 
456
+ _MONTHS = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
457
+ "November", "December")
458
+
459
+
460
+ def _found(pattern, text):
461
+ match = re.search(pattern, text or "")
462
+ return match.group(1) if match else ""
463
+
464
+
465
+ def _day_words(value):
466
+ """`2026-10-24` as `October 24`, or None when the text holds no ISO date."""
467
+ try:
468
+ day = dt.date.fromisoformat(_found(r"(\d{4}-\d{2}-\d{2})", _text(value)))
469
+ except ValueError:
470
+ return None
471
+ return f"{_MONTHS[day.month - 1]} {day.day}"
472
+
473
+
474
+ def ba_marker(rows):
475
+ """("waiting", day words or None), ("refused", None) or None from (entity, detail, updated_at) rows.
476
+
477
+ The newest of the waiting and refused markers wins. A refusal that says the week is not available
478
+ yet is Amazon still building that week, not a refusal."""
479
+ found = []
480
+ for entity, detail, updated in rows or ():
481
+ entity = _text(entity)
482
+ for key in (WAITING_KEY, REFUSED_KEY):
483
+ if entity == key or entity.endswith("__" + key):
484
+ found.append((_text(updated), key, _text(detail)))
485
+ if not found:
486
+ return None
487
+ _updated, key, detail = max(found)
488
+ try:
489
+ body = json.loads(detail)
490
+ except ValueError:
491
+ body = None
492
+ body = body if isinstance(body, dict) else {}
493
+ said = " ".join(_text(body.get(k)) for k in ("error", "reason", "diagnostic")) or detail
494
+ # A detail cut short is not JSON, so the end date is also read straight from the text.
495
+ end = (_text(body.get("weekEnd")) or _found(r'"weekEnd"\s*:\s*"(\d{4}-\d{2}-\d{2})', detail)
496
+ or _found(r"end date \[(\d{4}-\d{2}-\d{2})\]", said))
497
+ if key == WAITING_KEY or "not available yet" in said.lower():
498
+ return "waiting", _day_words(end)
499
+ return "refused", None
500
+
501
+
502
+ def _markers(cur):
503
+ """The brand analytics marker rows in the mirror's `_sync_state`. A missing table or a failed read is none."""
504
+ try:
505
+ cols = _columns(cur, "_sync_state")
506
+ if not {"entity", "cursor_wd"} <= cols:
507
+ return []
508
+ when = "CAST(updated_at AS VARCHAR)" if "updated_at" in cols else "NULL"
509
+ return cur.execute(f"SELECT entity, CAST(cursor_wd AS VARCHAR), {when} FROM _sync_state "
510
+ "WHERE ends_with(entity, ?) OR ends_with(entity, ?)", [WAITING_KEY, REFUSED_KEY]).fetchall()
511
+ except Exception: # noqa: BLE001
512
+ return []
513
+
514
+
515
+ def ba_lines(cur):
516
+ """One plain line per cause that keeps search-term share unchecked. A failed row probe raises `_ReadFailed`."""
517
+ cols = _columns(cur, BRAND_ANALYTICS)
518
+ try:
519
+ held = bool(cols) and bool(cur.execute(f"SELECT 1 FROM {BRAND_ANALYTICS} LIMIT 1").fetchall())
520
+ except Exception as exc: # noqa: BLE001
521
+ raise _ReadFailed(type(exc).__name__) from exc
522
+ marker = ba_marker(_markers(cur))
523
+ if marker and marker[0] == "refused":
524
+ return [BA_REFUSED]
525
+ out = []
526
+ if marker:
527
+ out.append(BA_WAITING.format(day=marker[1]) if marker[1] else BA_WAITING_NEXT)
528
+ if held:
529
+ out.append(BA_NO_FIELDS if set(BA_NEED) - cols else BA_NO_WEEK)
530
+ return out or [_missing(BRAND_ANALYTICS)]
531
+
532
+
533
  def snapshot(cur, carry=None):
534
  """This sync's point per ASIN in Sales and Traffic, the P10 totals, and each source it could not read.
535
 
 
558
  for table, got in ((CATALOG, catalog), (BRAND_ANALYTICS, shares), (ECONOMICS, spend),
559
  (FBA_INVENTORY, stock), (SUPPRESSED, suppressed), (STRANDED, stranded), (PLANNING, prices)):
560
  if got is None:
561
+ problems.extend(ba_lines(cur) if table == BRAND_ANALYTICS else [_missing(table)])
562
  points = {}
563
  for asin in sorted(pricing):
564
  p = pricing[asin]
 
950
  return chosen, nxt, counts, int(total)
951
 
952
 
953
+ def _plain(line):
954
+ """A stored line in today's plain words: an older `ut_amzn_*` not-synced line names its source plainly."""
955
+ match = _OLD_MISSING.match(_text(line))
956
+ return _missing(match.group(1)) if match and match.group(1) in LABELS else line
957
+
958
+
959
+ def _live_ba(tenant_key, st, stored):
960
+ """The stored brand analytics lines, replaced by today's cause while the mirror still gives no share."""
961
+ if tenant_key is None or not any(_text(p).startswith(_BA_PREFIXES) for p in stored):
962
+ return stored
963
+ try:
964
+ with _reader(tenant_key, st) as cur:
965
+ if cur is None or _search_share(cur)[0] is not None:
966
+ return stored
967
+ live = ba_lines(cur)
968
+ except Exception: # noqa: BLE001
969
+ return stored
970
+ kept = [p for p in stored if not _text(p).startswith(_BA_PREFIXES)]
971
+ return kept + [p for p in live if p not in kept]
972
+
973
+
974
+ def _problems(st, tenant_key=None):
975
  held = _index(st)
976
+ out = [_plain(p) for p in _paths(st, WEEK_PREFIX + held[-1], [("problems",)])[0] or ()] if held else [
977
  "No weekly history yet. The first point is written at the next Amazon sync."]
978
+ out = _live_ba(tenant_key, st, out)
979
  if len(held) == 1:
980
  out.append(NOT_READY)
981
  error = _doc(st, KEY_INDEX).get("last_error")
 
994
  chosen, nxt, counts, total = _list_page(st, names, "items", cursor=cursor, limit=limit, status=status, asin=asin, q=q)
995
  page = [{"id": iid, **{k: v for k, v in row.items() if k != "history"}} for iid, row in chosen]
996
  return {"items": page, "cursor": nxt, "counts": {s: counts.get(s, 0) for s in STATUSES}, "total": total,
997
+ "problems": _problems(st, tenant_key),
998
  "week": week or (held[-1] if held else None), "weeks": held}
999
 
1000
 
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 4dc3f9c04973060388ccc1b686880fe16cee3c65597ee729bc699e47175954fb
 
1
+ 384590ac94c0c9119c8af60d10fb6dcf4c7ad850d52fbd530f6ee8eb9d5035fe
web/src/apps/aged-stock/AppView.tsx CHANGED
@@ -720,6 +720,29 @@ export default function AgedStockView({ application, canEdit }: AgedStockAppView
720
  if (!loadingPage) setNavigation({ scope, cursor: next });
721
  };
722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
723
  const refresh = async () => {
724
  setRefreshing(true);
725
  setRefreshError("");
 
720
  if (!loadingPage) setNavigation({ scope, cursor: next });
721
  };
722
 
723
+ // A first open queues a refresh server side, so a waiting view follows it and loads once it is done.
724
+ const waiting = screen !== null && !screen.ready;
725
+ useEffect(() => {
726
+ if (!waiting || refreshing) return;
727
+ let stopped = false;
728
+ const deadline = Date.now() + 120000;
729
+ const follow = async () => {
730
+ while (!stopped && Date.now() < deadline) {
731
+ await new Promise((resolve) => window.setTimeout(resolve, 2000));
732
+ if (stopped) return;
733
+ const response = await fetch(url("/refresh"), { credentials: CREDENTIALS }).catch(() => null);
734
+ const state = response?.ok ? await response.json().catch(() => ({})) as { state?: string } : {};
735
+ if (stopped || state.state === "failed" || !state.state) return;
736
+ if (state.state === "done") {
737
+ setAttempt((count) => count + 1);
738
+ return;
739
+ }
740
+ }
741
+ };
742
+ void follow();
743
+ return () => { stopped = true; };
744
+ }, [waiting, refreshing, url]);
745
+
746
  const refresh = async () => {
747
  setRefreshing(true);
748
  setRefreshError("");
web/src/apps/templateMarks.tsx CHANGED
@@ -2,7 +2,7 @@
2
  // R36: navy strokes and a gold accent, painted by app-marks.css from tokens. No hex here.
3
  // An unknown or missing mark draws DEFAULT_MARK, so a kind added to C1 before its art still renders.
4
  import type { ReactElement } from "react";
5
- import { ConnectorMark } from "../connectors/brandMarks";
6
  import "../styles/sdlc79/app-marks.css";
7
 
8
  const INK = "app-mark-ink";
@@ -87,12 +87,27 @@ export function TemplateMark({ mark, size = 24 }: { mark?: unknown; size?: numbe
87
  </svg>;
88
  }
89
 
90
- /** C2: the two marks every App frame carries: the App's own, and its connector's at 16 px on the right. */
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  export function AppFrameMarks({ mark, connector }: { mark?: unknown; connector?: unknown }): ReactElement {
 
92
  return <>
93
- <span className="cg-app-frame-mark" data-mark={hasTemplateMark(mark) ? mark : "default"} aria-hidden="true">
94
- <TemplateMark mark={mark} size={20} />
 
95
  </span>
96
- <ConnectorMark connector={connector} className="cg-app-frame-connector" size={16} />
97
  </>;
98
  }
 
2
  // R36: navy strokes and a gold accent, painted by app-marks.css from tokens. No hex here.
3
  // An unknown or missing mark draws DEFAULT_MARK, so a kind added to C1 before its art still renders.
4
  import type { ReactElement } from "react";
5
+ import { BRAND_OF_CONNECTOR, ConnectorMark } from "../connectors/brandMarks";
6
  import "../styles/sdlc79/app-marks.css";
7
 
8
  const INK = "app-mark-ink";
 
87
  </svg>;
88
  }
89
 
90
+ /** True when an App's own icon is its source's brand: every Amazon App wears the Amazon mark. */
91
+ export function appWearsBrand(connector: unknown): boolean {
92
+ return typeof connector === "string" && Object.prototype.hasOwnProperty.call(BRAND_OF_CONNECTOR, connector)
93
+ && BRAND_OF_CONNECTOR[connector] === "amazon";
94
+ }
95
+
96
+ /** An App's own icon on every surface: the source brand for an Amazon App, else its designed mark. */
97
+ export function AppMark({ mark, connector, size = 24 }: { mark?: unknown; connector?: unknown; size?: number }) {
98
+ return appWearsBrand(connector)
99
+ ? <ConnectorMark connector={connector} size={size} className="lp-source-mark app-mark-brand" />
100
+ : <TemplateMark mark={mark} size={size} />;
101
+ }
102
+
103
+ /** C2: an App frame's marks. The connector mark sits at 16 px on the right unless the App mark is that brand. */
104
  export function AppFrameMarks({ mark, connector }: { mark?: unknown; connector?: unknown }): ReactElement {
105
+ const branded = appWearsBrand(connector);
106
  return <>
107
+ <span className="cg-app-frame-mark" data-mark={hasTemplateMark(mark) ? mark : "default"}
108
+ aria-hidden={branded ? undefined : "true"}>
109
+ <AppMark mark={mark} connector={connector} size={20} />
110
  </span>
111
+ {branded ? null : <ConnectorMark connector={connector} className="cg-app-frame-connector" size={16} />}
112
  </>;
113
  }
web/src/home/HomePage.tsx CHANGED
@@ -21,7 +21,7 @@
21
  // ---------------------------------------------------------------------------
22
 
23
  import { useEffect, useId, useRef, useState } from "react";
24
- import { hasTemplateMark, TemplateMark } from "../apps/templateMarks";
25
  import type { DragEvent, MouseEvent, ReactNode } from "react";
26
  import { useRailSelection } from "../ui/useRailSelection";
27
  import { IconTile } from "../ui/IconTile";
@@ -322,6 +322,8 @@ export interface ApplicationTile {
322
  type: string;
323
  templateKind?: string;
324
  templateLabel?: string;
 
 
325
  deepLink?: string;
326
  compatibleDatabases?: string[];
327
  openTarget?: { database?: string | null };
@@ -500,16 +502,18 @@ function DatabaseDeleteBody({ footprint }: { footprint: TableFootprint | null })
500
  * One text body for every grid and list tile: the name, then the kind and (while searching) the
501
  * folder path. No opened or edited line: owner item 23 revoked R57's meta line.
502
  */
503
- export function HomeTileText({ tile, fallback = "", index = null, searching = false }: {
504
  tile: HomeFindTile;
505
  fallback?: string;
506
  index?: ModuleFolderIndex | null;
507
  searching?: boolean;
 
 
508
  }) {
509
  const kind = fallback.trim();
510
  const folder = searching && kind ? homeFindFolderName(index, moduleTileId(tile)) : "";
511
  return <span className="home-tile-text">
512
- <TruncatedName>{tile.name ?? tile.label ?? ""}</TruncatedName>
513
  {kind ? <span className="home-tile-ago">{[kind, folder].filter(Boolean).join(" · ")}</span>
514
  : searching ? <HomeFindFolder index={index} id={moduleTileId(tile)} /> : null}
515
  </span>;
@@ -1651,7 +1655,6 @@ export function HomeSurface({
1651
  const database = application.openTarget?.database
1652
  ?? application.compatibleDatabases?.[0]
1653
  ?? "";
1654
- const kind = application.templateLabel ?? "";
1655
  return (
1656
  <ItemTileMenu key={application.id} id={application.id} kind="App" name={application.name}
1657
  canRename={application.capabilities?.edit === true}
@@ -1693,11 +1696,13 @@ export function HomeSurface({
1693
  }}
1694
  >
1695
  <IconTile surface="home" fill="tint">
1696
- {hasTemplateMark(application.templateKind)
1697
- ? <TemplateMark mark={application.templateKind!} size={16} /> : <ApplicationIcon />}
 
1698
  </IconTile>
 
1699
  <HomeTileText tile={{ id: application.id, name: application.name }}
1700
- fallback={kind}
1701
  index={moduleIndex} searching={searching} />
1702
  <SharedMark mark={shareMarks.get(shareMarkKey("app", application.id))} fade={shareMarksFade} pending={shareMarksPending} />
1703
  </a>
@@ -2187,7 +2192,7 @@ export default function HomePage(props: HomeProps) {
2187
  }
2188
  };
2189
  const [tenantApplications, setApplications] = useState<ApplicationTile[]>([]);
2190
- const [applicationTemplateLabels, setApplicationTemplateLabels] = useState<Record<string, string>>({});
2191
  const [applicationsKnown, setApplicationsKnown] = useState(false);
2192
  const [applicationsError, setApplicationsError] = useState("");
2193
  const [applicationBusy, setApplicationBusy] = useState<Set<string>>(() => new Set());
@@ -2232,11 +2237,12 @@ export default function HomePage(props: HomeProps) {
2232
  const options = { credentials: CREDENTIALS, signal: controller.signal };
2233
  void fetch(`${API_V1}/nav/application-templates`, options)
2234
  .then(async (response) => response.ok ? parseApplicationTemplates(await response.json()) : [])
2235
- .then((templates) => Object.fromEntries(templates.map((template) => [template.kind, template.label])))
2236
- .catch(() => ({}))
2237
- .then((templateLabels) => {
 
2238
  if (!controller.signal.aborted && requestEpoch === applicationRequestEpoch.current) {
2239
- setApplicationTemplateLabels(templateLabels);
2240
  }
2241
  });
2242
  void fetch(`${API_V1}/applications`, options).then(readApplications)
@@ -2369,7 +2375,10 @@ export default function HomePage(props: HomeProps) {
2369
  // server wall use the Application id "finance_ar-collections". An App tile is its `id`, always.
2370
  const asTile = (row: ApplicationTile): ApplicationTile => {
2371
  const { key: _registryKey, ...tile } = row as ApplicationTile & { key?: unknown };
2372
- return { ...tile, templateLabel: row.templateLabel ?? applicationTemplateLabels[row.templateKind ?? ""] };
 
 
 
2373
  };
2374
  const merged = new Map([...financeApplications, ...studioApplications, ...tenantApplications]
2375
  .map((row) => [row.id, asTile(row)]));
 
21
  // ---------------------------------------------------------------------------
22
 
23
  import { useEffect, useId, useRef, useState } from "react";
24
+ import { AppMark, appWearsBrand, hasTemplateMark } from "../apps/templateMarks";
25
  import type { DragEvent, MouseEvent, ReactNode } from "react";
26
  import { useRailSelection } from "../ui/useRailSelection";
27
  import { IconTile } from "../ui/IconTile";
 
322
  type: string;
323
  templateKind?: string;
324
  templateLabel?: string;
325
+ /** The C1 connector of a templatic App. Rows that omit it take the template catalogue's. */
326
+ templateConnector?: string;
327
  deepLink?: string;
328
  compatibleDatabases?: string[];
329
  openTarget?: { database?: string | null };
 
502
  * One text body for every grid and list tile: the name, then the kind and (while searching) the
503
  * folder path. No opened or edited line: owner item 23 revoked R57's meta line.
504
  */
505
+ export function HomeTileText({ tile, fallback = "", index = null, searching = false, nameClass = "" }: {
506
  tile: HomeFindTile;
507
  fallback?: string;
508
  index?: ModuleFolderIndex | null;
509
  searching?: boolean;
510
+ /** A modifier added beside `home-tile-name`, for example the App name's two-line clamp. */
511
+ nameClass?: string;
512
  }) {
513
  const kind = fallback.trim();
514
  const folder = searching && kind ? homeFindFolderName(index, moduleTileId(tile)) : "";
515
  return <span className="home-tile-text">
516
+ <TruncatedName className={nameClass ? "home-tile-name " + nameClass : undefined}>{tile.name ?? tile.label ?? ""}</TruncatedName>
517
  {kind ? <span className="home-tile-ago">{[kind, folder].filter(Boolean).join(" · ")}</span>
518
  : searching ? <HomeFindFolder index={index} id={moduleTileId(tile)} /> : null}
519
  </span>;
 
1655
  const database = application.openTarget?.database
1656
  ?? application.compatibleDatabases?.[0]
1657
  ?? "";
 
1658
  return (
1659
  <ItemTileMenu key={application.id} id={application.id} kind="App" name={application.name}
1660
  canRename={application.capabilities?.edit === true}
 
1696
  }}
1697
  >
1698
  <IconTile surface="home" fill="tint">
1699
+ {hasTemplateMark(application.templateKind) || appWearsBrand(application.templateConnector)
1700
+ ? <AppMark mark={application.templateKind} connector={application.templateConnector} size={16} />
1701
+ : <ApplicationIcon />}
1702
  </IconTile>
1703
+ {/* An App's name is its whole identity: no template subtitle, up to two lines. */}
1704
  <HomeTileText tile={{ id: application.id, name: application.name }}
1705
+ nameClass="home-tile-name--app"
1706
  index={moduleIndex} searching={searching} />
1707
  <SharedMark mark={shareMarks.get(shareMarkKey("app", application.id))} fade={shareMarksFade} pending={shareMarksPending} />
1708
  </a>
 
2192
  }
2193
  };
2194
  const [tenantApplications, setApplications] = useState<ApplicationTile[]>([]);
2195
+ const [applicationTemplates, setApplicationTemplates] = useState<Record<string, { label: string; connector?: string }>>({});
2196
  const [applicationsKnown, setApplicationsKnown] = useState(false);
2197
  const [applicationsError, setApplicationsError] = useState("");
2198
  const [applicationBusy, setApplicationBusy] = useState<Set<string>>(() => new Set());
 
2237
  const options = { credentials: CREDENTIALS, signal: controller.signal };
2238
  void fetch(`${API_V1}/nav/application-templates`, options)
2239
  .then(async (response) => response.ok ? parseApplicationTemplates(await response.json()) : [])
2240
+ .then((templates) => Object.fromEntries(templates.map((template) => [template.kind, {
2241
+ label: template.label, connector: (template as { connector?: unknown }).connector,
2242
+ }]))).catch(() => ({}))
2243
+ .then((byKind) => {
2244
  if (!controller.signal.aborted && requestEpoch === applicationRequestEpoch.current) {
2245
+ setApplicationTemplates(byKind as Record<string, { label: string; connector?: string }>);
2246
  }
2247
  });
2248
  void fetch(`${API_V1}/applications`, options).then(readApplications)
 
2375
  // server wall use the Application id "finance_ar-collections". An App tile is its `id`, always.
2376
  const asTile = (row: ApplicationTile): ApplicationTile => {
2377
  const { key: _registryKey, ...tile } = row as ApplicationTile & { key?: unknown };
2378
+ const template = Object.prototype.hasOwnProperty.call(applicationTemplates, row.templateKind ?? "")
2379
+ ? applicationTemplates[row.templateKind ?? ""] : undefined;
2380
+ return { ...tile, templateLabel: row.templateLabel ?? template?.label,
2381
+ templateConnector: row.templateConnector ?? (typeof template?.connector === "string" ? template.connector : undefined) };
2382
  };
2383
  const merged = new Map([...financeApplications, ...studioApplications, ...tenantApplications]
2384
  .map((row) => [row.id, asTile(row)]));
web/src/home/TemplateGallery.tsx CHANGED
@@ -1,6 +1,6 @@
1
  import { useId, useState } from "react";
2
  import { CONNECTORS_ROUTE } from "../shell/nav";
3
- import { TemplateMark } from "../apps/templateMarks";
4
  import { IconTile } from "../ui/IconTile";
5
  import type { PickerTemplate, TemplateModule } from "../sheet/templates/templatesApi";
6
  import "../styles/wave59/templates.css";
@@ -28,6 +28,9 @@ const connectorLabels: Record<string, string> = {
28
  };
29
  const connectorLabel = (key: string) => (connectorLabels[key]
30
  ?? plain(key.replaceAll("_", " ").replace(/^./, (letter) => letter.toUpperCase()))) || "source";
 
 
 
31
 
32
  const ownedApps = (template: PickerTemplate): OwnedApp[] => {
33
  const existing: unknown = template.existing;
@@ -68,7 +71,8 @@ function ModuleGallery({ module, templates, loading, busy, onCreate, onNavigate,
68
  ? template.tags?.includes(category) : template.category === category);
69
 
70
  const templateTile = (template: PickerTemplate) => <IconTile surface="template">
71
- <TemplateMark mark={module === "app" ? template.mark ?? template.key : undefined} />
 
72
  </IconTile>;
73
  // The whole card is the create target. A card that cannot create renders no button body.
74
  const templateCard = (template: PickerTemplate) => {
@@ -87,8 +91,8 @@ function ModuleGallery({ module, templates, loading, busy, onCreate, onNavigate,
87
  </span>
88
  <span className="home-tpl-card-title">{name}</span>
89
  <span className="home-tpl-description" title={shortLine(template.description)}>{shortLine(template.description)}</span>
90
- {module === "app" ? <span className="home-tpl-category-label" aria-label="Tags">
91
- {template.tags?.map(plain).join(" · ")}
92
  </span> : null}
93
  </>;
94
  const footer = (!actionable && (!!reason || !!template.action)) || addError?.key === template.key;
 
1
  import { useId, useState } from "react";
2
  import { CONNECTORS_ROUTE } from "../shell/nav";
3
+ import { AppMark, appWearsBrand } from "../apps/templateMarks";
4
  import { IconTile } from "../ui/IconTile";
5
  import type { PickerTemplate, TemplateModule } from "../sheet/templates/templatesApi";
6
  import "../styles/wave59/templates.css";
 
28
  };
29
  const connectorLabel = (key: string) => (connectorLabels[key]
30
  ?? plain(key.replaceAll("_", " ").replace(/^./, (letter) => letter.toUpperCase()))) || "source";
31
+ // An Amazon App card's mark already names its source, so that source tag leaves the card's tag line.
32
+ const cardTags = (template: PickerTemplate) => (template.tags ?? []).filter((tag) =>
33
+ !appWearsBrand(template.connector) || tag !== connectorLabel(template.connector ?? ""));
34
 
35
  const ownedApps = (template: PickerTemplate): OwnedApp[] => {
36
  const existing: unknown = template.existing;
 
71
  ? template.tags?.includes(category) : template.category === category);
72
 
73
  const templateTile = (template: PickerTemplate) => <IconTile surface="template">
74
+ <AppMark mark={module === "app" ? template.mark ?? template.key : undefined}
75
+ connector={module === "app" ? template.connector : undefined} />
76
  </IconTile>;
77
  // The whole card is the create target. A card that cannot create renders no button body.
78
  const templateCard = (template: PickerTemplate) => {
 
91
  </span>
92
  <span className="home-tpl-card-title">{name}</span>
93
  <span className="home-tpl-description" title={shortLine(template.description)}>{shortLine(template.description)}</span>
94
+ {module === "app" && cardTags(template).length ? <span className="home-tpl-category-label" aria-label="Tags">
95
+ {cardTags(template).map(plain).join(" · ")}
96
  </span> : null}
97
  </>;
98
  const footer = (!actionable && (!!reason || !!template.action)) || addError?.key === template.key;
web/src/home/TruncatedName.tsx CHANGED
@@ -2,6 +2,10 @@ import { useEffect, useId, useRef, useState } from "react";
2
  import { AnchoredOverlay } from "../customer-grid/OverlaySurface";
3
  import "../styles/sdlc77/homes.css";
4
 
 
 
 
 
5
  /** Reveal an ellipsized name through its existing tile's hover and focus. */
6
  export function TruncatedName({ children, className = "home-tile-name" }: {
7
  children: string;
@@ -31,7 +35,7 @@ export function TruncatedName({ children, className = "home-tile-name" }: {
31
  const hide = () => { cancel(); setOpen(false); };
32
  const reveal = (delay: boolean) => {
33
  cancel();
34
- if (dismissed || name.scrollWidth <= name.clientWidth) { setOpen(false); return; }
35
  if (delay) timer = window.setTimeout(() => {
36
  timer = undefined;
37
  if (hovering && !dismissed) setOpen(true);
@@ -39,9 +43,9 @@ export function TruncatedName({ children, className = "home-tile-name" }: {
39
  else setOpen(true);
40
  };
41
  const measure = () => {
42
- const clipped = name.scrollWidth > name.clientWidth;
43
- setOverflow(clipped);
44
- if (!clipped) hide();
45
  else if (focused()) reveal(false);
46
  else if (hovering) reveal(true);
47
  };
 
2
  import { AnchoredOverlay } from "../customer-grid/OverlaySurface";
3
  import "../styles/sdlc77/homes.css";
4
 
5
+ // A one-line name clips sideways. A line-clamped name clips downward, past its last line.
6
+ const clipped = (name: HTMLElement) => name.scrollWidth > name.clientWidth
7
+ || name.scrollHeight - name.clientHeight > 1;
8
+
9
  /** Reveal an ellipsized name through its existing tile's hover and focus. */
10
  export function TruncatedName({ children, className = "home-tile-name" }: {
11
  children: string;
 
35
  const hide = () => { cancel(); setOpen(false); };
36
  const reveal = (delay: boolean) => {
37
  cancel();
38
+ if (dismissed || !clipped(name)) { setOpen(false); return; }
39
  if (delay) timer = window.setTimeout(() => {
40
  timer = undefined;
41
  if (hovering && !dismissed) setOpen(true);
 
43
  else setOpen(true);
44
  };
45
  const measure = () => {
46
+ const overflows = clipped(name);
47
+ setOverflow(overflows);
48
+ if (!overflows) hide();
49
  else if (focused()) reveal(false);
50
  else if (hovering) reveal(true);
51
  };
web/src/index.css CHANGED
@@ -12246,6 +12246,20 @@ a.cg-map-ctl-b { text-decoration: none; }
12246
  text-overflow: ellipsis;
12247
  white-space: nowrap;
12248
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12249
  .home-tile-ago {
12250
  font-size: var(--lp-fs-2xs);
12251
  color: var(--lp-muted);
 
12246
  text-overflow: ellipsis;
12247
  white-space: nowrap;
12248
  }
12249
+ /* An App name wraps to two lines inside the two-line text box, then ellipsizes; TruncatedName reveals the rest. */
12250
+ .home-tile-name.home-tile-name--app {
12251
+ display: -webkit-box;
12252
+ -webkit-box-orient: vertical;
12253
+ -webkit-line-clamp: 2;
12254
+ line-clamp: 2;
12255
+ white-space: normal;
12256
+ overflow-wrap: break-word;
12257
+ }
12258
+ /* A search location line takes the second line, so the App name keeps one. */
12259
+ .home-tile-text:has(> .home-tile-ago) > .home-tile-name.home-tile-name--app {
12260
+ -webkit-line-clamp: 1;
12261
+ line-clamp: 1;
12262
+ }
12263
  .home-tile-ago {
12264
  font-size: var(--lp-fs-2xs);
12265
  color: var(--lp-muted);
web/src/shell/Shell.tsx CHANGED
@@ -133,7 +133,7 @@ import { useEscapeClose } from "../ui/useEscapeClose";
133
  import { useApplicationWizardOverlay } from "./useApplicationWizardOverlay";
134
  import { BodyPortal } from "../customer-grid/OverlaySurface";
135
  import { OPEN_APP_TEMPLATE_EVENT } from "../sheet/templates/templatesApi";
136
- import { TemplateMark } from "../apps/templateMarks";
137
  import type { TemplateModule } from "../sheet/templates/templatesApi";
138
  /* ⭐ W35-T09 — the assistant's EXISTING door for opening an AI-built view: hash + a retry
139
  ladder, returning a canceller. A second emit here would be a second answer. */
@@ -698,8 +698,9 @@ function ApplicationWizard({
698
  onClick={() => chooseTemplate(candidate)}
699
  >
700
  <span className="app-wizard-template-icon">
701
- {/* C2: the kind's designed mark by its C1 `mark`; an unknown kind draws the default glyph. */}
702
- <TemplateMark mark={(candidate as { mark?: unknown }).mark ?? candidate.kind} />
 
703
  </span>
704
  <span><strong>{candidate.label}</strong></span>
705
  </button>
 
133
  import { useApplicationWizardOverlay } from "./useApplicationWizardOverlay";
134
  import { BodyPortal } from "../customer-grid/OverlaySurface";
135
  import { OPEN_APP_TEMPLATE_EVENT } from "../sheet/templates/templatesApi";
136
+ import { AppMark } from "../apps/templateMarks";
137
  import type { TemplateModule } from "../sheet/templates/templatesApi";
138
  /* ⭐ W35-T09 — the assistant's EXISTING door for opening an AI-built view: hash + a retry
139
  ladder, returning a canceller. A second emit here would be a second answer. */
 
698
  onClick={() => chooseTemplate(candidate)}
699
  >
700
  <span className="app-wizard-template-icon">
701
+ {/* C2: the kind's designed mark by its C1 `mark`, or the Amazon brand for an Amazon App. */}
702
+ <AppMark mark={(candidate as { mark?: unknown }).mark ?? candidate.kind}
703
+ connector={(candidate as { connector?: unknown }).connector} />
704
  </span>
705
  <span><strong>{candidate.label}</strong></span>
706
  </button>