fsanyoto commited on
Commit
e1ea5a0
·
verified ·
1 Parent(s): 1e78849

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "current": "80c1f5737c7 (80c1f5737c7)",
3
  "releases": [
4
  {
5
  "version": "v77",
 
1
  {
2
+ "current": "f4086da0ff8 (f4086da0ff8)",
3
  "releases": [
4
  {
5
  "version": "v77",
VERSION CHANGED
@@ -1 +1 @@
1
- 80c1f5737c7 (80c1f5737c7)
 
1
+ f4086da0ff8 (f4086da0ff8)
api/amazon_relational.py CHANGED
@@ -470,11 +470,18 @@ def _read_through(cur, key, count):
470
  return count * size > min(ut.MAX_TABLE_BYTES, AMAZON_STORED_BYTES)
471
 
472
 
 
 
 
 
473
  def missing_source(key):
474
- return {"missing": (
475
- f"The Amazon mirror in this container has not loaded Amazon "
476
- f"{key.removeprefix(PREFIX).replace('_', ' ')} yet. "
477
- "The next Amazon sync loads it. Nothing is lost.")}
 
 
 
478
 
479
 
480
  def _text(value):
 
470
  return count * size > min(ut.MAX_TABLE_BYTES, AMAZON_STORED_BYTES)
471
 
472
 
473
+ #: Set by the boot's resync loop until its Amazon restore ends (`main._restore_amazon_mirror`).
474
+ RESTORE_PENDING = {"since": None}
475
+
476
+
477
  def missing_source(key):
478
+ """The ONE sentence every door gives for an Amazon table this server's mirror does not hold yet."""
479
+ label = key.removeprefix(PREFIX).replace("_", " ")
480
+ if RESTORE_PENDING.get("since") is not None:
481
+ return {"missing": f"Amazon {label} is still loading after a server restart, "
482
+ "so ask again in 5 minutes."}
483
+ return {"missing": f"Amazon {label} is still loading from Amazon, "
484
+ "so ask again after the next Amazon sync finishes."}
485
 
486
 
487
  def _text(value):
api/assistant_mirror.py CHANGED
@@ -93,6 +93,58 @@ def open_cursor(session, table_key=None):
93
  return cur, None
94
 
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  def binding(cur, table_key):
97
  """The resolved read-through spec for one connected grid, or None when it has no binding.
98
 
@@ -340,7 +392,7 @@ def read_rows(session, table_key, cur, spec, fields, *, wall_tree=None, filter_t
340
  # ends anywhere else (the mirror resynced mid-read) refuses by name rather than answering short.
341
  first = datastore.window(select=f'{spec["id"]} AS "_pid", {select}',
342
  where=where, params=tuple(params), order_by=spec["id"],
343
- limit=datastore.WINDOW_MAX, cur=cur, **frm)
344
  total = int(first["total"])
345
  if total > ASSISTANT_READ_MAX:
346
  raise MirrorRefusal(
@@ -358,7 +410,8 @@ def read_rows(session, table_key, cur, spec, fields, *, wall_tree=None, filter_t
358
  more_where = f"({where}) AND {spec['id']} > ?" if where else f"{spec['id']} > ?"
359
  nxt = datastore.window(select=f'{spec["id"]} AS "_pid", {select}',
360
  where=more_where, params=tuple(params) + (last,),
361
- order_by=spec["id"], limit=datastore.WINDOW_MAX, cur=cur, **frm)
 
362
  page = list(nxt["rows"])
363
  if not page:
364
  break
@@ -481,7 +534,7 @@ def read_page(session, table_key, cur, spec, fields, *, offset, limit, wall_tree
481
  frm = {"from_sql": spec["from_sql"]} if spec.get("from_sql") else {"table": spec["table"]}
482
  win = datastore.window(select=f'{spec["id"]} AS "_pid", {select}', where=where,
483
  params=tuple(params), order_by=spec["id"], offset=int(offset),
484
- limit=int(limit), cur=cur, **frm)
485
  stamp = rot.mirror_stamp(spec, cur, table_key, getattr(session, "runtime", None),
486
  defn=defn if isinstance(defn, dict) else None)
487
  rows = rot._pool_from_window(spec, win, stamp=stamp)
 
93
  return cur, None
94
 
95
 
96
+ #: The one sentence for a mirror-served table this deployment has no binding for at all.
97
+ NO_BINDING = ("this database is read live from its connector mirror, and this deployment has no "
98
+ "read-through binding for it yet, so it holds no rows the assistant can read")
99
+
100
+
101
+ def unavailable_reason(table_key, definition=None):
102
+ """The ONE sentence `list_databases` and `query_database` both give when the mirror holds no rows.
103
+
104
+ A bound table whose mirror tables are absent is loading (a restart starts from the boot seed);
105
+ a table with no binding at all gets `NO_BINDING`.
106
+ """
107
+ key = str(table_key or "")
108
+ try:
109
+ if key.startswith("ut_amzn_"):
110
+ import amazon_relational as amazon
111
+ if key in amazon.SOURCE:
112
+ return amazon.missing_source(key)["missing"]
113
+ spec = _rot()._sources().get(key)
114
+ if spec and not spec.get("missing"):
115
+ label = str((definition or {}).get("label") or key)
116
+ return (f"{label} is still loading into this server after a restart, "
117
+ "so ask again in an hour.")
118
+ except Exception: # noqa: BLE001
119
+ pass
120
+ return NO_BINDING
121
+
122
+
123
+ def still_loading(table_key):
124
+ """True while an Odoo table behind this binding is in its first backfill, so its rows are partial.
125
+
126
+ Evidence only: a `_sync_state` phase other than live, or an empty table with no state row yet
127
+ (created, first batch not written). A mirror with no sync bookkeeping is not loading.
128
+ """
129
+ from harness import datastore
130
+ try:
131
+ spec = _rot()._sources().get(str(table_key or "")) or {}
132
+ names = set((spec.get("tables") or {}).values()) | ({spec["table"]} if spec.get("table") else set())
133
+ cur = datastore.ro_cursor()
134
+ try:
135
+ for name in sorted(n for n in names if n in datastore.ENTITIES):
136
+ row = cur.execute("SELECT phase FROM _sync_state WHERE entity = ?", [name]).fetchone()
137
+ if row is not None and row[0] != "live":
138
+ return True
139
+ if row is None and not cur.execute(f"SELECT count(*) FROM (SELECT 1 FROM {name} LIMIT 1)").fetchone()[0]:
140
+ return True
141
+ finally:
142
+ cur.close()
143
+ except Exception: # noqa: BLE001
144
+ return False
145
+ return False
146
+
147
+
148
  def binding(cur, table_key):
149
  """The resolved read-through spec for one connected grid, or None when it has no binding.
150
 
 
392
  # ends anywhere else (the mirror resynced mid-read) refuses by name rather than answering short.
393
  first = datastore.window(select=f'{spec["id"]} AS "_pid", {select}',
394
  where=where, params=tuple(params), order_by=spec["id"],
395
+ limit=datastore.WINDOW_MAX, cur=cur, page_first=True, **frm)
396
  total = int(first["total"])
397
  if total > ASSISTANT_READ_MAX:
398
  raise MirrorRefusal(
 
410
  more_where = f"({where}) AND {spec['id']} > ?" if where else f"{spec['id']} > ?"
411
  nxt = datastore.window(select=f'{spec["id"]} AS "_pid", {select}',
412
  where=more_where, params=tuple(params) + (last,),
413
+ order_by=spec["id"], limit=datastore.WINDOW_MAX, cur=cur,
414
+ page_first=True, **frm)
415
  page = list(nxt["rows"])
416
  if not page:
417
  break
 
534
  frm = {"from_sql": spec["from_sql"]} if spec.get("from_sql") else {"table": spec["table"]}
535
  win = datastore.window(select=f'{spec["id"]} AS "_pid", {select}', where=where,
536
  params=tuple(params), order_by=spec["id"], offset=int(offset),
537
+ limit=int(limit), cur=cur, page_first=True, **frm)
538
  stamp = rot.mirror_stamp(spec, cur, table_key, getattr(session, "runtime", None),
539
  defn=defn if isinstance(defn, dict) else None)
540
  rows = rot._pool_from_window(spec, win, stamp=stamp)
api/deps.py CHANGED
@@ -490,10 +490,9 @@ def _assistant_mirror_table(session: Session, database: str, requested_fields=No
490
  if refusal is not None:
491
  _assistant_refuse(refusal.status, refusal.code, refusal.message)
492
  spec = assistant_mirror.binding(cur, database)
493
- if spec is None:
494
  _assistant_refuse(409, "assistant_snapshot_unavailable",
495
- "this database is served live from its connector and this deployment "
496
- "has no read-through binding for it yet, so it holds no rows to read")
497
  fields = assistant_mirror.answerable_fields(database, spec, declared)
498
  if not fields:
499
  _assistant_refuse(409, "assistant_snapshot_unavailable",
@@ -1028,11 +1027,15 @@ def assistant_source_status(session: Session, databases=None):
1028
  import assistant_mirror
1029
  cur, refusal = assistant_mirror.open_cursor(session)
1030
  mirror_cur = None if refusal is not None else cur
 
 
1031
  except Exception: # noqa: BLE001
1032
  mirror_cur = None
1033
  return mirror_cur
1034
 
1035
  amazon_cur = {}
 
 
1036
 
1037
  def _amazon_mirror(key):
1038
  """ONE Amazon cursor for the batch, chosen by `_table_cursor` for an Amazon key."""
@@ -1041,6 +1044,8 @@ def assistant_source_status(session: Session, databases=None):
1041
  import assistant_mirror
1042
  cur, refusal = assistant_mirror.open_cursor(session, key)
1043
  amazon_cur["cur"] = None if refusal is not None else cur
 
 
1044
  except Exception: # noqa: BLE001
1045
  amazon_cur["cur"] = None
1046
  return amazon_cur["cur"]
@@ -1165,8 +1170,9 @@ def assistant_source_status(session: Session, databases=None):
1165
  # first-sync gate, so "not open right now" is never Odoo's backfill answering for Amazon.
1166
  cur = _amazon_mirror(key) if key.startswith("ut_amzn_") else _mirror()
1167
  if cur is None:
1168
- row["reason"] = ("this database is read live from its connector mirror, and that "
1169
- "mirror is not open on this deployment right now; try again shortly")
 
1170
  continue
1171
  # ⛔ `is_bound`, NOT `binding` - THE AVAILABILITY QUESTION, NOT THE READ ONE, and the
1172
  # difference is 300 ms PER KEY here. Resolving a full spec runs `excluded_ids`, a scan of
@@ -1177,12 +1183,11 @@ def assistant_source_status(session: Session, databases=None):
1177
  bound = assistant_mirror.is_bound(cur, key)
1178
  except Exception: # noqa: BLE001
1179
  bound = False
1180
- if bound:
 
1181
  row["answerable"] = True
1182
  else:
1183
- row["reason"] = ("this database is read live from its connector mirror, and this "
1184
- "deployment has no read-through binding for it yet, so it holds no "
1185
- "rows the assistant can read")
1186
  return out
1187
 
1188
 
 
490
  if refusal is not None:
491
  _assistant_refuse(refusal.status, refusal.code, refusal.message)
492
  spec = assistant_mirror.binding(cur, database)
493
+ if spec is None or assistant_mirror.still_loading(database):
494
  _assistant_refuse(409, "assistant_snapshot_unavailable",
495
+ assistant_mirror.unavailable_reason(database, definition))
 
496
  fields = assistant_mirror.answerable_fields(database, spec, declared)
497
  if not fields:
498
  _assistant_refuse(409, "assistant_snapshot_unavailable",
 
1027
  import assistant_mirror
1028
  cur, refusal = assistant_mirror.open_cursor(session)
1029
  mirror_cur = None if refusal is not None else cur
1030
+ if refusal is not None:
1031
+ closed_reason["odoo"] = refusal.message
1032
  except Exception: # noqa: BLE001
1033
  mirror_cur = None
1034
  return mirror_cur
1035
 
1036
  amazon_cur = {}
1037
+ #: The cursor refusal's own message, so this list and `query_database` say one sentence.
1038
+ closed_reason = {}
1039
 
1040
  def _amazon_mirror(key):
1041
  """ONE Amazon cursor for the batch, chosen by `_table_cursor` for an Amazon key."""
 
1044
  import assistant_mirror
1045
  cur, refusal = assistant_mirror.open_cursor(session, key)
1046
  amazon_cur["cur"] = None if refusal is not None else cur
1047
+ if refusal is not None:
1048
+ closed_reason["amazon"] = refusal.message
1049
  except Exception: # noqa: BLE001
1050
  amazon_cur["cur"] = None
1051
  return amazon_cur["cur"]
 
1170
  # first-sync gate, so "not open right now" is never Odoo's backfill answering for Amazon.
1171
  cur = _amazon_mirror(key) if key.startswith("ut_amzn_") else _mirror()
1172
  if cur is None:
1173
+ row["reason"] = (closed_reason.get("amazon" if key.startswith("ut_amzn_") else "odoo")
1174
+ or "this database is read live from its connector mirror, and that "
1175
+ "mirror is not open on this deployment right now; try again shortly")
1176
  continue
1177
  # ⛔ `is_bound`, NOT `binding` - THE AVAILABILITY QUESTION, NOT THE READ ONE, and the
1178
  # difference is 300 ms PER KEY here. Resolving a full spec runs `excluded_ids`, a scan of
 
1183
  bound = assistant_mirror.is_bound(cur, key)
1184
  except Exception: # noqa: BLE001
1185
  bound = False
1186
+ import assistant_mirror
1187
+ if bound and not assistant_mirror.still_loading(key):
1188
  row["answerable"] = True
1189
  else:
1190
+ row["reason"] = assistant_mirror.unavailable_reason(key, definition)
 
 
1191
  return out
1192
 
1193
 
api/main.py CHANGED
@@ -898,6 +898,40 @@ def _pull_amazon(why, vendor=True):
898
  return False
899
 
900
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
901
  class _SyncNowRefused(Exception):
902
  """An application-authored, credential-free sentence for a Sync now receipt."""
903
 
@@ -1926,7 +1960,21 @@ def _resync_mirror_pass(manual, passes, entities=None):
1926
  # relational rebuild, which `manual` says nothing about.
1927
  if manual:
1928
  return None, None, None
 
 
 
 
 
 
 
 
 
 
 
1929
  sync_report = _ds.sync_all(entities=entities, log=lambda *a, **k: None)
 
 
 
1930
  # Wave 21: purge hard-deleted rows the cursor sync cannot see (the boot pass's comment has
1931
  # the measured case); without it deleted Odoo lines inflate every sum on the mirror FOREVER.
1932
  # ⚠ INSIDE the non-manual branch: a reconcile is a pass over the mirror this loop was
@@ -1941,6 +1989,30 @@ def _resync_mirror_pass(manual, passes, entities=None):
1941
  return None, None, _classify_resync_error(e)
1942
 
1943
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1944
  def _classify_resync_error(exc):
1945
  """D-314, the half that fits inside this loop: a failed pass leaves a MARK, not just a line.
1946
 
@@ -2183,7 +2255,15 @@ def _store_resync_loop():
2183
  # restores Amazon's saved mirror. The store-touching half (Meta and Amazon vendor pulls,
2184
  # the relational rebuild) waits for the pass that is actually due.
2185
  if not passes:
 
 
 
 
 
2186
  _await_boot_seed()
 
 
 
2187
  _boot_catch_up = ((not passes or catch_up_retries)
2188
  and _resync_boot_recent(last_good_pass_at, _t.time(), _every))
2189
  # ⛔⛔ HOTFIX 2026-09-24 — A DESK WAKE IS NOT A SCHEDULED PASS.
 
898
  return False
899
 
900
 
901
+ def _restore_amazon_mirror(why):
902
+ """Restore Amazon's saved mirror and republish its registry: store reads only, no vendor call."""
903
+ try:
904
+ return _restore_amazon_mirror_once(why)
905
+ finally:
906
+ try:
907
+ import amazon_relational as _ar
908
+ _ar.RESTORE_PENDING["since"] = None
909
+ except Exception: # noqa: BLE001
910
+ pass
911
+
912
+
913
+ def _restore_amazon_mirror_once(why):
914
+ try:
915
+ import time as _t
916
+ from harness import amazon_store as _amazon
917
+ from harness import runtime as _runtime
918
+ import routes_odoo_tables as _rot
919
+ tenant_key = "royal-imports"
920
+ rt = _runtime.get_runtime(tenant_key)
921
+ if not _amazon.credential(tenant_key, rt=rt):
922
+ return False
923
+ started = _t.time()
924
+ result = _amazon.restore_mirror(tenant_key, rt)
925
+ _rot.invalidate_sources()
926
+ _rot._sources()
927
+ print(f"[aios-api] amazon mirror {result.get('state')} ({why}) in "
928
+ f"{_t.time() - started:.0f}s, before the Odoo catch-up")
929
+ return True
930
+ except Exception as exc: # noqa: BLE001
931
+ print(f"[aios-api] amazon mirror restore FAILED ({why}): {type(exc).__name__}: {exc}")
932
+ return False
933
+
934
+
935
  class _SyncNowRefused(Exception):
936
  """An application-authored, credential-free sentence for a Sync now receipt."""
937
 
 
1960
  # relational rebuild, which `manual` says nothing about.
1961
  if manual:
1962
  return None, None, None
1963
+ absent = _smallest_first(_ds, _absent_mirror_entities(_ds, entities))
1964
+ first = {}
1965
+ if absent:
1966
+ # Tables the boot seed lacks first: minutes, where the whole catch-up runs an hour.
1967
+ try:
1968
+ first = _ds.sync_all(entities=absent, log=lambda *a, **k: None)
1969
+ print(f"[aios-api] store resync: synced {', '.join(absent)} first, "
1970
+ f"absent from the boot seed")
1971
+ except Exception as exc: # noqa: BLE001
1972
+ print(f"[aios-api] store resync: the absent tables first failed "
1973
+ f"({type(exc).__name__}); the full pass below still syncs them")
1974
  sync_report = _ds.sync_all(entities=entities, log=lambda *a, **k: None)
1975
+ for key, row in first.items():
1976
+ if isinstance(sync_report, dict) and isinstance(sync_report.get(key), dict):
1977
+ sync_report[key]["pulled"] = sync_report[key].get("pulled", 0) + row.get("pulled", 0)
1978
  # Wave 21: purge hard-deleted rows the cursor sync cannot see (the boot pass's comment has
1979
  # the measured case); without it deleted Odoo lines inflate every sum on the mirror FOREVER.
1980
  # ⚠ INSIDE the non-manual branch: a reconcile is a pass over the mirror this loop was
 
1989
  return None, None, _classify_resync_error(e)
1990
 
1991
 
1992
+ def _absent_mirror_entities(ds, entities=None):
1993
+ """The mirror entities this pass syncs that have no table in the DuckDB file yet, in sync order."""
1994
+ try:
1995
+ cur = ds.connect().cursor()
1996
+ try:
1997
+ return [key for key in (list(ds.ENTITIES) if entities is None else list(entities))
1998
+ if key in ds.ENTITIES and not ds.columns_of(key, cur=cur)]
1999
+ finally:
2000
+ cur.close()
2001
+ except Exception: # noqa: BLE001
2002
+ return []
2003
+
2004
+
2005
+ def _smallest_first(ds, keys):
2006
+ """`keys` ordered by their Odoo row count, so a small table answers before a large backfill."""
2007
+ def size(key):
2008
+ try:
2009
+ spec = ds.ENTITIES[key]
2010
+ return int(ds.O.get_odoo().search_count(spec["model"], ds._base_domain(spec)))
2011
+ except Exception: # noqa: BLE001
2012
+ return float("inf")
2013
+ return sorted(keys, key=size)
2014
+
2015
+
2016
  def _classify_resync_error(exc):
2017
  """D-314, the half that fits inside this loop: a failed pass leaves a MARK, not just a line.
2018
 
 
2255
  # restores Amazon's saved mirror. The store-touching half (Meta and Amazon vendor pulls,
2256
  # the relational rebuild) waits for the pass that is actually due.
2257
  if not passes:
2258
+ try:
2259
+ import amazon_relational as _ar
2260
+ _ar.RESTORE_PENDING["since"] = _t.time()
2261
+ except Exception: # noqa: BLE001
2262
+ pass
2263
  _await_boot_seed()
2264
+ # Amazon's saved mirror first: minutes of store reads, where the Odoo catch-up below
2265
+ # runs about an hour after a restart and Amazon answered nothing until it ended.
2266
+ _restore_amazon_mirror("boot")
2267
  _boot_catch_up = ((not passes or catch_up_retries)
2268
  and _resync_boot_recent(last_good_pass_at, _t.time(), _every))
2269
  # ⛔⛔ HOTFIX 2026-09-24 — A DESK WAKE IS NOT A SCHEDULED PASS.
api/routes_odoo_tables.py CHANGED
@@ -3657,7 +3657,7 @@ def odoo_table_rows(table_key: str,
3657
  select = ", ".join(f'{expr} AS "{key}"' for key, (expr, _c) in spec["cols"].items())
3658
  win = datastore.window(select=f'{spec["id"]} AS "_pid", {select}',
3659
  where=where, params=tuple(params), order_by=order,
3660
- offset=offset, limit=(limit or None), cur=cur, **frm)
3661
  if win["clamped"]:
3662
  limits.append({
3663
  "subject": "limit", "effect": "window_clamped",
 
3657
  select = ", ".join(f'{expr} AS "{key}"' for key, (expr, _c) in spec["cols"].items())
3658
  win = datastore.window(select=f'{spec["id"]} AS "_pid", {select}',
3659
  where=where, params=tuple(params), order_by=order,
3660
+ offset=offset, limit=(limit or None), cur=cur, page_first=True, **frm)
3661
  if win["clamped"]:
3662
  limits.append({
3663
  "subject": "limit", "effect": "window_clamped",
api/test_odoo_rank_refresh.py CHANGED
@@ -72,6 +72,10 @@ def _with_collaborators(source, nodes, ns):
72
  return have
73
 
74
 
 
 
 
 
75
  class RankRefreshTests(unittest.TestCase):
76
  def setUp(self):
77
  self.stack = ExitStack()
@@ -195,7 +199,7 @@ class RankRefreshTests(unittest.TestCase):
195
  clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60,
196
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
197
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
198
- _configured_mirror_entities=lambda *_: None,
199
  _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False,
200
  tenant='synthetic', tenantSource='test', tenantSeconds=60,
201
  envSeconds=60, disagreement=None),
@@ -480,7 +484,7 @@ class RankRefreshTests(unittest.TestCase):
480
 
481
  # ── HOTFIX 2026-09-29 (owner): "only sync daily at the fastest", and only what changed ──────
482
  def run_daily_loop(self, memory, current, *, now=10_000.0, cadence=86400, manual=False,
483
- errors=(), max_sleeps=1):
484
  """Drive the real loop with a persisted memory; stop at its `max_sleeps`-th wait."""
485
  source = ast.parse(Path(__file__).with_name('main.py').read_text(encoding='utf-8'))
486
  functions = {'_store_resync_loop', '_resync_rebuild_reason',
@@ -518,6 +522,7 @@ class RankRefreshTests(unittest.TestCase):
518
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
519
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
520
  _configured_mirror_entities=lambda *_: None,
 
521
  _resolve_resync_cadence=lambda: dict(
522
  resolvedSeconds=cadence, manual=manual, tenant='synthetic',
523
  tenantSource='test', tenantSeconds=cadence, envSeconds=cadence,
@@ -538,13 +543,18 @@ class RankRefreshTests(unittest.TestCase):
538
  import harness
539
  from core import connector_sync
540
  wake = SimpleNamespace(wait=sleep, clear=lambda: None)
 
541
  with patch.dict(sys.modules, {'time': clock, 'harness.runtime': runtime}), \
542
  patch.object(harness, 'runtime', runtime, create=True), \
543
  patch.object(connector_sync, 'sync_wake_event', return_value=wake), \
544
  patch.object(connector_sync, 'take_sync_requests', return_value=[]), \
545
- redirect_stdout(io.StringIO()):
546
- with self.assertRaisesRegex(StopIteration, 'bounded synthetic'):
547
- ns['_store_resync_loop']()
 
 
 
 
548
  self.net.assert_not_called()
549
  return events, sleeps
550
 
@@ -591,6 +601,25 @@ class RankRefreshTests(unittest.TestCase):
591
  self.assertIn(('rebuild', None), events)
592
  self.assertNotIn('fresh', [e[0] for e in events])
593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
  def test_a_changed_entity_rebuilds_only_its_tables(self):
595
  events, _ = self.run_daily_loop(
596
  {'passAt': None, 'fullRebuildAt': 10_000.0 - 600,
 
72
  return have
73
 
74
 
75
+ #: W-6864: main.py's `_freeze_holds` reads this import, so every loop namespace stubs it (freeze off).
76
+ FREEZE_OFF = SimpleNamespace(enabled=lambda: False)
77
+
78
+
79
  class RankRefreshTests(unittest.TestCase):
80
  def setUp(self):
81
  self.stack = ExitStack()
 
199
  clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60,
200
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
201
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
202
+ _configured_mirror_entities=lambda *_: None, freeze_notice=FREEZE_OFF,
203
  _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False,
204
  tenant='synthetic', tenantSource='test', tenantSeconds=60,
205
  envSeconds=60, disagreement=None),
 
484
 
485
  # ── HOTFIX 2026-09-29 (owner): "only sync daily at the fastest", and only what changed ──────
486
  def run_daily_loop(self, memory, current, *, now=10_000.0, cadence=86400, manual=False,
487
+ errors=(), max_sleeps=1, frozen=False):
488
  """Drive the real loop with a persisted memory; stop at its `max_sleeps`-th wait."""
489
  source = ast.parse(Path(__file__).with_name('main.py').read_text(encoding='utf-8'))
490
  functions = {'_store_resync_loop', '_resync_rebuild_reason',
 
522
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
523
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
524
  _configured_mirror_entities=lambda *_: None,
525
+ freeze_notice=SimpleNamespace(enabled=lambda: frozen),
526
  _resolve_resync_cadence=lambda: dict(
527
  resolvedSeconds=cadence, manual=manual, tenant='synthetic',
528
  tenantSource='test', tenantSeconds=cadence, envSeconds=cadence,
 
543
  import harness
544
  from core import connector_sync
545
  wake = SimpleNamespace(wait=sleep, clear=lambda: None)
546
+ out = io.StringIO()
547
  with patch.dict(sys.modules, {'time': clock, 'harness.runtime': runtime}), \
548
  patch.object(harness, 'runtime', runtime, create=True), \
549
  patch.object(connector_sync, 'sync_wake_event', return_value=wake), \
550
  patch.object(connector_sync, 'take_sync_requests', return_value=[]), \
551
+ redirect_stdout(out):
552
+ if frozen:
553
+ self.assertIsNone(ns['_store_resync_loop']())
554
+ else:
555
+ with self.assertRaisesRegex(StopIteration, 'bounded synthetic'):
556
+ ns['_store_resync_loop']()
557
+ self.loop_output = out.getvalue()
558
  self.net.assert_not_called()
559
  return events, sleeps
560
 
 
601
  self.assertIn(('rebuild', None), events)
602
  self.assertNotIn('fresh', [e[0] for e in events])
603
 
604
+ def test_maintenance_freeze_holds_the_resident_loop(self):
605
+ """W-6864: a maintenance freeze holds the resident loop before its first pass"""
606
+ # The same inputs unfrozen rebuild in full (the test above), so the stub is load-bearing.
607
+ events, sleeps = self.run_daily_loop(
608
+ {'passAt': None, 'fullRebuildAt': None, 'fingerprints': None},
609
+ {'res_partner': (3, 7)}, frozen=True)
610
+ self.assertEqual((events, sleeps), ([], []))
611
+ self.assertIn('maintenance freeze holds store resync', self.loop_output)
612
+
613
+ def test_an_unstubbed_main_import_is_a_named_fixture_gap(self):
614
+ """W-6864 NC: an unstubbed main.py import stays a named FixtureGap"""
615
+ source = ast.parse(Path(__file__).with_name('main.py').read_text(encoding='utf-8'))
616
+ holds = [node for node in source.body
617
+ if isinstance(node, ast.FunctionDef) and node.name == '_freeze_holds']
618
+ self.assertEqual(len(holds), 1)
619
+ with self.assertRaisesRegex(FixtureGap, "reads 'freeze_notice'"):
620
+ _with_collaborators(source, holds, {})
621
+ self.assertEqual(_with_collaborators(source, holds, {'freeze_notice': FREEZE_OFF}), holds)
622
+
623
  def test_a_changed_entity_rebuilds_only_its_tables(self):
624
  events, _ = self.run_daily_loop(
625
  {'passAt': None, 'fullRebuildAt': 10_000.0 - 600,
platform/harness/datastore.py CHANGED
@@ -601,8 +601,13 @@ WINDOW_DEFAULT = 200
601
  _IDENT_OK = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
602
 
603
 
 
 
 
 
604
  def window(table=None, select=None, where=None, params=(), order_by=None,
605
- offset=0, limit=WINDOW_DEFAULT, cur=None, from_sql=None, group_by=None):
 
606
  """One page of a mirror table PLUS the true count of everything the same predicate matches.
607
 
608
  Returns `{"rows": [tuple, ...], "columns": [name, ...], "total": int, "offset": int,
@@ -626,6 +631,9 @@ def window(table=None, select=None, where=None, params=(), order_by=None,
626
  `count(DISTINCT ...)`, which is a projection, not a second meaning for this field.
627
  ⚠ It defaults to None and emits nothing then, so every existing caller's two statements are
628
  byte-identical to what they were before this argument existed.
 
 
 
629
  """
630
  if bool(table) == bool(from_sql):
631
  raise ValueError("datastore.window: pass exactly one of `table` or `from_sql`")
@@ -661,10 +669,19 @@ def window(table=None, select=None, where=None, params=(), order_by=None,
661
  total = int(cur.execute(f"SELECT count(*) FROM {name}{tail}", params).fetchone()[0] or 0)
662
  grp = str(group_by or "").strip()
663
  fold = f" GROUP BY {grp}" if grp else ""
664
- got = cur.execute(
665
- f"SELECT {proj} FROM {name}{tail}{fold} "
666
- f"ORDER BY {order} LIMIT {int(limit)} OFFSET {int(offset)}",
667
- params).fetchall()
 
 
 
 
 
 
 
 
 
668
  cols = [d[0] for d in (cur.description or [])]
669
  return {"rows": [tuple(r) for r in got], "columns": cols,
670
  "total": total, "offset": offset, "limit": limit, "clamped": clamped}
 
601
  _IDENT_OK = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
602
 
603
 
604
+ #: A scalar subquery inside SQL text: `window(page_first=True)` defers it until the page is chosen.
605
+ _SUBQUERY = re.compile(r"\(\s*SELECT\b", re.IGNORECASE)
606
+
607
+
608
  def window(table=None, select=None, where=None, params=(), order_by=None,
609
+ offset=0, limit=WINDOW_DEFAULT, cur=None, from_sql=None, group_by=None,
610
+ page_first=False):
611
  """One page of a mirror table PLUS the true count of everything the same predicate matches.
612
 
613
  Returns `{"rows": [tuple, ...], "columns": [name, ...], "total": int, "offset": int,
 
631
  `count(DISTINCT ...)`, which is a projection, not a second meaning for this field.
632
  ⚠ It defaults to None and emits nothing then, so every existing caller's two statements are
633
  byte-identical to what they were before this argument existed.
634
+
635
+ `page_first` (a row page whose `order_by` is an expression over the table's own columns) picks
636
+ the page's rows before the projection runs, when only the projection holds a subquery.
637
  """
638
  if bool(table) == bool(from_sql):
639
  raise ValueError("datastore.window: pass exactly one of `table` or `from_sql`")
 
669
  total = int(cur.execute(f"SELECT count(*) FROM {name}{tail}", params).fetchone()[0] or 0)
670
  grp = str(group_by or "").strip()
671
  fold = f" GROUP BY {grp}" if grp else ""
672
+ if (page_first and table and not grp and _SUBQUERY.search(proj)
673
+ and not _SUBQUERY.search(f"{pred} {order}")):
674
+ # A correlated lookup in the projection joins EVERY row before the LIMIT: one 50-row page
675
+ # of the 952k-row Amazon transactions held 2.8 GB and restarted staging (hotfix sdlc71).
676
+ got = cur.execute(
677
+ f"SELECT {proj} FROM (SELECT * FROM {name}{tail} ORDER BY {order} "
678
+ f"LIMIT {int(limit)} OFFSET {int(offset)}) AS {name} ORDER BY {order}",
679
+ params).fetchall()
680
+ else:
681
+ got = cur.execute(
682
+ f"SELECT {proj} FROM {name}{tail}{fold} "
683
+ f"ORDER BY {order} LIMIT {int(limit)} OFFSET {int(offset)}",
684
+ params).fetchall()
685
  cols = [d[0] for d in (cur.description or [])]
686
  return {"rows": [tuple(r) for r in got], "columns": cols,
687
  "total": total, "offset": offset, "limit": limit, "clamped": clamped}
platform/modules/procurement.py CHANGED
@@ -11,7 +11,7 @@ from Odoo (read-only).
11
  Reorder math (validated on the v24.4 Backend):
12
  8-month demand = units sold same 8-month window last year (all channels)
13
  avg/day = 8M demand / window days
14
- days of cover = (on-hand + incoming PO) / avg/day
15
  reorder point = lead-time days * avg/day
16
  Reorder QTY = ceil(reorder point * 1.05) [Baseline]
17
  Reorder +trend = Reorder QTY * (YTD units this yr / last yr)
@@ -183,6 +183,13 @@ def recommendations(t=None):
183
  if not by_code:
184
  return []
185
 
 
 
 
 
 
 
 
186
  rows = []
187
  for code, p in by_code.items():
188
  pid = p['id']
@@ -198,15 +205,20 @@ def recommendations(t=None):
198
  avg_day = d8 / window_days if window_days else 0.0
199
  on_hand = p.get('qty_available') or 0.0
200
  po = inpo.get(pid, 0.0)
201
- avail = on_hand + po
202
- days_cover = (avail / avg_day) if avg_day > 0 else (None if avail <= 0 else 9999.0)
 
 
 
203
  reorder_pt = lead * avg_day
204
  rbase = math.ceil(reorder_pt * (1 + _SAFETY)) if reorder_pt > 0 else 0
205
  yn, yp = ytd_now.get(pid, 0.0), ytd_prev.get(pid, 0.0)
206
  trend = (yn / yp) if yp > 0 else None
207
  rtrend = int(round(rbase * trend)) if trend is not None else rbase
208
- buy = (days_cover is not None and lead > 0 and avg_day > 0 and days_cover < lead)
 
209
  rows.append({
 
210
  'code': code, 'product': meta.get('desc') or p.get('name') or code,
211
  'unit': meta.get('unit') or '', 'qty_per_unit': meta.get('qty_per_unit'),
212
  'vendor': meta.get('vendor') or '(no supplier)', 'country': meta.get('country') or '',
@@ -226,6 +238,10 @@ def recommendations(t=None):
226
  'cheapest_price': cheapest['price'] if cheapest else None,
227
  'alt_vendors': priced,
228
  })
 
 
 
 
229
  return rows
230
 
231
 
@@ -596,4 +612,61 @@ def validate(t=None, team_id=None):
596
  checks.append({'check': 'Vendor reliability: Σ(per-vendor PO value) == Σ(row PO value)',
597
  'a': round(vv, 2), 'b': round(rv, 2), 'gap': round(vv - rv, 2),
598
  'ok': abs(vv - rv) <= 1.0})
 
599
  return checks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  Reorder math (validated on the v24.4 Backend):
12
  8-month demand = units sold same 8-month window last year (all channels)
13
  avg/day = 8M demand / window days
14
+ days of cover = (on-hand - FBA + incoming PO) / avg/day
15
  reorder point = lead-time days * avg/day
16
  Reorder QTY = ceil(reorder point * 1.05) [Baseline]
17
  Reorder +trend = Reorder QTY * (YTD units this yr / last yr)
 
183
  if not by_code:
184
  return []
185
 
186
+ try:
187
+ import modules.warehouse as warehouse
188
+ fba_by_pid, fba_report = warehouse.fba_qty_by_product_id(
189
+ [p['id'] for p in by_code.values()])
190
+ except Exception as exc:
191
+ fba_by_pid, fba_report = None, {
192
+ 'available': False, 'cause': 'the FBA warehouse read failed: ' + type(exc).__name__}
193
  rows = []
194
  for code, p in by_code.items():
195
  pid = p['id']
 
205
  avg_day = d8 / window_days if window_days else 0.0
206
  on_hand = p.get('qty_available') or 0.0
207
  po = inpo.get(pid, 0.0)
208
+ fba = (fba_by_pid or {}).get(pid)
209
+ fba_known = isinstance(fba, (int, float)) and fba_report.get('available') is not False
210
+ avail = on_hand - fba + po if fba_known else None
211
+ days_cover = ((avail / avg_day) if avg_day > 0 else
212
+ (None if avail <= 0 else 9999.0)) if avail is not None else None
213
  reorder_pt = lead * avg_day
214
  rbase = math.ceil(reorder_pt * (1 + _SAFETY)) if reorder_pt > 0 else 0
215
  yn, yp = ytd_now.get(pid, 0.0), ytd_prev.get(pid, 0.0)
216
  trend = (yn / yp) if yp > 0 else None
217
  rtrend = int(round(rbase * trend)) if trend is not None else rbase
218
+ buy = ((days_cover is not None and lead > 0 and avg_day > 0 and days_cover < lead)
219
+ if fba_known else None)
220
  rows.append({
221
+ '_product_id': pid, '_fba': fba,
222
  'code': code, 'product': meta.get('desc') or p.get('name') or code,
223
  'unit': meta.get('unit') or '', 'qty_per_unit': meta.get('qty_per_unit'),
224
  'vendor': meta.get('vendor') or '(no supplier)', 'country': meta.get('country') or '',
 
238
  'cheapest_price': cheapest['price'] if cheapest else None,
239
  'alt_vendors': priced,
240
  })
241
+ if not fba_known:
242
+ rows[-1]['_coverage_report'] = {**fba_report, 'available': False,
243
+ 'cause': fba_report.get('cause')
244
+ or 'FBA stock unavailable for this product'}
245
  return rows
246
 
247
 
 
612
  checks.append({'check': 'Vendor reliability: Σ(per-vendor PO value) == Σ(row PO value)',
613
  'a': round(vv, 2), 'b': round(rv, 2), 'gap': round(vv - rv, 2),
614
  'ok': abs(vv - rv) <= 1.0})
615
+ checks.extend(validate_coverage(recommendations(t=t), t=t))
616
  return checks
617
+
618
+
619
+ def validate_coverage(rows, t=None, oracle=None, demand=None):
620
+ """Reconcile procurement cover and buy arithmetic to independent live non-FBA quant stock."""
621
+ import modules.product_data as product_data
622
+ stock = product_data.coverage_quant_oracle() if oracle is None else oracle
623
+ if not stock.get('available') or not rows:
624
+ return [{'check': 'Procurement coverage source refusal: non-FBA stock is required',
625
+ 'a': 0, 'b': 1, 'gap': -1, 'ok': False,
626
+ 'detail': {'source_refusal': stock.get('cause') or 'no procurement rows'}}]
627
+ t = t or P.today()
628
+ ps, pe = _proj_window(t)
629
+ window_days = max(1, (pe - ps).days)
630
+ if demand is None:
631
+ f, to = _add_months(t, -12).isoformat(), _add_months(pe, -12).isoformat()
632
+ groups = O.get_odoo().read_group('sale.order.line', _demand_domain(f, to),
633
+ ['product_uom_qty:sum'], ['product_id'], lazy=False)
634
+ demand = {O.m2o_id(g['product_id']): float(g.get('product_uom_qty') or 0.0)
635
+ for g in groups if g.get('product_id')}
636
+ unavailable, wrong_days, wrong_buy = [], [], []
637
+ for r in rows:
638
+ pid = r.get('_product_id')
639
+ known = (pid is not None and isinstance(r.get('_fba'), (int, float))
640
+ and r.get('_coverage_report', {}).get('available') is not False)
641
+ expected_days, expected_buy = None, None
642
+ if not known:
643
+ unavailable.append({'code': r['code'],
644
+ 'cause': r.get('_coverage_report', {}).get('cause')
645
+ or 'FBA stock unavailable for this product'})
646
+ else:
647
+ effective = stock['non_fba_by_pid'].get(pid, 0.0) + float(r.get('incoming_po') or 0.0)
648
+ daily = demand.get(pid, 0.0) / window_days
649
+ expected_days = effective / daily if daily > 0 else (
650
+ None if effective <= 0 else 9999.0)
651
+ lead = r.get('lead') or 0
652
+ expected_buy = (expected_days is not None and lead > 0 and daily > 0
653
+ and expected_days < lead)
654
+ actual_days = r.get('days_cover')
655
+ if not (actual_days == expected_days or (
656
+ isinstance(actual_days, (int, float)) and isinstance(expected_days, (int, float))
657
+ and abs(actual_days - expected_days) < 1e-7)):
658
+ wrong_days.append(r['code'])
659
+ if r.get('buy') != expected_buy:
660
+ wrong_buy.append(r['code'])
661
+ detail = stock.get('detail', {})
662
+ return [
663
+ {'check': 'Procurement coverage source: each SKU has a measured FBA quantity',
664
+ 'a': len(rows) - len(unavailable), 'b': len(rows), 'gap': -len(unavailable),
665
+ 'ok': not unavailable, 'detail': {**detail, 'source_refusal': unavailable}},
666
+ {'check': 'Procurement days_cover == independent non-FBA quants plus inbound',
667
+ 'a': len(rows) - len(wrong_days), 'b': len(rows), 'gap': -len(wrong_days),
668
+ 'ok': not wrong_days, 'detail': {**detail, 'wrong_codes': wrong_days}},
669
+ {'check': 'Procurement buy == independent non-FBA cover below lead time',
670
+ 'a': len(rows) - len(wrong_buy), 'b': len(rows), 'gap': -len(wrong_buy),
671
+ 'ok': not wrong_buy, 'detail': {**detail, 'wrong_codes': wrong_buy}},
672
+ ]
platform/modules/product_data.py CHANGED
@@ -553,7 +553,7 @@ def _dos_with_inbound(on_hand, incoming, qty_ltm):
553
  return (None if dos_raw == float('inf') else round(float(dos_raw), 0)), bucket
554
 
555
 
556
- def _rescope_inventory(e, share, incoming=None):
557
  """`(qty_ltm, dos, bucket)` recomputed for ONE business unit's sales rate.
558
 
559
  The formulas are `inventory.sku_inventory`'s, applied to a BU-shaped numerator — NOT a second
@@ -561,8 +561,8 @@ def _rescope_inventory(e, share, incoming=None):
561
  copies of a threshold table is how the Product grid and the Inventory page start disagreeing
562
  about which SKUs are dead.
563
 
564
- ⚠ `on_hand` is whatever the warehouse holds, unscoped — so a Fisch reader's `dos` answers
565
- "how long does ALL our stock last at Fisch's rate", which is the question a Fisch salesperson
566
  actually has. It is deliberately NOT a pro-rated share of the shelf: there is no such shelf,
567
  and inventing one would put a number on screen that no Odoo query could reproduce.
568
  """
@@ -576,10 +576,12 @@ def _rescope_inventory(e, share, incoming=None):
576
  _dos, _bucket = _dos_with_inbound(None, incoming, 0.0)
577
  return None, _dos, _bucket
578
  qty = float(e.get("qty_ltm") or 0.0) * float(share or 0.0)
 
 
579
  # ⭐ THE FORMULA MOVED TO `_dos_with_inbound` (owner 2026-08-19) so the scoped and consolidated
580
  # paths cannot answer days-of-supply differently. Only the NUMERATOR is BU-shaped: `qty` is
581
  # this unit's share of LTM units, while the shelf and the inbound are one warehouse's.
582
- dos, bucket = _dos_with_inbound(on_hand, incoming, qty)
583
  return qty, dos, bucket
584
 
585
 
@@ -833,15 +835,27 @@ def pool(team_id=None, t=None, raw_fields=()):
833
  # ⭐⭐ OWNER 2026-08-19: *"Days of supply field should INCLUDE inbound quantities."* Read
834
  # here rather than in the cover-gap block below, because `dos` now needs it too.
835
  incoming = meta.get("incoming")
 
 
 
 
 
836
  if consolidated:
837
  # ⛔ RECOMPUTED, NOT `e['dos']`. `inventory.sku_inventory` returns a shelf-only
838
  # days-of-supply and knows nothing about purchase orders; taking its figure here would
839
  # ship the pre-ruling number under the new column description.
840
  qty_ltm = e.get("qty_ltm")
841
- dos, bucket = _dos_with_inbound(e.get("on_hand"), incoming, qty_ltm)
 
842
  else:
843
  qty_ltm, dos, bucket = _rescope_inventory(
844
- e, bu_share.get(code, _DEFAULT_SHARE), incoming)
 
 
 
 
 
 
845
  row.update({
846
  # UNSCOPED on purpose (owner 2026-08-11): one warehouse, no per-brand shelf.
847
  "on_hand": e.get("on_hand"),
@@ -852,7 +866,7 @@ def pool(team_id=None, t=None, raw_fields=()):
852
  # at Amazon" is measured. BLANK means the source could not answer at all, which is
853
  # `fba_by_code is None` (no FBA warehouse, or the read failed) or a code carrying no
854
  # Odoo product id. Those are different sentences and only one of them is a number.
855
- "fba": (fba_by_code or {}).get(code),
856
  "unit_cost": e.get("unit_cost"),
857
  "inv_value": e.get("inv_value"),
858
  # W48C-T09 — this is not `unit_cost`. The older field falls back to LTM COGS when
@@ -923,7 +937,7 @@ def pool(team_id=None, t=None, raw_fields=()):
923
  row["demand_fwd"] = round(fwd) if isinstance(fwd, (int, float)) and fwd > 0 else None
924
  daily = (fwd / float(products.FORWARD_DAYS)) if isinstance(fwd, (int, float)) \
925
  and fwd > 0 else None
926
- on_shelf = row["on_hand"]
927
  effective = ((on_shelf or 0.0) + (incoming or 0.0)
928
  if isinstance(on_shelf, (int, float)) else None)
929
  if daily and isinstance(effective, (int, float)) and isinstance(lead, (int, float)) \
@@ -1392,33 +1406,6 @@ def validate(team_id=None, t=None):
1392
  fresh_inventory = inventory.sku_inventory(t=t)
1393
  checks.append(reconcile_inventory_value_per_quantity(rows, fresh_inventory))
1394
 
1395
- # ⭐⭐ OWNER 2026-08-19 — `dos` COUNTS INBOUND, and this is the leg that proves it rather
1396
- # than trusting the expression. Two halves, because either alone is passable by accident:
1397
- # (a) EVERY row's dos re-derives from (on_hand + incoming) / (qty_ltm / 365), and
1398
- # (b) at least one row's dos is STRICTLY GREATER than the shelf-only figure would be.
1399
- # Without (b) the check stays green if `incoming` silently becomes 0 everywhere — the
1400
- # column would read exactly as it did before the ruling, and nothing would say so.
1401
- moved, wrong = 0, []
1402
- for r in rows:
1403
- oh, inc, q = r.get("on_hand"), r.get("incoming") or 0.0, r.get("qty_ltm")
1404
- if oh is None or not isinstance(q, (int, float)) or q <= 0:
1405
- continue
1406
- want = round((float(oh) + float(inc)) / (float(q) / 365.0), 0)
1407
- if r.get("dos") != want:
1408
- wrong.append(r["code"])
1409
- if inc > 0 and want > round(float(oh) / (float(q) / 365.0), 0):
1410
- moved += 1
1411
- checks.append({
1412
- "check": "Days of supply counts INBOUND units: every row re-derives from "
1413
- "(on_hand + incoming) / daily, and inbound demonstrably moves it",
1414
- "ours": moved, "theirs": len([r for r in rows if (r.get("incoming") or 0) > 0]),
1415
- "ok": not wrong and moved > 0,
1416
- "detail": {"rows_not_re_deriving": wrong[:10], "n_wrong": len(wrong),
1417
- "rows_where_inbound_raised_dos": moved,
1418
- "note": "a 0 in 'rows_where_inbound_raised_dos' means the column reads as "
1419
- "it did BEFORE the ruling, which is the silent-regression case."},
1420
- })
1421
-
1422
  # ⭐ RULE 8 — the FORWARD demand basis against a DIRECT Odoo read_group over the same
1423
  # reference window. ⛔ The oracle re-reads Odoo itself rather than re-calling
1424
  # `forward_demand_by_code`, which would reconcile the number with itself — the self-sealing
@@ -1457,9 +1444,137 @@ def validate(team_id=None, t=None):
1457
  checks.extend(validate_stock_measures(t=t))
1458
  checks.extend(validate_fba(rows))
1459
  checks.extend(validate_linked_metric(team_id=team_id))
 
1460
  return checks
1461
 
1462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1463
  def validate_linked_metric(customer_id=None, product_id=None, team_id=None,
1464
  date_from=None, date_to=None):
1465
  """Reconcile one customer/product revenue cell to an independent live Odoo aggregate.
@@ -1578,7 +1693,12 @@ def validate_fba(rows, oracle=None, sample=10):
1578
  ⚠ RUN ON `pool()` ROWS, NEVER ON `rows_from_pool` OUTPUT. `fba` is an `int` field, so the wire
1579
  projection rounds it and a fractional quant would read as a spurious sub-unit gap.
1580
  """
1581
- wh, by_pid, detail = oracle if oracle is not None else fba_quant_oracle()
 
 
 
 
 
1582
  rows = list(rows or ())
1583
  valued = [r for r in rows if isinstance(r.get('fba'), (int, float))
1584
  and not isinstance(r.get('fba'), bool)]
 
553
  return (None if dos_raw == float('inf') else round(float(dos_raw), 0)), bucket
554
 
555
 
556
+ def _rescope_inventory(e, share, incoming=None, fba=0.0):
557
  """`(qty_ltm, dos, bucket)` recomputed for ONE business unit's sales rate.
558
 
559
  The formulas are `inventory.sku_inventory`'s, applied to a BU-shaped numerator — NOT a second
 
561
  copies of a threshold table is how the Product grid and the Inventory page start disagreeing
562
  about which SKUs are dead.
563
 
564
+ ⚠ Stock stays warehouse-wide after subtracting FBA, so a Fisch reader's `dos` answers
565
+ "how long does non-FBA stock last at Fisch's rate", which is the question a Fisch salesperson
566
  actually has. It is deliberately NOT a pro-rated share of the shelf: there is no such shelf,
567
  and inventing one would put a number on screen that no Odoo query could reproduce.
568
  """
 
576
  _dos, _bucket = _dos_with_inbound(None, incoming, 0.0)
577
  return None, _dos, _bucket
578
  qty = float(e.get("qty_ltm") or 0.0) * float(share or 0.0)
579
+ if not isinstance(fba, (int, float)):
580
+ return qty, None, None
581
  # ⭐ THE FORMULA MOVED TO `_dos_with_inbound` (owner 2026-08-19) so the scoped and consolidated
582
  # paths cannot answer days-of-supply differently. Only the NUMERATOR is BU-shaped: `qty` is
583
  # this unit's share of LTM units, while the shelf and the inbound are one warehouse's.
584
+ dos, bucket = _dos_with_inbound(float(on_hand) - fba, incoming, qty)
585
  return qty, dos, bucket
586
 
587
 
 
835
  # ⭐⭐ OWNER 2026-08-19: *"Days of supply field should INCLUDE inbound quantities."* Read
836
  # here rather than in the cover-gap block below, because `dos` now needs it too.
837
  incoming = meta.get("incoming")
838
+ fba = (fba_by_code or {}).get(code)
839
+ fba_known = isinstance(fba, (int, float)) and _fba_report.get("available") is not False
840
+ on_hand = e.get("on_hand")
841
+ non_fba = (float(on_hand) - fba
842
+ if isinstance(on_hand, (int, float)) and fba_known else None)
843
  if consolidated:
844
  # ⛔ RECOMPUTED, NOT `e['dos']`. `inventory.sku_inventory` returns a shelf-only
845
  # days-of-supply and knows nothing about purchase orders; taking its figure here would
846
  # ship the pre-ruling number under the new column description.
847
  qty_ltm = e.get("qty_ltm")
848
+ dos, bucket = (_dos_with_inbound(non_fba, incoming, qty_ltm)
849
+ if fba_known else (None, None))
850
  else:
851
  qty_ltm, dos, bucket = _rescope_inventory(
852
+ e, bu_share.get(code, _DEFAULT_SHARE), incoming, fba if fba_known else None)
853
+ if not fba_known:
854
+ dos, bucket = None, None
855
+ if not fba_known:
856
+ row["_coverage_report"] = {**_fba_report, "available": False,
857
+ "cause": _fba_report.get("cause")
858
+ or "FBA stock unavailable for this product"}
859
  row.update({
860
  # UNSCOPED on purpose (owner 2026-08-11): one warehouse, no per-brand shelf.
861
  "on_hand": e.get("on_hand"),
 
866
  # at Amazon" is measured. BLANK means the source could not answer at all, which is
867
  # `fba_by_code is None` (no FBA warehouse, or the read failed) or a code carrying no
868
  # Odoo product id. Those are different sentences and only one of them is a number.
869
+ "fba": fba,
870
  "unit_cost": e.get("unit_cost"),
871
  "inv_value": e.get("inv_value"),
872
  # W48C-T09 — this is not `unit_cost`. The older field falls back to LTM COGS when
 
937
  row["demand_fwd"] = round(fwd) if isinstance(fwd, (int, float)) and fwd > 0 else None
938
  daily = (fwd / float(products.FORWARD_DAYS)) if isinstance(fwd, (int, float)) \
939
  and fwd > 0 else None
940
+ on_shelf = non_fba
941
  effective = ((on_shelf or 0.0) + (incoming or 0.0)
942
  if isinstance(on_shelf, (int, float)) else None)
943
  if daily and isinstance(effective, (int, float)) and isinstance(lead, (int, float)) \
 
1406
  fresh_inventory = inventory.sku_inventory(t=t)
1407
  checks.append(reconcile_inventory_value_per_quantity(rows, fresh_inventory))
1408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1409
  # ⭐ RULE 8 — the FORWARD demand basis against a DIRECT Odoo read_group over the same
1410
  # reference window. ⛔ The oracle re-reads Odoo itself rather than re-calling
1411
  # `forward_demand_by_code`, which would reconcile the number with itself — the self-sealing
 
1444
  checks.extend(validate_stock_measures(t=t))
1445
  checks.extend(validate_fba(rows))
1446
  checks.extend(validate_linked_metric(team_id=team_id))
1447
+ checks.extend(validate_coverage(rows, t=t, team_id=team_id))
1448
  return checks
1449
 
1450
 
1451
+ def coverage_quant_oracle():
1452
+ """Read signed internal stock.quant totals outside and inside the independently resolved FBA subtree."""
1453
+ try:
1454
+ o = O.get_odoo()
1455
+ found = o.search_read('stock.warehouse', [('code', '=', 'FBA')],
1456
+ ['id', 'code', 'view_location_id'])
1457
+ if not found:
1458
+ return {'available': False, 'cause': 'no stock.warehouse carries the code FBA'}
1459
+ wh = found[0]
1460
+ root = O.m2o_id(wh.get('view_location_id'))
1461
+ if not root:
1462
+ return {'available': False, 'cause': 'the FBA warehouse has no location subtree'}
1463
+ quantities = []
1464
+ for exclude_fba in (True, False):
1465
+ domain = [('location_id.usage', '=', 'internal')]
1466
+ if exclude_fba:
1467
+ domain.append('!')
1468
+ domain.append(('location_id', 'child_of', root))
1469
+ groups = o.read_group('stock.quant', domain,
1470
+ ['quantity:sum'], ['product_id'], lazy=False)
1471
+ quantities.append({O.m2o_id(g['product_id']): float(g.get('quantity') or 0.0)
1472
+ for g in groups if g.get('product_id')})
1473
+ records = o.search_read('product.product', [('active', 'in', [True, False])],
1474
+ ['id', 'default_code', 'active'])
1475
+ ids_by_code = {}
1476
+ for p in records:
1477
+ if p.get('default_code') and p.get('active') is not False:
1478
+ code = str(p['default_code']).strip()
1479
+ ids_by_code.setdefault(code, []).append(p['id'])
1480
+ return {'available': True, 'non_fba_by_pid': quantities[0],
1481
+ 'fba_by_pid': quantities[1], 'ids_by_code': ids_by_code,
1482
+ 'detail': {'warehouse_id': wh['id'], 'location_root': root,
1483
+ 'source': 'live stock.quant quantity:sum excluding the FBA subtree'}}
1484
+ except Exception as exc:
1485
+ return {'available': False, 'cause': 'non-FBA stock.quant read failed: ' + type(exc).__name__}
1486
+
1487
+
1488
+ def validate_coverage(rows, t=None, team_id=None, oracle=None, demand=None):
1489
+ """Reconcile coverage and its signals to independent live non-FBA quant stock in either BU scope."""
1490
+ stock = coverage_quant_oracle() if oracle is None else oracle
1491
+ if not stock.get('available') or not rows:
1492
+ return [{'check': 'Coverage source refusal: independent non-FBA stock is required',
1493
+ 'a': 0, 'b': 1, 'gap': -1, 'ok': False,
1494
+ 'detail': {'team_id': team_id, 'source_refusal': stock.get('cause')
1495
+ or 'the product pool has no rows'}}]
1496
+ if demand is None:
1497
+ demand, _report = products.forward_demand_by_code(t=t, team_id=team_id)
1498
+ outside, inside = stock['non_fba_by_pid'], stock['fba_by_pid']
1499
+ ids_by_code = stock['ids_by_code']
1500
+ unavailable, wrong_dos, wrong_cover, residuals = [], [], [], {}
1501
+ for r in rows:
1502
+ days = None
1503
+ code, pid = r['code'], r.get('product_id')
1504
+ known = (isinstance(r.get('fba'), (int, float))
1505
+ and r.get('_coverage_report', {}).get('available') is not False)
1506
+ if not known:
1507
+ unavailable.append({'code': code, 'cause': r.get('_coverage_report', {}).get('cause')
1508
+ or 'FBA stock unavailable for this product'})
1509
+ expected_dos, expected_bucket = None, None
1510
+ shelf = None
1511
+ else:
1512
+ ids = ids_by_code.get(code)
1513
+ # Inventory groups active SKU ids, while the FBA column selects the catalogue id.
1514
+ residual = sum(inside.get(i, 0.0) for i in (ids or []) if i != pid)
1515
+ if residual:
1516
+ residuals[code] = residual
1517
+ shelf = sum(outside.get(i, 0.0) for i in ids) + residual if ids else None
1518
+ qty = r.get('qty_ltm') or 0.0
1519
+ if shelf is None:
1520
+ expected_dos, expected_bucket = None, 'No stock record'
1521
+ else:
1522
+ effective = shelf + float(r.get('incoming') or 0.0)
1523
+ days = effective / (qty / 365.0) if qty > 0 else (
1524
+ float('inf') if effective > 0 else 0.0)
1525
+ expected_dos = None if days == float('inf') else round(days, 0)
1526
+ if shelf <= 0:
1527
+ expected_bucket = 'Out of stock'
1528
+ elif qty <= 0:
1529
+ expected_bucket = 'No recent sales'
1530
+ else:
1531
+ bands = ((0, 15, 'Critical (<15d)'), (15, 45, 'Lean (15 to 45d)'),
1532
+ (45, 120, 'Healthy (45 to 120d)'), (120, 270, 'Ample (120 to 270d)'),
1533
+ (270, 540, 'Overstock (270 to 540d)'))
1534
+ expected_bucket = next((label for lower, upper, label in bands
1535
+ if lower <= days < upper),
1536
+ 'Excess (>540d)')
1537
+ actual_dos = r.get('dos')
1538
+ dos_matches = actual_dos == expected_dos
1539
+ if isinstance(actual_dos, (int, float)) and days is not None and _math.isfinite(days):
1540
+ # Independent float aggregates can straddle an exact half-day rounding boundary.
1541
+ dos_matches = abs(actual_dos - days) <= 0.5 + 1e-7
1542
+ if not dos_matches or r.get('stock_bucket') != expected_bucket:
1543
+ wrong_dos.append(code)
1544
+ fwd, lead = demand.get(code), r.get('lead_days')
1545
+ expected = (None, None, None)
1546
+ cover_days = None
1547
+ if shelf is not None and isinstance(fwd, (int, float)) and fwd > 0 \
1548
+ and isinstance(lead, (int, float)) and lead > 0:
1549
+ daily = fwd / float(products.FORWARD_DAYS)
1550
+ effective = shelf + float(r.get('incoming') or 0.0)
1551
+ short = daily * lead - effective
1552
+ units = int(_math.ceil(short) if short > 0 else _math.floor(short))
1553
+ cover_days = effective / daily - lead
1554
+ expected = (units, int(round(cover_days)),
1555
+ 'Buy now' if units > 0 else 'OK')
1556
+ actual = (r.get('cover_gap_units'), r.get('cover_gap_d'), r.get('buy_now'))
1557
+ cover_matches = actual == expected
1558
+ if cover_days is not None and isinstance(actual[1], (int, float)):
1559
+ cover_matches = (actual[0] == expected[0] and actual[2] == expected[2]
1560
+ and abs(actual[1] - cover_days) <= 0.5 + 1e-7)
1561
+ if not cover_matches:
1562
+ wrong_cover.append(code)
1563
+ detail = {**stock.get('detail', {}), 'team_id': team_id,
1564
+ 'fba_sibling_residual_by_code': residuals}
1565
+ return [
1566
+ {'check': 'Coverage source: each product has a measured FBA quantity',
1567
+ 'a': len(rows) - len(unavailable), 'b': len(rows), 'gap': -len(unavailable),
1568
+ 'ok': not unavailable, 'detail': {**detail, 'source_refusal': unavailable}},
1569
+ {'check': 'Days of supply and stock_bucket == independent non-FBA quants plus inbound',
1570
+ 'a': len(rows) - len(wrong_dos), 'b': len(rows), 'gap': -len(wrong_dos),
1571
+ 'ok': not wrong_dos, 'detail': {**detail, 'wrong_codes': wrong_dos}},
1572
+ {'check': 'Cover gap units, days and buy_now == independent non-FBA quants plus inbound',
1573
+ 'a': len(rows) - len(wrong_cover), 'b': len(rows), 'gap': -len(wrong_cover),
1574
+ 'ok': not wrong_cover, 'detail': {**detail, 'wrong_codes': wrong_cover}},
1575
+ ]
1576
+
1577
+
1578
  def validate_linked_metric(customer_id=None, product_id=None, team_id=None,
1579
  date_from=None, date_to=None):
1580
  """Reconcile one customer/product revenue cell to an independent live Odoo aggregate.
 
1693
  ⚠ RUN ON `pool()` ROWS, NEVER ON `rows_from_pool` OUTPUT. `fba` is an `int` field, so the wire
1694
  projection rounds it and a fractional quant would read as a spurious sub-unit gap.
1695
  """
1696
+ try:
1697
+ wh, by_pid, detail = oracle if oracle is not None else fba_quant_oracle()
1698
+ except Exception as exc:
1699
+ return [{'check': 'FBA source refusal: independent stock.quant read failed',
1700
+ 'a': 0, 'b': 1, 'gap': -1, 'ok': False,
1701
+ 'detail': {'source_refusal': type(exc).__name__}}]
1702
  rows = list(rows or ())
1703
  valued = [r for r in rows if isinstance(r.get('fba'), (int, float))
1704
  and not isinstance(r.get('fba'), bool)]
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- badf2ed512593f7f4ca3759b4daa13cfa62221c61b94ad3a51c684a292b3432d
 
1
+ 00f4ab2a6e805f652f413e45a8f825f69d4ce98ab0f2d9762c47f2d2a86b5b55