Spaces:
Running

fsanyoto commited on
Commit
4d2d061
·
verified ·
1 Parent(s): 2dd7fc3

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "current": "a07ac23d9f",
3
  "releases": []
4
  }
 
1
  {
2
+ "current": "4941465266",
3
  "releases": []
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- a07ac23d9f
 
1
+ 4941465266
api/routes_connectors.py CHANGED
@@ -3192,6 +3192,11 @@ def amazon_hold_cause(row=None, held="", *, table=""):
3192
  cause = "Amazon is limiting requests. The next try is scheduled."
3193
  elif state == "pending" or (not state and held.startswith("pending")):
3194
  cause = "Amazon is still preparing this report."
 
 
 
 
 
3195
  elif state == "deferred" or (not row and (not held or held.startswith("not reached"))):
3196
  cause = "This pass ended before this table's turn. The next pass continues it."
3197
  elif state == "never_run" and _amazon_table_period(table) in ("week", "month"):
 
3192
  cause = "Amazon is limiting requests. The next try is scheduled."
3193
  elif state == "pending" or (not state and held.startswith("pending")):
3194
  cause = "Amazon is still preparing this report."
3195
+ elif state == "deferred" and row.get("lastRefusal"):
3196
+ # A table this pass did not ask keeps the cause of its last refusal, never a scheduling delay.
3197
+ last = str(row["lastRefusal"]).strip() # already plain: `refused_reports` projects it
3198
+ cause = (last if last.startswith(("Amazon refused", "Amazon does not offer", "Reconciliation"))
3199
+ else "Amazon refused the last request: " + last) + " The next try is scheduled."
3200
  elif state == "deferred" or (not row and (not held or held.startswith("not reached"))):
3201
  cause = "This pass ended before this table's turn. The next pass continues it."
3202
  elif state == "never_run" and _amazon_table_period(table) in ("week", "month"):
platform/harness/amazon_store.py CHANGED
@@ -532,8 +532,31 @@ def _scheduled_phase_due(table, state, now, earliest):
532
  return max(earliest, datetime.fromtimestamp(phase, timezone.utc))
533
 
534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535
  def _cadence_seconds(table, state=None):
536
- """Return the table's scheduled interval, using calendar months for monthly reports."""
537
  cadence = _client().CADENCE[table]
538
  if cadence["period"] == "week":
539
  if state:
@@ -544,10 +567,8 @@ def _cadence_seconds(table, state=None):
544
  stamp = _utc(state["updated_at"]) if state else None
545
  if stamp is None:
546
  return 30 * SCHEDULED_FLOOR_SECONDS
547
- year, month = stamp.year + (stamp.month == 12), stamp.month % 12 + 1
548
- due = stamp.replace(year=year, month=month, day=1,
549
- hour=0, minute=0, second=0, microsecond=0)
550
- return int((due - stamp).total_seconds())
551
  return SCHEDULED_FLOOR_SECONDS // cadence["perDay"]
552
 
553
 
@@ -588,7 +609,9 @@ def sync_plan(states, tables=None, *, now=None, slot_pass=False):
588
  recent = max(stamps.values()) if stamps else None
589
  ready = recent is None or (now - recent).total_seconds() >= REPORT_STAGGER_SECONDS
590
  oldest = datetime.min.replace(tzinfo=timezone.utc)
591
- selected = sorted(due, key=lambda table: (stamps.get(table, oldest), want.index(table))) if ready else []
 
 
592
  if not ready:
593
  for table in due:
594
  deferred[table] = ("table stagger: waiting for a later cadence tick; "
@@ -762,6 +785,10 @@ BACKFILL_FLOOR_PREFIX = "sync_floor_"
762
  RESYNC_PREFIX = "resync_"
763
  #: Queued resync windows one pass downloads at most: Amazon's createReport burst, less the pass's spend.
764
  RESYNC_REPORTS_PER_PASS = FIRST_LOAD_REPORTS_PER_PASS
 
 
 
 
765
  FIRST_LOAD_END_KEY = "sync_first_load_end"
766
  FIRST_LOAD_STATUS_KEY = "sync_first_load_status"
767
  FIRST_LOAD_DUE_KEY = "sync_first_load_next_due"
@@ -4100,7 +4127,12 @@ def _deferred_table(reason, refusal, *, current=False):
4100
  """Keep a seller-wide refusal visible when a scheduling hold prevents another request."""
4101
  if refusal and refusal.get("state") == "not_offered":
4102
  return {"state": "refused", "amazonStatus": "NOT_OFFERED", "reason": refusal["reason"]}
4103
- return {"state": "deferred", "reason": reason, "current": current}
 
 
 
 
 
4104
 
4105
 
4106
  class _AmazonMirrorStore(_mirrors.MirrorStore):
@@ -7319,7 +7351,13 @@ def sync_window(table, mark, *, now, since=None, backfill_days=None, intraday=Fa
7319
  if spec.get("monthly"):
7320
  start = start.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
7321
  end = end.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
7322
- cause += "; completed calendar months only"
 
 
 
 
 
 
7323
  if spec.get("weekly"):
7324
  end = _week_start(end)
7325
  if incremental or since is not None:
@@ -10987,13 +11025,18 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
10987
  and not spec.get("restatement_keys")):
10988
  # One due window per table tick respects the vendor floor. The watermark
10989
  # advances only to that window's end, so the following tick loses no history.
10990
- remaining_windows = len(margin_windows) - 1
10991
- margin_windows = [margin_windows[0]]
10992
- plan["end"] = margin_windows[0][1]
 
 
 
 
10993
  _record_state(con, entity_key("limit_" + table), "done", detail=json.dumps({
10994
  "reason": (f"{remaining_windows} history windows remain. Amazon allows this report at most "
10995
  f"once every {_readable_duration(spec['min_interval_s'])}. Keep the connection enabled; "
10996
- "sync resumes at its saved date. Use Sync now without a custom start date.")}))
 
10997
  elif table in GROUP_A_TABLES and table != "catalog_items":
10998
  saved_limit = _read_state(con, entity_key("limit_" + table)) or {}
10999
  reason = json.loads(saved_limit.get("detail") or "{}").get("reason", "")
@@ -11408,8 +11451,11 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
11408
  _save_throttled_resume(con, refused, e, sibling_plan, completed_windows,
11409
  first_load=first_load, since=since)
11410
  else:
11411
- _record_state(con, entity_key("sync_tick_" + refused),
11412
- "attempted" if problem["state"] == "pending" else "done")
 
 
 
11413
  except Exception: # noqa: BLE001
11414
  pass
11415
  _sync_event("amazon.sync", tenant_key, table,
 
532
  return max(earliest, datetime.fromtimestamp(phase, timezone.utc))
533
 
534
 
535
+ #: Amazon dates a month report in US Pacific time and publishes it some days after that month closes.
536
+ MONTH_REPORT_TIMEZONE = "America/Los_Angeles"
537
+ #: Days after the Pacific month close before a month report is asked; an earlier ask ends FATAL or empty.
538
+ MONTH_PUBLISH_LAG_DAYS = 3
539
+
540
+
541
+ def published_month_end(now):
542
+ """The UTC first of the month after the newest month Amazon has published as of `now`."""
543
+ from zoneinfo import ZoneInfo # noqa: PLC0415
544
+ local = _utc(now).astimezone(ZoneInfo(MONTH_REPORT_TIMEZONE)) - timedelta(days=MONTH_PUBLISH_LAG_DAYS)
545
+ return datetime(local.year, local.month, 1, tzinfo=timezone.utc)
546
+
547
+
548
+ def _next_month_published(stamp):
549
+ """The first instant after `stamp` at which a newer month report is published."""
550
+ from zoneinfo import ZoneInfo # noqa: PLC0415
551
+ current = published_month_end(stamp)
552
+ following = (current.replace(day=28) + timedelta(days=4)).replace(day=1)
553
+ local = datetime(following.year, following.month, following.day,
554
+ tzinfo=ZoneInfo(MONTH_REPORT_TIMEZONE)) + timedelta(days=MONTH_PUBLISH_LAG_DAYS)
555
+ return local.astimezone(timezone.utc)
556
+
557
+
558
  def _cadence_seconds(table, state=None):
559
+ """Return the table's scheduled interval, using published calendar months for monthly reports."""
560
  cadence = _client().CADENCE[table]
561
  if cadence["period"] == "week":
562
  if state:
 
567
  stamp = _utc(state["updated_at"]) if state else None
568
  if stamp is None:
569
  return 30 * SCHEDULED_FLOOR_SECONDS
570
+ # A month is due once Amazon publishes it, the same boundary `sync_window` asks up to.
571
+ return int((_next_month_published(stamp) - stamp).total_seconds())
 
 
572
  return SCHEDULED_FLOOR_SECONDS // cadence["perDay"]
573
 
574
 
 
609
  recent = max(stamps.values()) if stamps else None
610
  ready = recent is None or (now - recent).total_seconds() >= REPORT_STAGGER_SECONDS
611
  oldest = datetime.min.replace(tzinfo=timezone.utc)
612
+ # Freshness tables run first: a throttled tick is the newest stamp, so oldest-first starves it (SDLC 87 QI13).
613
+ selected = sorted(due, key=lambda table: (table not in FRESHNESS_FIRST_TABLES, stamps.get(table, oldest),
614
+ want.index(table))) if ready else []
615
  if not ready:
616
  for table in due:
617
  deferred[table] = ("table stagger: waiting for a later cadence tick; "
 
785
  RESYNC_PREFIX = "resync_"
786
  #: Queued resync windows one pass downloads at most: Amazon's createReport burst, less the pass's spend.
787
  RESYNC_REPORTS_PER_PASS = FIRST_LOAD_REPORTS_PER_PASS
788
+ #: Owed published months one scheduled pass asks for a month-cadence table, oldest first.
789
+ MONTH_CATCH_UP_WINDOWS = 3
790
+ #: Tables the business reads as current; a due pass asks them before every other report.
791
+ FRESHNESS_FIRST_TABLES = ("orders", "order_items", "inventory")
792
  FIRST_LOAD_END_KEY = "sync_first_load_end"
793
  FIRST_LOAD_STATUS_KEY = "sync_first_load_status"
794
  FIRST_LOAD_DUE_KEY = "sync_first_load_next_due"
 
4127
  """Keep a seller-wide refusal visible when a scheduling hold prevents another request."""
4128
  if refusal and refusal.get("state") == "not_offered":
4129
  return {"state": "refused", "amazonStatus": "NOT_OFFERED", "reason": refusal["reason"]}
4130
+ row = {"state": "deferred", "reason": reason, "current": current}
4131
+ if refusal and refusal.get("reason"):
4132
+ # The last refusal stays on the row, so the card names it instead of a scheduling delay (SDLC 87 QI12).
4133
+ row.update(lastRefusal=refusal["reason"], amazonStatus=str(refusal.get("amazonStatus") or ""),
4134
+ attempts=refusal.get("attempts", 0))
4135
+ return row
4136
 
4137
 
4138
  class _AmazonMirrorStore(_mirrors.MirrorStore):
 
7351
  if spec.get("monthly"):
7352
  start = start.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
7353
  end = end.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
7354
+ if (_client().CADENCE.get(table) or {}).get("period") == "month" and since is None:
7355
+ # A month-cadence table asks only months Amazon has published, never the month that just closed.
7356
+ end = min(end, published_month_end(now))
7357
+ start = min(start, end)
7358
+ cause += "; completed calendar months only, once Amazon publishes them"
7359
+ else:
7360
+ cause += "; completed calendar months only"
7361
  if spec.get("weekly"):
7362
  end = _week_start(end)
7363
  if incremental or since is not None:
 
11025
  and not spec.get("restatement_keys")):
11026
  # One due window per table tick respects the vendor floor. The watermark
11027
  # advances only to that window's end, so the following tick loses no history.
11028
+ # A month-cadence table takes up to MONTH_CATCH_UP_WINDOWS owed months, oldest first,
11029
+ # so a table that fell behind closes the gap instead of landing one month per month.
11030
+ keep = (MONTH_CATCH_UP_WINDOWS if spec.get("monthly")
11031
+ and cx.CADENCE[table]["period"] == "month" else 1)
11032
+ remaining_windows = max(0, len(margin_windows) - keep)
11033
+ margin_windows = margin_windows[:keep]
11034
+ plan["end"] = margin_windows[-1][1]
11035
  _record_state(con, entity_key("limit_" + table), "done", detail=json.dumps({
11036
  "reason": (f"{remaining_windows} history windows remain. Amazon allows this report at most "
11037
  f"once every {_readable_duration(spec['min_interval_s'])}. Keep the connection enabled; "
11038
+ "sync resumes at its saved date. Use Sync now without a custom start date.")}
11039
+ if remaining_windows else {}))
11040
  elif table in GROUP_A_TABLES and table != "catalog_items":
11041
  saved_limit = _read_state(con, entity_key("limit_" + table)) or {}
11042
  reason = json.loads(saved_limit.get("detail") or "{}").get("reason", "")
 
11451
  _save_throttled_resume(con, refused, e, sibling_plan, completed_windows,
11452
  first_load=first_load, since=since)
11453
  else:
11454
+ # A refused month-cadence table records an attempt, so it retries at the next daily
11455
+ # phase instead of waiting a month (SDLC 87 QI1: a refusal is never a success).
11456
+ retry = (problem["state"] == "pending"
11457
+ or cx.CADENCE.get(refused, {}).get("period") == "month")
11458
+ _record_state(con, entity_key("sync_tick_" + refused), "attempted" if retry else "done")
11459
  except Exception: # noqa: BLE001
11460
  pass
11461
  _sync_event("amazon.sync", tenant_key, table,
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 4bc1f35f98fcbb257813c7fcd2f18a91655cc792e6c64bd3e449daaf08a92f9a
 
1
+ fc490ba3cf6c8a5e488c922dda36a4736e58e3cd6f8f73807bbbf5153f90fb84