fsanyoto commited on
Commit
30ec070
Β·
verified Β·
1 Parent(s): acf3b49

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "current": "f121a20",
3
  "releases": [
4
  {
5
  "version": "v75",
 
1
  {
2
+ "current": "eb6c2c7",
3
  "releases": [
4
  {
5
  "version": "v75",
VERSION CHANGED
@@ -1 +1 @@
1
- f121a20
 
1
+ eb6c2c7
api/main.py CHANGED
@@ -870,11 +870,13 @@ def _sync_now_detail(provider, tenant, *, ok, detail="", cause=None):
870
  return sentence + suffix
871
 
872
 
873
- def _run_sync_requests():
874
  """Drain Sync now intents on the resident resync thread, the mirror's single writer.
875
 
876
  D-29 / intake F6: HTTP handlers only queue intents. Provider calls stay here, before the
877
  resident projection pass. A failed request cannot discard another tenant's queued work.
 
 
878
  """
879
  from core import connector_sync
880
  odoo_requested = False
@@ -985,9 +987,11 @@ def _run_sync_requests():
985
  first_load=first_load_progress)
986
  else:
987
  connector_sync.finish_sync_request(provider, tenant, ok=ok, detail=detail)
 
 
988
  if continuing and ok:
989
- # Finish first: the desk refuses a second in-flight intent. Return to the resident
990
- # loop so its Odoo mirror pass runs before this continuation is claimed.
991
  connector_sync.request_sync("amazon_sp", tenant, requested_by="first load")
992
  return odoo_requested
993
 
@@ -1697,6 +1701,26 @@ def _classify_resync_error(exc):
1697
  "this failure usually clears on the next pass")}
1698
 
1699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1700
  def _store_resync_loop():
1701
  """Keep the analytical mirror current β€” the API-process stand-in for the re-sync the
1702
  Streamlit app piggybacks on page renders. Measure memos key on the pool stamp, so a
@@ -1712,6 +1736,10 @@ def _store_resync_loop():
1712
  # rebuild that was due -- one question with two answers, which is the defect class this
1713
  # codebase already names. Unknown is never carried forward as equality evidence.
1714
  last_amzn_fingerprint = None
 
 
 
 
1715
  while True:
1716
  # ⭐⭐ WAVE 32 Β· OWNER ITEM 11 / R11 β€” THE TENANT'S OWN CADENCE, NOT A HARDCODED 1800.
1717
  #
@@ -1780,12 +1808,41 @@ def _store_resync_loop():
1780
  # so staging served a provably stale customer population for its first hour. Subsequent
1781
  # passes still sleep at the freshly resolved cadence, and manual still skips only Odoo.
1782
  # The wait is now interruptible so an administrator's Sync now wakes this writer.
 
1783
  if passes:
1784
- connector_sync.sync_wake_event().wait(_every)
 
 
 
 
1785
  connector_sync.sync_wake_event().clear()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1786
  passes += 1
 
 
1787
  RESYNC_STATE["passes"] = passes
1788
- _odoo_requested = _run_sync_requests()
 
1789
  sync_report, delete_report, _err = _resync_mirror_pass(_manual, passes)
1790
  RESYNC_STATE["lastPassAt"] = _t.strftime("%Y-%m-%dT%H:%M:%SZ", _t.gmtime())
1791
  RESYNC_STATE["lastPassOk"] = _err is None
 
870
  return sentence + suffix
871
 
872
 
873
+ def _run_sync_requests(outcome=None):
874
  """Drain Sync now intents on the resident resync thread, the mirror's single writer.
875
 
876
  D-29 / intake F6: HTTP handlers only queue intents. Provider calls stay here, before the
877
  resident projection pass. A failed request cannot discard another tenant's queued work.
878
+ `outcome`, when given, receives `{provider: "continuing" | "done" | "failed"}` so the loop can
879
+ project only what a desk wake actually changed.
880
  """
881
  from core import connector_sync
882
  odoo_requested = False
 
987
  first_load=first_load_progress)
988
  else:
989
  connector_sync.finish_sync_request(provider, tenant, ok=ok, detail=detail)
990
+ if outcome is not None:
991
+ outcome[provider] = "continuing" if (continuing and ok) else ("done" if ok else "failed")
992
  if continuing and ok:
993
+ # Finish first: the desk refuses a second in-flight intent. The resident loop claims
994
+ # the continuation as a DESK-ONLY wake; the Odoo pass keeps its own cadence.
995
  connector_sync.request_sync("amazon_sp", tenant, requested_by="first load")
996
  return odoo_requested
997
 
 
1701
  "this failure usually clears on the next pass")}
1702
 
1703
 
1704
+ def _project_desk(outcome, tenant):
1705
+ """Project only what a desk-only wake FINISHED: Meta and Amazon, never the Odoo rebuild.
1706
+
1707
+ The desk has already pulled, so this is the relational half alone. A continuing first load
1708
+ projects nothing until it finishes; the scheduled pass still projects at its cadence.
1709
+ """
1710
+ finished = [provider for provider, state in (outcome or {}).items() if state == "done"]
1711
+ if not finished:
1712
+ return
1713
+ try:
1714
+ from harness import runtime as _runtime
1715
+ rt = _runtime.get_runtime(tenant or "royal-imports")
1716
+ if "meta_ads" in finished:
1717
+ _rebuild_meta_relational("sync now", rt)
1718
+ if "amazon_sp" in finished:
1719
+ _rebuild_amazon_relational("sync now", rt)
1720
+ except Exception as e: # noqa: BLE001
1721
+ print(f"[aios-api] sync now projection failed: {type(e).__name__}: {e}")
1722
+
1723
+
1724
  def _store_resync_loop():
1725
  """Keep the analytical mirror current β€” the API-process stand-in for the re-sync the
1726
  Streamlit app piggybacks on page renders. Measure memos key on the pool stamp, so a
 
1736
  # rebuild that was due -- one question with two answers, which is the defect class this
1737
  # codebase already names. Unknown is never carried forward as equality evidence.
1738
  last_amzn_fingerprint = None
1739
+ last_full_pass_at = _t.time()
1740
+ last_pass_was_full = True
1741
+ # The least time between two desk-only passes of an Amazon first-load continuation.
1742
+ desk_min_spacing_seconds = 30.0
1743
  while True:
1744
  # ⭐⭐ WAVE 32 Β· OWNER ITEM 11 / R11 β€” THE TENANT'S OWN CADENCE, NOT A HARDCODED 1800.
1745
  #
 
1808
  # so staging served a provably stale customer population for its first hour. Subsequent
1809
  # passes still sleep at the freshly resolved cadence, and manual still skips only Odoo.
1810
  # The wait is now interruptible so an administrator's Sync now wakes this writer.
1811
+ _woken = False
1812
  if passes:
1813
+ # After a full pass the wait is the whole cadence; after a desk-only pass it is what
1814
+ # remains of it, so continuations can never postpone the scheduled pass.
1815
+ _woken = bool(connector_sync.sync_wake_event().wait(
1816
+ _every if last_pass_was_full
1817
+ else max(0.0, _every - (_t.time() - last_full_pass_at))))
1818
  connector_sync.sync_wake_event().clear()
1819
+ # β›”β›” HOTFIX 2026-09-24 β€” A DESK WAKE IS NOT A SCHEDULED PASS.
1820
+ # An Amazon first load requeues itself after every bounded chunk, and each requeue SETS the
1821
+ # wake event. This loop used to answer every wake with the whole scheduled pass: an Odoo
1822
+ # mirror sync, the Odoo relational rebuild (read + write of the whole `user_tables`
1823
+ # document), Meta and Amazon. MEASURED on live 2026-09-24: a full rebuild every ~17
1824
+ # minutes for the 3+ hours of a first load, which restarts on every deploy, against a
1825
+ # 4-hour cadence. So a wake before the cadence is due drains the desk and projects ONLY
1826
+ # what the desk finished; an Odoo Sync now still gets the full pass it asked for.
1827
+ if _woken and _t.time() - last_full_pass_at < _every:
1828
+ _desk = {}
1829
+ _desk_started = _t.time()
1830
+ if not _run_sync_requests(_desk):
1831
+ _project_desk(_desk, _cad["tenant"])
1832
+ if "continuing" in _desk.values():
1833
+ # A floor between continuations, so a chunk that returns at once cannot spin.
1834
+ _t.sleep(max(0.0, desk_min_spacing_seconds - (_t.time() - _desk_started)))
1835
+ last_pass_was_full = False
1836
+ continue
1837
+ _odoo_requested = True
1838
+ else:
1839
+ _odoo_requested = None
1840
  passes += 1
1841
+ last_full_pass_at = _t.time()
1842
+ last_pass_was_full = True
1843
  RESYNC_STATE["passes"] = passes
1844
+ if _odoo_requested is None:
1845
+ _odoo_requested = _run_sync_requests()
1846
  sync_report, delete_report, _err = _resync_mirror_pass(_manual, passes)
1847
  RESYNC_STATE["lastPassAt"] = _t.strftime("%Y-%m-%dT%H:%M:%SZ", _t.gmtime())
1848
  RESYNC_STATE["lastPassOk"] = _err is None
api/odoo_relational.py CHANGED
@@ -4120,6 +4120,8 @@ def _publish_discovered_model(rt, client, choice, catalogue_row, *, username, to
4120
  pages_since_publish += 1
4121
  if (not written or final or pages_since_publish >= MODEL_PUBLISH_PAGES
4122
  or _time.monotonic() - last_publish >= MODEL_PUBLISH_SECONDS):
 
 
4123
  def apply(current):
4124
  current = current if isinstance(current, dict) else {}
4125
  written[key] = _ensure_table_inplace(
@@ -4129,10 +4131,17 @@ def _publish_discovered_model(rt, client, choice, catalogue_row, *, username, to
4129
  **connector_sync.database_identity(
4130
  "odoo", connection_for_table_key(key, rt), key, model),
4131
  "key": "odoo", "readOnly": True}
 
4132
  return current
4133
 
4134
  # The caller supplies its fenced runtime. A superseded write must propagate.
4135
  rt.update(ut.STORE_KEY, apply, flush="sync")
 
 
 
 
 
 
4136
  ut.register_connected(key)
4137
  published_rows = written[key]["rows"]
4138
  pages_since_publish, last_publish = 0, _time.monotonic()
@@ -4170,6 +4179,21 @@ def sync_discovered_models(rt, source_connection_id=None, namespace=None,
4170
  return {}
4171
  ut = _ut()
4172
  existing = dict(rt.get(ut.STORE_KEY) or {})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4173
  keys = [str(choice["key"]) for choice in selected]
4174
  missing = [key for key in keys if key not in existing]
4175
  if missing and len(existing) + len(missing) > ut.MAX_TABLES:
 
4120
  pages_since_publish += 1
4121
  if (not written or final or pages_since_publish >= MODEL_PUBLISH_PAGES
4122
  or _time.monotonic() - last_publish >= MODEL_PUBLISH_SECONDS):
4123
+ held = {}
4124
+
4125
  def apply(current):
4126
  current = current if isinstance(current, dict) else {}
4127
  written[key] = _ensure_table_inplace(
 
4131
  **connector_sync.database_identity(
4132
  "odoo", connection_for_table_key(key, rt), key, model),
4133
  "key": "odoo", "readOnly": True}
4134
+ held["doc"] = current
4135
  return current
4136
 
4137
  # The caller supplies its fenced runtime. A superseded write must propagate.
4138
  rt.update(ut.STORE_KEY, apply, flush="sync")
4139
+ # β›” HOTFIX 2026-09-24: the store door stamps a prefix that crossed the byte budget
4140
+ # and stores none of its rows. Stop reading Odoo here rather than pull the rest of
4141
+ # the model only to have every further publish discarded.
4142
+ if ut.oversize_of(key, defn=(held.get("doc") or {}).get(key) or {}):
4143
+ raise Refused(str((ut.limit_report(key, st=rt) or {}).get("cause")
4144
+ or "this database is too large to store"))
4145
  ut.register_connected(key)
4146
  published_rows = written[key]["rows"]
4147
  pages_since_publish, last_publish = 0, _time.monotonic()
 
4179
  return {}
4180
  ut = _ut()
4181
  existing = dict(rt.get(ut.STORE_KEY) or {})
4182
+ # β›” HOTFIX 2026-09-24: a model the store's byte budget already refused is NOT pulled again.
4183
+ # Without this, every resync re-read all of it from Odoo (account.invoice.line.agent: 250,000
4184
+ # rows) only for the store door to discard the rows. The refusal and its fix are on the wire
4185
+ # through `limit_report`; disabling and re-enabling does not bypass it.
4186
+ oversized = [choice for choice in selected
4187
+ if ut.oversize_of(choice["key"], defn=existing.get(str(choice["key"])) or {})]
4188
+ if oversized:
4189
+ if requested is not None and on_progress is not None:
4190
+ raise Refused(str((ut.limit_report(oversized[0]["key"], st=rt) or {}).get("cause")
4191
+ or "this database is too large to store"))
4192
+ selected = [choice for choice in selected if choice not in oversized]
4193
+ if not selected:
4194
+ return {"tables": {}, "errors": [
4195
+ f"{choice['model']}: too large to store, served nowhere until read-through"
4196
+ for choice in oversized]}
4197
  keys = [str(choice["key"]) for choice in selected]
4198
  missing = [key for key in keys if key not in existing]
4199
  if missing and len(existing) + len(missing) > ut.MAX_TABLES:
platform/core/store_pg.py CHANGED
@@ -422,6 +422,58 @@ def exists(name, tenant_slug=None):
422
  return row is not None
423
 
424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
  def put(name, data, tenant_slug=None):
426
  # β›” BEFORE `_pool()`, NOT AFTER. A refusal that fires once a connection is open has already
427
  # told the database this deployment is here, and on a pooled driver it has already borrowed a
@@ -430,11 +482,12 @@ def put(name, data, tenant_slug=None):
430
  # which is what makes the negative control in `verify_store_pg.py` bite without a server.
431
  check_write(tenant_slug, f'put {name!r}')
432
  with _pool().connection() as con:
 
433
  con.execute(
434
  _q('INSERT INTO {tbl} (key, value) VALUES (%s, %s::jsonb) '
435
  'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, '
436
  'updated_at = now(), rev = {bare}.rev + 1', 'store_kv', tenant_slug),
437
- (str(name), json.dumps(data)))
438
  return data
439
 
440
 
@@ -494,11 +547,12 @@ def update(name, fn, flush='sync', tenant_slug=None, expect_rev=None, audit_row=
494
  data = dict(row[0]) if row and isinstance(row[0], dict) else (row[0] if row else {})
495
  result = fn(data)
496
  data = result if result is not None else data
 
497
  con.execute(
498
  _q('INSERT INTO {tbl} (key, value) VALUES (%s, %s::jsonb) '
499
  'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, '
500
  'updated_at = now(), rev = {bare}.rev + 1', 'store_kv', tenant_slug),
501
- (str(name), json.dumps(data)))
502
  # ⚠ A LIST, OR ONE DICT, AND THE LIST IS NOT A CONVENIENCE. Moving views between
503
  # folders is ONE workspace write that changes several views' placements, so the
504
  # trail for it is several rows or it is wrong; a single-row keyword would have
 
422
  return row is not None
423
 
424
 
425
+ #: β›”β›” HOTFIX 2026-09-24 β€” THE DOCUMENT CEILING, AT THE ONE DOOR EVERY WRITE CROSSES.
426
+ #:
427
+ #: Every whole read of a document is billed Neon egress, so a document's size multiplies every
428
+ #: reader. Tenant #0's `user_tables` reached 261.7 MB of JSON text with nothing refusing it; the
429
+ #: bill passed 1,100 GB against a 500 GB allowance, and at Postgres's 256 MB jsonb limit the Odoo
430
+ #: rebuild failed on every pass. So no document may GROW past this ceiling, whichever caller
431
+ #: writes it. `user_tables` first sheds connector-fed rows over its own budget
432
+ #: (`core.user_tables.enforce_byte_budget`), so a connector can never trip this; typed data that
433
+ #: would is refused with a named error rather than written. A write that SHRINKS an oversized
434
+ #: document is always allowed, so this can never lock a tenant out of repairing one.
435
+ DOCUMENT_CEILING_BYTES = 128 * 1024 * 1024
436
+
437
+
438
+ class StoreDocumentTooLarge(RuntimeError):
439
+ """A write refused because the document would grow past `DOCUMENT_CEILING_BYTES`."""
440
+
441
+ def __init__(self, name, size, ceiling=DOCUMENT_CEILING_BYTES):
442
+ self.name, self.size, self.ceiling = name, size, ceiling
443
+ super().__init__(
444
+ f'{name!r} would be {size / 1048576:.1f} MB, over the {ceiling // 1048576} MB '
445
+ f'store ceiling, so the write was refused')
446
+
447
+
448
+ def _payload(name, data, con=None, tenant_slug=None):
449
+ """Serialize `data` for `name` through the byte budget and the ceiling. Raises when refused."""
450
+ payload = json.dumps(data)
451
+ if str(name) == 'user_tables' and isinstance(data, dict):
452
+ from core import user_tables as _ut
453
+ # Under both budgets in TOTAL, no table can be over either, so the measurement runs only
454
+ # when it can find something (or a stamp may need clearing).
455
+ if len(payload) > min(_ut.MAX_TABLE_BYTES, _ut.MAX_DOCUMENT_BYTES) or any(
456
+ isinstance(t, dict) and t.get(_ut.OVERSIZE_FIELD) and t.get('rows')
457
+ for t in data.values()):
458
+ report = _ut.enforce_byte_budget(data)
459
+ if report['evicted'] or report['cleared']:
460
+ payload = json.dumps(data)
461
+ if report['evicted']:
462
+ print('[store_pg] user_tables byte budget: stored no rows for '
463
+ + ', '.join(f'{k} ({v / 1048576:.1f} MB)'
464
+ for k, v in sorted(report['evicted'].items())))
465
+ if len(payload) > DOCUMENT_CEILING_BYTES:
466
+ before = None
467
+ if con is not None:
468
+ row = con.execute(
469
+ _q('SELECT octet_length(value::text) FROM {tbl} WHERE key = %s',
470
+ 'store_kv', tenant_slug), (str(name),)).fetchone()
471
+ before = int(row[0]) if row and row[0] is not None else 0
472
+ if before is None or len(payload) > before:
473
+ raise StoreDocumentTooLarge(str(name), len(payload))
474
+ return payload
475
+
476
+
477
  def put(name, data, tenant_slug=None):
478
  # β›” BEFORE `_pool()`, NOT AFTER. A refusal that fires once a connection is open has already
479
  # told the database this deployment is here, and on a pooled driver it has already borrowed a
 
482
  # which is what makes the negative control in `verify_store_pg.py` bite without a server.
483
  check_write(tenant_slug, f'put {name!r}')
484
  with _pool().connection() as con:
485
+ payload = _payload(name, data, con, tenant_slug)
486
  con.execute(
487
  _q('INSERT INTO {tbl} (key, value) VALUES (%s, %s::jsonb) '
488
  'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, '
489
  'updated_at = now(), rev = {bare}.rev + 1', 'store_kv', tenant_slug),
490
+ (str(name), payload))
491
  return data
492
 
493
 
 
547
  data = dict(row[0]) if row and isinstance(row[0], dict) else (row[0] if row else {})
548
  result = fn(data)
549
  data = result if result is not None else data
550
+ payload = _payload(name, data, con, tenant_slug)
551
  con.execute(
552
  _q('INSERT INTO {tbl} (key, value) VALUES (%s, %s::jsonb) '
553
  'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, '
554
  'updated_at = now(), rev = {bare}.rev + 1', 'store_kv', tenant_slug),
555
+ (str(name), payload))
556
  # ⚠ A LIST, OR ONE DICT, AND THE LIST IS NOT A CONVENIENCE. Moving views between
557
  # folders is ONE workspace write that changes several views' placements, so the
558
  # trail for it is several rows or it is wrong; a single-row keyword would have
platform/core/user_tables.py CHANGED
@@ -133,6 +133,34 @@ HISTORY_EXTENDED_DAYS = 90
133
  #: read-through rollup, which never copies a row.
134
  MAX_ROWS = 60_000
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  # ─────────────────────────────────────────────────────────────────────────────────────────────
137
  # ⭐⭐ WAVE 30 / OWNER RULING R6 β€” THE CEILING ABOVE DOES NOT APPLY TO A CONNECTED SOURCE.
138
  #
@@ -358,6 +386,80 @@ def strip_materialised(st=None):
358
  return out
359
 
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  def row_limit(table_key, st=None):
362
  """The row ceiling that applies to this database: 0, None, or `MAX_ROWS`.
363
 
@@ -384,6 +486,21 @@ def limit_report(table_key, st=None):
384
  understates every total it feeds while looking exactly like a complete one, which is the
385
  failure this whole module is arranged against.
386
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  cap = row_limit(table_key, st)
388
  if cap is None:
389
  return None
 
133
  #: read-through rollup, which never copies a row.
134
  MAX_ROWS = 60_000
135
 
136
+ # ─────────────────────────────────────────────────────────────────────────────────────────────
137
+ # β›”β›” HOTFIX 2026-09-24 β€” THE BYTE BUDGET, AND WHY A ROW COUNT WAS NEVER ENOUGH.
138
+ #
139
+ # R6 exempts a CONNECTED table from `MAX_ROWS`, and "connected" was meant to mean "served
140
+ # read-through". A connected table whose read-through does not exist yet was therefore stored
141
+ # HERE with no ceiling at all. MEASURED 2026-09-24 on tenant #0: one enabled Odoo model
142
+ # (`account.invoice.line.agent`, 250,000 rows) was 167.6 MB of this document, which reached
143
+ # 261.7 MB of JSON text. Every whole read of it is billed Neon egress (1,108 GB against a 500 GB
144
+ # allowance), and past Postgres's 256 MB jsonb limit the Odoo rebuild failed on EVERY pass with
145
+ # ProgramLimitExceeded. Staging held the same shape (`sale.order.line.agent`, 69.5 MB).
146
+ #
147
+ # ⭐ SO THE LIMIT IS ON BYTES, ENFORCED AT THE STORE'S WRITE DOOR (`core.store_pg`), where every
148
+ # writer of this document has to pass. A materialiser may check early to save its pull, but the
149
+ # door is the guarantee: no caller can write past it, whatever it forgot.
150
+ # * `MAX_TABLE_BYTES`: the rows of ONE connector-fed table. Over it, the rows are NOT stored, the
151
+ # definition stays, and the table carries a stamp that `limit_report` serves on the wire with
152
+ # its cause and fix (R6's second sentence). Connector rows are a copy of the source, so nothing
153
+ # is lost that the next read-through cannot serve.
154
+ # * `MAX_DOCUMENT_BYTES`: the whole document. Past it, the largest connector-fed tables are
155
+ # evicted the same way; typed data is NEVER evicted, and a write that would still exceed the
156
+ # store's ceiling is refused by `store_pg` instead.
157
+ # ⚠ 48 MB is not tuned to pass today's tables: the largest admitted one is Amazon order items at
158
+ # 32.2 MB (50% headroom) and the smallest offender is 69.5 MB.
159
+ MAX_TABLE_BYTES = 48 * 1024 * 1024
160
+ MAX_DOCUMENT_BYTES = 128 * 1024 * 1024
161
+ #: The definition stamp an eviction leaves. Its presence is what `limit_report` serves.
162
+ OVERSIZE_FIELD = 'oversize'
163
+
164
  # ─────────────────────────────────────────────────────────────────────────────────────────────
165
  # ⭐⭐ WAVE 30 / OWNER RULING R6 β€” THE CEILING ABOVE DOES NOT APPLY TO A CONNECTED SOURCE.
166
  #
 
386
  return out
387
 
388
 
389
+ def rows_bytes(rows):
390
+ """Serialized size of a table's rows, measured the way the store writes them."""
391
+ try:
392
+ return len(_json.dumps(rows or {}, default=str))
393
+ except Exception: # noqa: BLE001
394
+ return 0
395
+
396
+
397
+ def _connector_fed(key, table):
398
+ """Rows that are a COPY of a connected source, so evicting them loses nothing.
399
+
400
+ β›” An automation-authored table is NOT enough: an automation can append history no source
401
+ can re-derive. Only a connector identity, a registration, or a stored `connected` flag count.
402
+ """
403
+ return (key in _CONNECTED or table.get('connected') is True
404
+ or isinstance(table.get('connector'), dict))
405
+
406
+
407
+ def enforce_byte_budget(doc, now=None):
408
+ """Evict connector-fed rows over the byte budget from a `user_tables` document, in place.
409
+
410
+ Idempotent, and called by `core.store_pg` on every write of this document. Returns
411
+ `{'evicted': {key: bytes}, 'cleared': [key, ...]}`.
412
+ """
413
+ out = {'evicted': {}, 'cleared': []}
414
+ if not isinstance(doc, dict):
415
+ return out
416
+ stamp_at = now or _dt.datetime.now(_dt.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
417
+ sizes = {}
418
+ for key, table in doc.items():
419
+ if not isinstance(table, dict):
420
+ continue
421
+ rows = table.get('rows')
422
+ if not rows:
423
+ continue
424
+ sizes[key] = rows_bytes(rows)
425
+
426
+ def _evict(key, nbytes):
427
+ table = doc[key]
428
+ count = len(table.get('rows') or {})
429
+ table['rows'] = {}
430
+ table[OVERSIZE_FIELD] = {'bytes': nbytes, 'rows': count, 'limit': MAX_TABLE_BYTES,
431
+ 'at': stamp_at}
432
+ out['evicted'][key] = nbytes
433
+ sizes.pop(key, None)
434
+
435
+ for key, nbytes in list(sizes.items()):
436
+ table = doc[key]
437
+ if nbytes > MAX_TABLE_BYTES and _connector_fed(key, table):
438
+ _evict(key, nbytes)
439
+ elif table.get(OVERSIZE_FIELD):
440
+ # Rows came back within budget, so the refusal no longer applies.
441
+ table.pop(OVERSIZE_FIELD, None)
442
+ out['cleared'].append(key)
443
+ # The whole document: shed the largest connector-fed tables until it fits.
444
+ for key in sorted(sizes, key=lambda k: -sizes[k]):
445
+ if sum(sizes.values()) <= MAX_DOCUMENT_BYTES:
446
+ break
447
+ if _connector_fed(key, doc[key]):
448
+ _evict(key, sizes[key])
449
+ return out
450
+
451
+
452
+ def oversize_of(table_key, st=None, defn=None):
453
+ """The eviction stamp on this database, or None. Reads the rows-free projection."""
454
+ if defn is None:
455
+ try:
456
+ defn = all_defs(st).get(str(table_key or '').strip()) or {}
457
+ except Exception: # noqa: BLE001
458
+ defn = {}
459
+ stamp = (defn or {}).get(OVERSIZE_FIELD)
460
+ return stamp if isinstance(stamp, dict) else None
461
+
462
+
463
  def row_limit(table_key, st=None):
464
  """The row ceiling that applies to this database: 0, None, or `MAX_ROWS`.
465
 
 
486
  understates every total it feeds while looking exactly like a complete one, which is the
487
  failure this whole module is arranged against.
488
  """
489
+ oversize = oversize_of(table_key, st)
490
+ if oversize:
491
+ mb = round(int(oversize.get('bytes') or 0) / (1024 * 1024), 1)
492
+ limit_mb = int(int(oversize.get('limit') or MAX_TABLE_BYTES) / (1024 * 1024))
493
+ return {
494
+ 'subject': 'bytes', 'limit': int(oversize.get('limit') or MAX_TABLE_BYTES),
495
+ 'effect': 'refused', 'measured': int(oversize.get('bytes') or 0),
496
+ 'cause': f'this database measured {mb} MB of rows ({int(oversize.get("rows") or 0):,} '
497
+ f'rows), over the {limit_mb} MB a connected database may keep in the shared '
498
+ f'`{STORE_KEY}` document. Every read of that document is billed database '
499
+ f'transfer, so its rows were not stored. Nothing was truncated',
500
+ 'recommendation': 'serve it read-through from the connector mirror (booked for '
501
+ 'SDLC 66), or disable it until then. The definition, fields and '
502
+ 'grants are kept',
503
+ }
504
  cap = row_limit(table_key, st)
505
  if cap is None:
506
  return None
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 58391bc0a313931ed8cc8e5dcc382d009fe84feb9b3bdf70ff1edf18eb3b4de8
 
1
+ d2c560fea1e64af64d28d69dceecf2bca31a827d50ebe33b22467e1d5298043e