diff --git a/RELEASES.json b/RELEASES.json index e10ae5a5f05a94488d46c3176c51cfc4885a2501..38b8b42896fe4cdf43e4c47d687dae977e0e5434 100644 --- a/RELEASES.json +++ b/RELEASES.json @@ -1,455 +1,11 @@ { - "current": "v76 (7990f03e02)", + "current": "v77 (e741e5ca)", "releases": [ { - "version": "v76", - "sha": "7990f03e02", - "date": "2026-10-01", - "subject": "release v76" - }, - { - "version": "v75", - "sha": "2c5146981c", - "date": "2026-09-19", - "subject": "release v75" - }, - { - "version": "v74", - "sha": "40dceba926", - "date": "2026-09-14", - "subject": "release v74: waves 58+59 incl. the Amazon credential door (W59-T21/22/23) and sync producer (W59-T42); hotfix promote so" - }, - { - "version": "v73", - "sha": "bb94de5665", - "date": "2026-09-13", - "subject": "release v73" - }, - { - "version": "v71", - "sha": "96143dc6fb", - "date": "2026-09-04", - "subject": "release v71 - wave 51, QA'd 24 PASS / 6 FAIL / 5 UNPROVEN" - }, - { - "version": "v70", - "sha": "eedfb6a001", - "date": "2026-09-03", - "subject": "release v70 - wave 50 (grid analytics, the Sheet as a spreadsheet, view trash and audit)" - }, - { - "version": "v69", - "sha": "8598f56293", - "date": "2026-09-02", - "subject": "release v69: wave48-continuation validated + hotfixes v67/v68/d4e3e31 + go-live QA repairs (Supplier option limit, optio" - }, - { - "version": "v68", - "sha": "62b065a261", - "date": "2026-09-01", - "subject": "release v68: unified holiday date offsets" - }, - { - "version": "v67", - "sha": "3b2241982c", - "date": "2026-09-01", - "subject": "release v67" - }, - { - "version": "v66", - "sha": "73c94f0767", - "date": "2026-08-31", - "subject": "release v66" - }, - { - "version": "v65", - "sha": "9f6cfedb8a", - "date": "2026-08-31", - "subject": "release v65" - }, - { - "version": "v64", - "sha": "bb8985c6e7", - "date": "2026-08-31", - "subject": "release v64" - }, - { - "version": "v63", - "sha": "ec75cb8f34", - "date": "2026-08-31", - "subject": "release v63" - }, - { - "version": "v62", - "sha": "2c8f702c96", - "date": "2026-08-31", - "subject": "release v62" - }, - { - "version": "v61", - "sha": "bbc4c0760a", - "date": "2026-08-30", - "subject": "v61 - wave 46 hotfix: D-590 schema overlay reaches the grid read (all three assemblies), D-599 route to zone association" - }, - { - "version": "v60", - "sha": "dd1b062021", - "date": "2026-08-30", - "subject": "v60 - wave 46" - }, - { - "version": "v59", - "sha": "3cb73ce802", - "date": "2026-08-29", - "subject": "release v59" - }, - { - "version": "v58", - "sha": "c526cede82", - "date": "2026-08-29", - "subject": "release v58" - }, - { - "version": "v57", - "sha": "4859c035e6", - "date": "2026-08-29", - "subject": "release v57" - }, - { - "version": "v56", - "sha": "2970b729e8", - "date": "2026-08-28", - "subject": "release v56" - }, - { - "version": "v55", - "sha": "b766b904b8", - "date": "2026-08-28", - "subject": "release v55" - }, - { - "version": "v54", - "sha": "6a8e99a064", - "date": "2026-08-27", - "subject": "release v54" - }, - { - "version": "v53", - "sha": "89ed2cb867", - "date": "2026-08-24", - "subject": "release v53" - }, - { - "version": "v52", - "sha": "8263493762", - "date": "2026-08-23", - "subject": "release v52" - }, - { - "version": "v51", - "sha": "5740d20fa5", - "date": "2026-08-23", - "subject": "release v51" - }, - { - "version": "v50", - "sha": "8027c83d1d", - "date": "2026-08-23", - "subject": "release v50" - }, - { - "version": "v49", - "sha": "9a14c01a48", - "date": "2026-08-23", - "subject": "release v49" - }, - { - "version": "v48", - "sha": "3c69fd93da", - "date": "2026-08-23", - "subject": "release v48" - }, - { - "version": "v47", - "sha": "917f9a0eb5", - "date": "2026-08-23", - "subject": "release v47" - }, - { - "version": "v46", - "sha": "a8dc531b50", - "date": "2026-08-23", - "subject": "release v46" - }, - { - "version": "v45", - "sha": "0b2b5010d8", - "date": "2026-08-23", - "subject": "release v45" - }, - { - "version": "v44", - "sha": "a7877aaf20", - "date": "2026-08-23", - "subject": "Hotfix: allow renaming the active Map view" - }, - { - "version": "v43", - "sha": "e2ba4616fa", - "date": "2026-08-23", - "subject": "release v43" - }, - { - "version": "v42", - "sha": "25b885e637", - "date": "2026-08-23", - "subject": "release v42" - }, - { - "version": "v41", - "sha": "1d6adcc3c3", - "date": "2026-08-22", - "subject": "v41 vertical route navigation" - }, - { - "version": "v40", - "sha": "1451837168", - "date": "2026-08-22", - "subject": "release v40 route UI correction" - }, - { - "version": "v39", - "sha": "a78bdf00e3", - "date": "2026-08-21", - "subject": "W39 clean stale migration targets" - }, - { - "version": "v38", - "sha": "a556631cf9", - "date": "2026-08-21", - "subject": "W39 reversible Live store cutover" - }, - { - "version": "v37", - "sha": "1ddd8cf823", - "date": "2026-08-21", - "subject": "W39: coalesce derived workspace reads" - }, - { - "version": "v36", - "sha": "51db3b7fee", - "date": "2026-08-21", - "subject": "W39 share-field oid repair" - }, - { - "version": "v35", - "sha": "0f642129c8", - "date": "2026-08-21", - "subject": "W39 visual QA fixture" - }, - { - "version": "v34", - "sha": "cb4caa188e", - "date": "2026-08-21", - "subject": "W39 Staging PostgreSQL egress controls" - }, - { - "version": "v33", - "sha": "2b115e573d", - "date": "2026-08-21", - "subject": "W39: stop nonproduction Postgres background egress" - }, - { - "version": "v32", - "sha": "b47b27ca7b", - "date": "2026-08-21", - "subject": "W39: repair Staging Postgres projection" - }, - { - "version": "v31", - "sha": "8880a49a24", - "date": "2026-08-21", - "subject": "v31: Wave 39 staging candidate" - }, - { - "version": "v30", - "sha": "71776ca6c8", - "date": "2026-08-20", - "subject": "v30: W39-T29 Member-owned View first-share hotfix" - }, - { - "version": "v29", - "sha": "55ced5018f", - "date": "2026-08-18", - "subject": "v29: the build staging has been running since 2026-08-18 (55ced50)." - }, - { - "version": "v28", - "sha": "2793e4ba53", - "date": "2026-08-18", - "subject": "release v28" - }, - { - "version": "v27", - "sha": "c05a794837", - "date": "2026-08-18", - "subject": "release v27" - }, - { - "version": "v25", - "sha": "bc248295ed", - "date": "2026-08-14", - "subject": "v25 - wave 32's LIVE build (deployed 2026-08-13, bc24829)" - }, - { - "version": "v26", - "sha": "6a642e139c", - "date": "2026-08-14", - "subject": "v26 - wave 33's build (deployed 2026-08-14, 6a642e1)" - }, - { - "version": "v24", - "sha": "514d8292ce", - "date": "2026-08-09", - "subject": "release v24" - }, - { - "version": "v23", - "sha": "44cc28380f", - "date": "2026-08-09", - "subject": "release v23" - }, - { - "version": "v22", - "sha": "f14e199ff3", - "date": "2026-08-09", - "subject": "release v22" - }, - { - "version": "v21", - "sha": "5fdffce8b5", - "date": "2026-08-09", - "subject": "release v21" - }, - { - "version": "v20", - "sha": "442e4a1430", - "date": "2026-08-09", - "subject": "release v20" - }, - { - "version": "v19", - "sha": "94cb4a4dfd", - "date": "2026-08-09", - "subject": "release v19" - }, - { - "version": "v18", - "sha": "0106ef9498", - "date": "2026-08-09", - "subject": "release v18" - }, - { - "version": "v17", - "sha": "308240cc75", - "date": "2026-08-09", - "subject": "release v17" - }, - { - "version": "v16", - "sha": "a804df5443", - "date": "2026-08-09", - "subject": "release v16" - }, - { - "version": "v15", - "sha": "6b84d7264c", - "date": "2026-08-09", - "subject": "release v15" - }, - { - "version": "v14", - "sha": "bee5eaf5b5", - "date": "2026-08-07", - "subject": "release v14" - }, - { - "version": "v13", - "sha": "314c0e368b", - "date": "2026-08-06", - "subject": "release v13" - }, - { - "version": "v12", - "sha": "4908f39978", - "date": "2026-08-06", - "subject": "release v12" - }, - { - "version": "v11", - "sha": "312661712b", - "date": "2026-08-05", - "subject": "release v11" - }, - { - "version": "v10", - "sha": "43f8042874", - "date": "2026-08-05", - "subject": "release v10" - }, - { - "version": "v9", - "sha": "2e26f57396", - "date": "2026-08-05", - "subject": "release v9" - }, - { - "version": "v8", - "sha": "8073b3ecba", - "date": "2026-08-05", - "subject": "release v8" - }, - { - "version": "v7", - "sha": "b1c5a7e9b8", - "date": "2026-08-05", - "subject": "release v7" - }, - { - "version": "v6", - "sha": "a344020f11", - "date": "2026-08-05", - "subject": "release v6" - }, - { - "version": "v5", - "sha": "c0b0fbb40a", - "date": "2026-08-05", - "subject": "release v5" - }, - { - "version": "v4", - "sha": "de5037ff1c", - "date": "2026-08-05", - "subject": "release v4" - }, - { - "version": "v3", - "sha": "6b9fa62d93", - "date": "2026-08-05", - "subject": "release v3" - }, - { - "version": "v2", - "sha": "fd05861998", - "date": "2026-08-04", - "subject": "release v2" - }, - { - "version": "v1", - "sha": "5b4e2c4bc3", - "date": "2026-08-04", - "subject": "release v1" + "version": "v77", + "sha": "e741e5ca", + "date": "2026-10-05", + "subject": "release v77" } ] } \ No newline at end of file diff --git a/VERSION b/VERSION index 04e0381fdefdce440af84c198e42c458cd7be95d..8b8cf45e6576c3723bc2f5c3810064231372e832 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v76 (7990f03e02) +v77 (e741e5ca) diff --git a/api/agent_tools.py b/api/agent_tools.py index 52b5c23fa1bfc416e4dc97bcd59e5ab7aea56e50..3e49a4dd0e25ef6b8989df55d27f5fbca4fff952 100644 --- a/api/agent_tools.py +++ b/api/agent_tools.py @@ -34,6 +34,20 @@ def register(name, schema, fn, *, effect): def load_modules(): + from core import keychain + + # Minting changes only capability metadata. Credentials are entered through the + # public form, so every channel may return this link without a write confirmation. + register("connector_setup_link", { + "type": "object", + "description": "Create a one-time HTTPS setup link for your connector. Enter credentials only through the link.", + "properties": { + "connector": {"type": "string", "enum": sorted(keychain.ENTRY_TYPES)}, + "scope": {"type": "string", "enum": ["personal", "business"]}, + }, + "required": ["connector", "scope"], + "additionalProperties": False, + }, _connector_setup_link, effect="read") for name in AGENT_TOOL_MODULES: try: module = importlib.import_module(name) @@ -48,6 +62,12 @@ def load_modules(): _LOG.info("Assistant tool entry in %s was skipped (%s)", name, type(exc).__name__) +def _connector_setup_link(session, args): + from routes_forms import mint_setup_link + + return mint_setup_link(args, session=session) + + def registered(): return {name: {**entry, "schema": copy.deepcopy(entry["schema"])} for name, entry in _TOOLS.items()} diff --git a/api/amazon_relational.py b/api/amazon_relational.py index 30836b3c8d194eb8acd66ade85384dcb4c4f7ff6..b9d77980016cb758842879e079dd097830e0a42a 100644 --- a/api/amazon_relational.py +++ b/api/amazon_relational.py @@ -139,6 +139,14 @@ _MONEY.update({"estimated_fee_total", "estimated_referral_fee_per_unit", #: fractional field type the grid offers, so they take it and the LABEL carries the meaning. _RATE_SUFFIX = ("_percentage", "_percentage_b2_b", "_rate", "_ctr") +# Inventory reports remain VARCHAR when a quantity column mixes numbers and blanks. +# Names identify units; identifiers such as SKU and FNSKU must stay text. +_QUANTITY = {"quantity", "quantity_purchased", "starting_warehouse_balance", + "in_transit_between_warehouses", "receipts", "customer_shipments", + "customer_returns", "vendor_returns", "warehouse_transfer_in", + "warehouse_transfer_out", "found", "lost", "damaged", "disposed", + "other_events", "ending_warehouse_balance", "unknown_events"} + def _label(col): return col.replace("_", " ").strip().capitalize() @@ -157,10 +165,10 @@ def _ftype(col, duck_type): if col in _MONEY or col.endswith(("_amount", "_fee")) or any(col.endswith(s) for s in _RATE_SUFFIX): return "currency" base = str(duck_type or "").upper().split("(")[0] - if base in ("BIGINT", "INTEGER", "HUGEINT") or base.startswith("DECIMAL"): - return "int" - if base in ("DOUBLE", "FLOAT"): + if base in ("DOUBLE", "FLOAT", "DECIMAL"): return "currency" + if col in _QUANTITY or base in ("BIGINT", "INTEGER", "HUGEINT"): + return "int" return "text" @@ -521,9 +529,10 @@ def grid_source(cur, key): "needs_excluded": False, "cols": cols} -def grid_sources(): +def grid_sources(cur=None): """Only Amazon read-through grids; no keys at all on an Odoo-only mirror.""" - cur = datastore.ro_cursor() + own_cursor = cur is None + cur = datastore.ro_cursor() if own_cursor else cur try: present = {key for key, table in SOURCE.items() if mirror_columns(cur, table)} if not present: @@ -535,7 +544,8 @@ def grid_sources(): keys.add(key) return {key: grid_source(cur, key) for key in SOURCE if key in keys} finally: - cur.close() + if own_cursor: + cur.close() def _row_id(row, key): @@ -584,8 +594,12 @@ def _read(cur, key): cols = [c[0] for c in mirror_columns(cur, SOURCE[key])] if not cols: return [] - quoted = ", ".join(f'"{c}"' for c in cols) spec = grid_source(cur, key) + # Stored snapshots and mirror windows share their numeric casts. Empty report cells + # become null; decimals remain numbers instead of text that defeats aggregation. + quoted = ", ".join(spec["cols"][c][0] for c in cols) + numeric = {name for name, dtype in mirror_columns(cur, SOURCE[key]) + if _ftype(name, dtype) in ("currency", "int")} # S67-T30: a stored snapshot carries the same product lookups, computed by the SAME SQL # expressions the read-through binding uses, in this one SELECT (never per row). derived = [(lookup["key"], lookup["expr"]) for lookup in lookups(cur, key)] @@ -596,6 +610,9 @@ def _read(cur, key): for r in rows: row = {} for name, val in zip(names, r[1:]): + if name in numeric: + row[name] = spec["cols"][name][1](val) + continue # Postgres JSON cannot hold U+0000, and one such byte refused the whole Amazon write. row[name] = "" if val is None else (val if isinstance(val, (int, float)) else str(val).replace("\x00", "")) @@ -858,6 +875,17 @@ def apply_plan(rt, built, username="amazon", today=None): written[key].update(rows=rows["count"], removed=removed, readThrough=True) else: doc[key].pop("readThrough", None) + # The shared merger compares string spellings, so an old "2" and a new + # integer 2 otherwise compare equal and leave the old cell untyped. + numeric = {f["key"] for f in doc[key].get("fields", []) + if f.get("type") in ("currency", "int") + and (f.get("automation") or {}).get("flowId") == "amazon_relational" + and not _odoo._ut().user_edited(f)} + stored = doc[key].get("rows") or {} + for row in rows: + saved = stored.get(row["_id"]) + if numeric and isinstance(saved, dict): + saved.update({name: row[name] for name in numeric if name in row}) # DPP 1.8 attribution is explicit in the stored definition, not inferred from a # mutable display label or from the process having imported this module. doc[key]["connector"] = source_attribution(key) @@ -913,6 +941,10 @@ def _refresh_from_cursor(cur, rt, why, tenant, previous_fingerprint, log, state) f"(run `python platform/harness/amazon_store.py --sync --tenant {tenant}` first)") return state state["present"] = True + # S70-T12 / D-3495: publish immediately from the restored cursor, before planning or + # the unchanged-fingerprint return. Registry readiness must not wait for a store write. + import routes_odoo_tables + routes_odoo_tables._learn_amazon_sources(cur) built = plan(cur, rt) problems = built.get("problems") or [] for p in problems: diff --git a/api/automation_engine.py b/api/automation_engine.py index 2b5306fdb428c3bfbb4d96ea11c682fe7a261735..de5a95389f36610ca23f1533b5bb3d0aafa50030 100644 --- a/api/automation_engine.py +++ b/api/automation_engine.py @@ -16506,6 +16506,10 @@ def start_scheduler(log=print): external EventBridge tick (R5) does not depend on it either way, since that POSTs the endpoint rather than riding this thread. """ + from core import freeze_notice + if freeze_notice.enabled(): + log("[aios-auto] maintenance freeze holds automation scheduler") + return False if os.environ.get("AIOS_AUTOMATIONS") != "1": return False # PostgreSQL production has one durable wake-up: EventBridge -> Lambda -> protected endpoint. diff --git a/api/automation_worker.py b/api/automation_worker.py index c62c371c7c3004cec21bafbfa43e5995d21d8706..c7422f977dd5a82753a27602fcc74890cb20d1cd 100644 --- a/api/automation_worker.py +++ b/api/automation_worker.py @@ -67,7 +67,9 @@ def _claim(job: dict): url = str(os.environ.get("DATABASE_URL") or "").strip() if not url: raise RuntimeError("DATABASE_URL is required") - con = psycopg.connect(url) + ca = os.environ.get("AIOS_DATABASE_CA_PEM_B64") + options = {"sslrootcert": store_pg._database_ca_file(ca)} if ca is not None else {} + con = psycopg.connect(url, **options) lock_name = f"aios-automation:{job['tenant']}:{job['automationId'] or job['mode']}" with con.cursor() as cur: cur.execute("SELECT pg_try_advisory_lock(hashtextextended(%s, 0))", (lock_name,)) diff --git a/api/connector_onboarding.py b/api/connector_onboarding.py new file mode 100644 index 0000000000000000000000000000000000000000..a58e0b994483f778cf06160d30a043c47ca5296a --- /dev/null +++ b/api/connector_onboarding.py @@ -0,0 +1,124 @@ +"""First-connection choices, stored before a credential can dispatch a sync.""" +from __future__ import annotations + + +def choices(provider): + if provider == "odoo": + import odoo_relational as rel + defaults = {"invoices", "orders", "order_lines", "stock_moves", "stock_valuation", "stock_quant"} + return ([{"key": "customer_data", "label": "Customers", "selected": True}, + {"key": "product_data", "label": "Products", "selected": True}] + + [{"key": key, "label": label, "selected": bucket in defaults} + for bucket, key, label, _ in rel.TABLES]) + if provider == "amazon_sp": + from harness import amazon_store + return [{"key": "ut_amzn_" + key, "label": key.replace("_", " ").capitalize(), + "selected": key in {"orders", "order_items", "inventory", "settlements"}} + for key in amazon_store.TABLES] + return [] + + +def install_selection(rt, provider, selected, *, existing=False): + """Explicit first-save intent only; preserve every pre-existing connector configuration. + + This runs before add_entry, so an automatic runner cannot see a credential with all-on defaults. + A failed credential save leaves a safe, resumable choice document, never an unselected sync. + """ + offered = {row["key"] for row in choices(provider)} + if not offered or not isinstance(selected, list) or any(type(key) is not str for key in selected): + raise ValueError("Choose databases from this connector's setup list.") + if len(set(selected)) != len(selected) or not set(selected) <= offered: + raise ValueError("The database selection contains an unknown or duplicate database.") + if existing: + raise ValueError("This connection already exists. Change its databases in Settings.") + if provider == "odoo": + import odoo_relational as rel + key, member = rel.CONFIG_KEY, "grids" + else: + import connectors_amazon as amazon + key, member = amazon.ENABLEMENT_KEY, "databases" + + def install(current): + if current is None: + current = {} + if not isinstance(current, dict): + raise ValueError("The stored connector settings are unavailable.") + # Only our own incomplete setup can be retried. Older selections are never repurposed. + if current and current.get("initialSetup") != "selected": + raise ValueError("This workspace already has database choices. Use Settings to change them.") + return {**current, member: {name: name in selected for name in sorted(offered)}, + "initialSetup": "selected"} + + rt.update(key, install, flush="sync") + + +def seal_selection(rt, provider): + from odoo_relational import CONFIG_KEY + from connectors_amazon import ENABLEMENT_KEY + key = CONFIG_KEY if provider == "odoo" else ENABLEMENT_KEY + + def seal(current): + if not isinstance(current, dict) or current.get("initialSetup") != "selected": + raise ValueError("The first-connection selection was not saved.") + return {**current, "initialSetup": "configured"} + + rt.update(key, seal, flush="sync") + + +def selected_mirror_entities(rt): + """None preserves old sync policy; an explicit setup limits source pulls to selected dependencies.""" + import odoo_relational as rel + config = rt.get(rel.CONFIG_KEY) + if not isinstance(config, dict) or config.get("initialSetup") not in {"selected", "configured"}: + return None + grids = config.get("grids") + if not isinstance(grids, dict) or any(type(v) is not bool for v in grids.values()): + raise ValueError("The first-connection database selection is unavailable.") + import routes_odoo_tables as route + from harness import datastore + sources = route._odoo_sources() + entities = set() + for key, enabled in grids.items(): + if not enabled: + continue + if key in {"customer_data", "product_data"}: + entities.add({"customer_data": "res_partner", "product_data": "product_product"}[key]) + continue + if key not in sources: + continue + source = sources[key] + if source.get("table"): + entities.add(source["table"]) + entities.update(source.get("tables", {}).values()) + if source.get("needs_excluded"): + entities.add("res_partner") + return sorted(entities & set(datastore.ENTITIES)) + + +def mirror_cursor(rt, table_key=None): + """A selected source waits for its dependencies, without declaring the entire mirror ready.""" + from harness import datastore + if rt is None: + return datastore.ro_con() + rt.assert_datastore_matches() + entities = selected_mirror_entities(rt) + if entities is None: + return datastore.ro_con() + if table_key is not None: + import odoo_relational as rel + import routes_odoo_tables as route + config = rt.get(rel.CONFIG_KEY) + if not config.get("grids", {}).get(table_key, False): + raise RuntimeError("Enable this database in connection settings before reading it.") + source = route._odoo_sources().get(table_key) + if source is None: + return datastore.ro_con() + entities = set(source.get("tables", {}).values()) + if source.get("table"): + entities.add(source["table"]) + if source.get("needs_excluded"): + entities.add("res_partner") + if any(not datastore.entity_live(key) for key in entities): + raise RuntimeError("The selected database is completing its first sync. Try again shortly.") + # An empty first selection has no readers to run and need not create a mirror file. + return datastore.ro_cursor() if entities else None diff --git a/api/formula_runtime.py b/api/formula_runtime.py index ac372f93a3364a30724956a9b4640ab4eb29d483..7e5d203c54ccc908cacb0fe4107980cbede6cc97 100644 --- a/api/formula_runtime.py +++ b/api/formula_runtime.py @@ -3110,7 +3110,7 @@ class _SheetInputs(dict): self.engine._consume(super().get(SHEET_ADDR_KEY), key) owner = self.engine.spill_parents.get(key) if owner in self.engine._pending: raise _NeedReference(owner) - fault = key if key in self.engine.errors else None + fault = key if key in self.engine.errors and self.engine.errors[key] != "blank" else None if not _occupied(self.engine.specs.get(key)): producers = self.engine.prospective.at(key, self.engine.stats) pending = producers & self.engine._pending @@ -3123,6 +3123,8 @@ class _SheetInputs(dict): raise _ReferenceFault(fault) self.engine.stats["reads"] += 1 if self.format_stack: self.format_stack[-1].append(self.engine.formats.get(key)) + if self.engine.errors.get(key) == "blank": + return "" return self.engine.values.get(key, default) def range_values(self, bounds): @@ -3783,7 +3785,7 @@ def recompute_document(book, *, cache_key=None, changes=None, today=None): projected += 1 code = engine.errors.get(key) cell["value"] = engine.values.get(key) - if code: + if code and code != "blank": message = _sheet_error_message(code) cell.update(state="refused", display="", refusal={"code": code, "message": message}, error={"code": code, "message": message, @@ -3791,6 +3793,7 @@ def recompute_document(book, *, cache_key=None, changes=None, today=None): excel = {**REFERENCE_ERRORS, "no_match": "#N/A", "bad_value": "#VALUE!", "div_zero": "#DIV/0!", "numeric_error": "#NUM!"}.get(code) if excel: cell["error"]["excelCode"] = excel else: + if code == "blank": cell["value"] = "" cell.update(state="fresh", display=_display_text(cell["value"])) cell.pop("refusal", None) cell.pop("error", None) diff --git a/api/main.py b/api/main.py index a3eee6e65908f61b30514b4ac1ffb7d5111e83b8..f47bfedbbba24f3eb1f192f5041086d045442acf 100644 --- a/api/main.py +++ b/api/main.py @@ -89,6 +89,7 @@ from core import grid_events # noqa: E402 # ⚠ Neither is a `StoreUnavailable` subclass, on purpose: routes that degrade a store outage into a # session fallback must NOT degrade a refusal, or the user is told a change was saved that was not. from core import data_binding # noqa: E402 +from core import freeze_notice # noqa: E402 from core import store # noqa: E402 # W50-T28 clause 2 (C3 / R9) — the THIRD store refusal, and the only one of the three that is a # 409: a view write whose caller was looking at an older revision. See its handler below. @@ -182,6 +183,32 @@ def _store_unavailable(request: Request, exc: grid_events.StoreUnavailable): status_code=503) +@app.exception_handler(freeze_notice.DataInputFrozen) +def _data_input_frozen(request: Request, exc: freeze_notice.DataInputFrozen): + """Use the existing readable error envelope for every maintenance refusal.""" + return JSONResponse( + {"error": {"code": freeze_notice.ERROR_CODE, "message": freeze_notice.REFUSAL_TEXT}}, + status_code=freeze_notice.STATUS_CODE) + + +@app.middleware("http") +async def _freeze_response(request: Request, call_next): + """Keep a refused write readable even when a route catches its store error.""" + if request.method in {"GET", "HEAD", "OPTIONS"}: + return await call_next(request) + with freeze_notice.refusal_scope() as refused: + try: + response = await call_next(request) + except Exception: + if not refused[0]: + raise + return _data_input_frozen(request, freeze_notice.DataInputFrozen()) + if refused[0]: + # Replace the response, including any cookie minted from unpersisted auth state. + return _data_input_frozen(request, freeze_notice.DataInputFrozen()) + return response + + @app.exception_handler(data_binding.StoreWriteRefused) def _store_write_refused(request: Request, exc: data_binding.StoreWriteRefused): """D-315 — this deployment may not write that store. 503, with the REASON. @@ -266,6 +293,12 @@ def _view_revision_unverifiable(request: Request, exc: view_audit.RevisionUnveri status_code=503) +@app.get("/api/v1/freeze-notice") +def public_freeze_notice(): + """Report public maintenance state without resolving a session or store.""" + return JSONResponse(freeze_notice.notice(), headers={"Cache-Control": "no-store"}) + + @app.get("/api/health") def health(challenge: str = ""): """Unauthenticated LIVENESS only — it must answer before anyone can sign in, so it may not @@ -641,6 +674,14 @@ if _WEB_DIST.is_dir(): # default envelope; scoped users still pay their own scope's first build, once. # Env-gated rather than a startup event so importing `api.main` in a gate (verify_api and friends # run against fakes) can never fire a live Odoo pull. +def _freeze_holds(writer): + """Log each held writer before it can start work.""" + if not freeze_notice.enabled(): + return False + print(f"[aios-api] maintenance freeze holds {writer}") + return True + + def _seed_and_sync_store(): """Bring the analytical store (harness.datastore) LIVE for this container — the app.py bootstrap, mirrored (2026-07-31, owner item 1). @@ -656,6 +697,8 @@ def _seed_and_sync_store(): passes). Fail-quiet throughout: no seed/token/Odoo → the columns stay blank, the rows still serve. """ + if _freeze_holds("boot store sync"): + return import time as _t t0 = _t.time() try: @@ -669,9 +712,10 @@ def _seed_and_sync_store(): _odoo._tlocal.client = _odoo.OdooClient() except Exception: pass + selected_entities = _configured_mirror_entities() res = {} for i in range(12): - res = _ds.sync_all(log=lambda *a, **k: None) + res = _ds.sync_all(entities=selected_entities, log=lambda *a, **k: None) print(f"[aios-api] datastore sync pass {i + 1}: " + ", ".join(f"{k}={v.get('phase')}" for k, v in sorted(res.items()))) if all(v.get("phase") == "live" for v in res.values()): @@ -682,7 +726,7 @@ def _seed_and_sync_store(): # measure is served. MEASURED 2026-08-05: five deleted sale_order_line rows = $284.25 of # phantom YTD revenue, stable across re-syncs, zero the moment reconcile ran. try: - _ds.reconcile_deletes(log=lambda *a, **k: None) + _ds.reconcile_deletes(entities=selected_entities, log=lambda *a, **k: None) except Exception: pass print(f"[aios-api] datastore sync done in {_t.time() - t0:.1f}s " @@ -1007,11 +1051,14 @@ def _run_sync_requests(outcome=None): # transport. Do not borrow a request's cached XML-RPC client. odoo._tlocal.client = odoo.OdooClient(creds=source._creds) odoo._tlocal.tenant_ctx = None - report = datastore.sync_all(log=lambda *a, **k: None) + from connector_onboarding import selected_mirror_entities + selected_entities = selected_mirror_entities(rt) + report = datastore.sync_all(entities=selected_entities, + log=lambda *a, **k: None) finally: odoo._tlocal.client = previous_client odoo._tlocal.tenant_ctx = previous_context - if not report: + if not report and selected_entities != []: raise _SyncNowRefused( "Odoo did not run a sync. Check whether the connection is paused.") if any(row.get("phase") != "live" for row in report.values()): @@ -1844,7 +1891,13 @@ _neon_usage.set_guards(STORE_GUARDS) _startup_notes() -def _resync_mirror_pass(manual, passes): +def _configured_mirror_entities(tenant=None): + from harness import runtime + from connector_onboarding import selected_mirror_entities + return selected_mirror_entities(runtime.get_runtime(tenant or _resync_tenant()[0])) + + +def _resync_mirror_pass(manual, passes, entities=None): """ONE bounded mirror pass: DuckDB and the Odoo XML-RPC client, and nothing else. ⛔⛔ NO `Store`, NO `store_kv`, NO `user_tables` IN THIS BODY, AND THAT IS A CONTRACT RATHER @@ -1873,7 +1926,7 @@ def _resync_mirror_pass(manual, passes): # relational rebuild, which `manual` says nothing about. if manual: return None, None, None - sync_report = _ds.sync_all(log=lambda *a, **k: None) + sync_report = _ds.sync_all(entities=entities, log=lambda *a, **k: None) # Wave 21: purge hard-deleted rows the cursor sync cannot see (the boot pass's comment has # the measured case); without it deleted Odoo lines inflate every sum on the mirror FOREVER. # ⚠ INSIDE the non-manual branch: a reconcile is a pass over the mirror this loop was @@ -1881,7 +1934,7 @@ def _resync_mirror_pass(manual, passes): # HOTFIX 2026-09-29: EVERY pass, not every fourth. At a daily cadence a fourth pass is four # days, and the first pass after a reseed never reconciled at all. `reconcile_deletes` # compares counts first and walks ids only for an entity whose count moved. - delete_report = _ds.reconcile_deletes(log=lambda *a, **k: None) + delete_report = _ds.reconcile_deletes(entities=entities, log=lambda *a, **k: None) return sync_report, delete_report, None except Exception as e: # noqa: BLE001 # A partial report must never be mistaken for a confirmed zero-change pass. @@ -2019,6 +2072,8 @@ def _store_resync_loop(): """Keep the analytical mirror current — the API-process stand-in for the re-sync the Streamlit app piggybacks on page renders. Measure memos key on the pool stamp, so a refreshed pool re-reads the freshly synced store.""" + if _freeze_holds("store resync"): + return import time as _t from core import connector_sync passes = 0 @@ -2156,7 +2211,11 @@ def _store_resync_loop(): RESYNC_STATE["passes"] = passes if _odoo_requested is None: _odoo_requested = _run_sync_requests() - sync_report, delete_report, _err = _resync_mirror_pass(_manual, passes) + try: + selected_entities = _configured_mirror_entities(_cad["tenant"]) + sync_report, delete_report, _err = _resync_mirror_pass(_manual, passes, selected_entities) + except Exception as exc: + sync_report, delete_report, _err = None, None, _classify_resync_error(exc) source_fingerprints = _resync_source_fingerprints(sync_report) RESYNC_STATE["lastPassAt"] = _t.strftime("%Y-%m-%dT%H:%M:%SZ", _t.gmtime()) RESYNC_STATE["lastPassOk"] = _err is None @@ -2322,6 +2381,8 @@ def _store_resync_loop(): def _boot_app_migrations(why): """Apply tenant App retirement migrations independently of mirror sync and reads.""" + if _freeze_holds("app migrations"): + return try: import application_registry import timeseries_retirement @@ -2345,6 +2406,8 @@ def _boot_app_migrations(why): def _prewarm(): + if _freeze_holds("prewarm"): + return import time as _t t0 = _t.time() # The Odoo seed/sync can run for minutes and is separate from the finite cache warm below. @@ -2443,6 +2506,8 @@ def _provision_qa_sandbox(pg=None): does not open a PostgreSQL connection merely to learn it should refuse. Live has neither deployment flag and returns before importing the PostgreSQL backend. """ + if _freeze_holds("QA schema provisioning"): + return False if os.environ.get("AIOS_ENABLE_QA_TENANT") != "1": return False granted = {s.strip().lower() for s in @@ -2478,18 +2543,23 @@ except Exception as e: # noqa: BLE001 # never blocks health/readiness; the SQL procedure is idempotent, and `_provision_qa_sandbox` # itself refuses before opening a pool on every other environment. if (os.environ.get("AIOS_ENABLE_QA_TENANT") == "1" - and os.environ.get("STORE_BACKEND") == "pg"): + and os.environ.get("STORE_BACKEND") == "pg" + and not _freeze_holds("QA schema provisioning")): import threading as _threading_qa _threading_qa.Thread(target=_provision_qa_sandbox, daemon=True, name="qa-sandbox-schema").start() if (os.environ.get("AIOS_BOOT_APP_MIGRATIONS") != "0" - and (data_binding.is_space() or os.environ.get("AIOS_BOOT_APP_MIGRATIONS") == "1")): + and (data_binding.is_space() or os.environ.get("AIOS_BOOT_APP_MIGRATIONS") == "1") + and not _freeze_holds("app migrations")): import threading as _threading_apps _threading_apps.Thread(target=_boot_app_migrations, args=("boot",), daemon=True, name="app-migrations").start() -if os.environ.get("AIOS_PREWARM") == "1": +if os.environ.get("AIOS_BOOT_SYNC") == "1" and freeze_notice.enabled(): + _freeze_holds("boot store sync") + +if os.environ.get("AIOS_PREWARM") == "1" and not _freeze_holds("prewarm"): import threading as _threading _threading.Thread(target=_prewarm, daemon=True, name="prewarm").start() @@ -2524,6 +2594,6 @@ if os.environ.get("AIOS_PREWARM") == "1": # The other structural cost remains outside that skip policy: # * the whole-document store write itself. A partial-JSONB write is a wave. # ⚠ SO DO NOT PUT THIS BACK TO 3600 to "fix staleness" without closing one of those two first. -if os.environ.get("AIOS_STORE_RESYNC") == "1": +if os.environ.get("AIOS_STORE_RESYNC") == "1" and not _freeze_holds("store resync"): import threading as _threading_resync _threading_resync.Thread(target=_store_resync_loop, daemon=True, name="store-resync").start() diff --git a/api/odoo_purchasing.py b/api/odoo_purchasing.py new file mode 100644 index 0000000000000000000000000000000000000000..da9db5c89f28263dbfb5f5eac14549724f2b2195 --- /dev/null +++ b/api/odoo_purchasing.py @@ -0,0 +1,188 @@ +"""Read-only purchasing projections and source reconciliation for the Odoo connector.""" +from __future__ import annotations + +import datetime as dt +import math + +ORDERS_KEY = "ut_odoo_purchase_orders" +LINES_KEY = "ut_odoo_purchase_lines" +CONFIRMED = "po.state IN ('purchase','done')" + +MIRROR_ENTITIES = { + "purchase_order": { + "model": "purchase.order", "archivable": False, "optional": True, + "fields": ["name", "partner_id", "date_approve", "date_order", "state", + "amount_untaxed", "amount_total", "currency_id", "write_date"], + }, + "purchase_order_line": { + "model": "purchase.order.line", "archivable": False, "optional": True, + "fields": ["order_id", "product_id", "product_uom", "product_uom_qty", "product_qty", + "qty_received", "price_unit", "price_subtotal", "date_planned", "write_date"], + }, +} + + +def _text(value): + return str(value or "") + + +def _id(value): + return "" if value in (None, False, "") else int(value) + + +def _number(value): + return float(value or 0) + + +def _date(value): + return str(value or "")[:10] + + +def _fields(items): + return [{"source": "overlay", "default": True, + "automation": {"flowId": "odoo_relational", "preset": True}, **field} for field in items] + + +def order_fields(): + return _fields([ + {"key": "order_no", "label": "Purchase order", "type": "text", "pinned": True}, + {"key": "odoo_id", "label": "Odoo ID", "type": "int", "default": False, + "description": "The `purchase.order` id. Also this row's id."}, + {"key": "vendor", "label": "Vendor", "type": "text"}, + {"key": "vendor_id", "label": "Odoo vendor id", "type": "int", "default": False}, + {"key": "confirmation_date", "label": "Confirmation date", "type": "date"}, + {"key": "order_date", "label": "Order date", "type": "date"}, + {"key": "state", "label": "State", "type": "text"}, + {"key": "currency", "label": "Currency", "type": "text"}, + {"key": "amount_untaxed", "label": "Untaxed amount", "type": "currency", "agg": "sum"}, + {"key": "amount_total", "label": "Total amount", "type": "currency", "agg": "sum"}, + ]) + + +def line_fields(): + return _fields([ + {"key": "order_no", "label": "Purchase order", "type": "text", "pinned": True}, + {"key": "odoo_id", "label": "Odoo ID", "type": "int", "default": False, + "description": "The `purchase.order.line` id. Also this row's id."}, + {"key": "order_id", "label": "Odoo order id", "type": "int", "default": False}, + {"key": "vendor", "label": "Vendor", "type": "text"}, + {"key": "vendor_id", "label": "Odoo vendor id", "type": "int", "default": False}, + {"key": "confirmation_date", "label": "Confirmation date", "type": "date"}, + {"key": "state", "label": "State", "type": "text"}, + {"key": "currency", "label": "Currency", "type": "text"}, + {"key": "product", "label": "Product", "type": "text"}, + {"key": "product_id", "label": "Odoo product id", "type": "int", "default": False}, + {"key": "sku", "label": "SKU", "type": "text"}, + {"key": "uom", "label": "Product unit of measure", "type": "text"}, + {"key": "purchase_uom", "label": "Purchase unit of measure", "type": "text"}, + {"key": "standard_price", "label": "Standard cost", "type": "currency"}, + {"key": "category", "label": "Category", "type": "text"}, + {"key": "qty", "label": "Quantity in product units", "type": "int", "agg": "sum"}, + {"key": "purchase_qty", "label": "Quantity in purchase units", "type": "int", "agg": "sum"}, + {"key": "received_qty", "label": "Received in purchase units", "type": "int", "agg": "sum"}, + {"key": "price_unit", "label": "Unit price", "type": "currency"}, + {"key": "price_subtotal", "label": "Untaxed amount", "type": "currency", "agg": "sum"}, + {"key": "planned_date", "label": "Planned date", "type": "date"}, + ]) + + +def source_specs(): + common = { + "order_no": ("po.name", _text), "vendor": ("po.partner_name", _text), + "vendor_id": ("po.partner_id", _id), "confirmation_date": ("po.date_approve", _date), + "state": ("po.state", _text), "currency": ("po.currency_name", _text), + } + return { + ORDERS_KEY: { + "from_sql": "purchase_order po", "tables": {"po": "purchase_order"}, + "where": CONFIRMED, "id": "po.id", + "cols": {**common, "odoo_id": ("po.id", _id), "order_date": ("po.date_order", _date), + "amount_untaxed": ("po.amount_untaxed", _number), + "amount_total": ("po.amount_total", _number)}, + }, + LINES_KEY: { + "from_sql": "(purchase_order_line pol JOIN purchase_order po ON po.id = pol.order_id " + "LEFT JOIN product_product pp ON pp.id = pol.product_id)", + "tables": {"pol": "purchase_order_line", "po": "purchase_order", "pp": "product_product"}, + "where": CONFIRMED, "id": "pol.id", + "cols": {**common, "odoo_id": ("pol.id", _id), "order_id": ("pol.order_id", _id), + "product": ("pol.product_name", _text), "product_id": ("pol.product_id", _id), + "sku": ("pp.default_code", _text), "uom": ("pp.uom_name", _text), + "purchase_uom": ("pol.product_uom_name", _text), + "standard_price": ("pp.standard_price", _number), "category": ("pp.categ_name", _text), + "qty": ("pol.product_uom_qty", _number), "purchase_qty": ("pol.product_qty", _number), + "received_qty": ("pol.qty_received", _number), "price_unit": ("pol.price_unit", _number), + "price_subtotal": ("pol.price_subtotal", _number), "planned_date": ("pol.date_planned", _date)}, + }, + } + + +def _row_totals(client, model, domain, field): + last, count, total = 0, 0, 0.0 + while True: + rows = client.search_read(model, domain + [("id", ">", last)], ["id", field], + limit=2000, order="id asc") + if not rows: + return count, total + values = [] + for row in rows: + rid = int(row["id"]) + value = float(row.get(field) or 0) + if rid <= last or not math.isfinite(value): + raise ValueError("purchasing validation source did not advance or returned a non-finite amount") + last = rid + values.append(value) + count += len(rows) + total = math.fsum([total, *values]) + + +def validate(client, cur=None, month="2026-06"): + """Tie a closed confirmation month to independent Odoo line aggregates, without printing totals. + + A supplied cursor must be admitted for the same tenant/connection as the read-only client. + Without it, this validates live row totals only and makes no production-mirror claim. + """ + start = dt.date.fromisoformat(month + "-01") + end = (start.replace(day=28) + dt.timedelta(days=4)).replace(day=1) + if end > dt.date.today().replace(day=1): + raise ValueError("purchasing validation requires a closed month") + checks, totals = [], {} + plane = "mirror" if cur is not None else "live_rows" + periods = [("date_approve", ">=", start.isoformat()), ("date_approve", "<", end.isoformat())] + for key, model, field in ((ORDERS_KEY, "purchase.order", "amount_untaxed"), + (LINES_KEY, "purchase.order.line", "price_subtotal")): + prefix = "order_id." if key == LINES_KEY else "" + domain = [(prefix + "state", "in", ["purchase", "done"])] + domain += [(prefix + f, op, value) for f, op, value in periods] + try: + if cur is None: + count, total = _row_totals(client, model, domain, field) + else: + spec = source_specs()[key] + count, distinct, total = cur.execute( + "SELECT COUNT(*), COUNT(DISTINCT " + spec["id"] + "), COALESCE(SUM(" + + spec["cols"][field][0] + "), 0) FROM " + spec["from_sql"] + " WHERE " + + spec["where"] + " AND po.date_approve >= ? AND po.date_approve < ?", + [start.isoformat(), end.isoformat()]).fetchone() + if count != distinct: + raise ValueError("purchasing projection duplicated identities") + expected = int(client.search_count(model, domain)) + groups = client.read_group(model, domain, [field + ":sum"], + ["order_id" if key == LINES_KEY else "partner_id"], lazy=False) + summary = math.fsum(float(row.get(field) or 0) for row in groups) + checks.append({"check": key + " complete population", "ok": count == expected, + "observation": plane, "observed_rows": count, "source_rows": expected}) + checks.append({"check": key + " vs Odoo read_group", + "ok": math.isfinite(total) and math.isfinite(summary) + and abs(total - summary) <= 0.01, + "observation": plane}) + totals[key] = (total, summary, count) + except Exception as exc: + checks.append({"check": key + " reconciliation", "ok": False, + "error_type": type(exc).__name__, "observation": plane}) + if len(totals) == 2: + checks.append({"check": "confirmed purchase orders vs independent Odoo line total", + "ok": totals[ORDERS_KEY][2] > 0 and totals[LINES_KEY][2] > 0 + and abs(totals[ORDERS_KEY][0] - totals[LINES_KEY][1]) <= 0.01, + "observation": plane}) + return checks diff --git a/api/odoo_relational.py b/api/odoo_relational.py index 31eba800742e3ada06283b277d43cdc6b6bc1789..6b847396a48cc70dfc9e4f008b8edc2191eebb28 100644 --- a/api/odoo_relational.py +++ b/api/odoo_relational.py @@ -91,6 +91,14 @@ VENDORS_KEY = "ut_odoo_vendors" #: grid; that is the whole shape of the conversion. ORDER_LINES_KEY = "ut_odoo_order_lines" GL_LINES_KEY = "ut_odoo_gl_lines" +STOCK_MOVES_KEY = "ut_odoo_stock_moves" +STOCK_VALUATION_KEY = "ut_odoo_stock_valuation" +STOCK_QUANT_KEY = "ut_odoo_stock_quant" +from odoo_purchasing import (ORDERS_KEY as PURCHASE_ORDERS_KEY, LINES_KEY as PURCHASE_LINES_KEY, + order_fields as purchase_order_fields, + line_fields as purchase_line_fields, + source_specs as purchase_source_specs, + validate as validate_purchasing) #: The join column the partner-grain tables carry. Derived links resolve through it (`on`/`from`). JOIN_KEY = "partner_id" @@ -166,6 +174,7 @@ def _scope_field(): def _refreshed_field(): + """Materialized row stamp; the definition writer omits it on read-through tables.""" return {"key": "refreshed", "label": "Refreshed", "type": "date", "source": "overlay", "default": False, "description": "When this row was last reconciled against Odoo."} @@ -713,6 +722,69 @@ def gl_line_fields(): )] +def _stock_product_fields(): + """D-2632 product master columns, joined by product id including archived products.""" + return [ + {"key": "product", "label": "Product", "type": "text", "default": True}, + {"key": PRODUCT_JOIN_KEY, "label": "Odoo product id", "type": "int", "default": False}, + {"key": "sku", "label": "SKU", "type": "text", "default": True}, + {"key": "uom", "label": "Product unit of measure", "type": "text", "default": True}, + {"key": "standard_price", "label": "Standard cost", "type": "currency", "default": True}, + {"key": "category", "label": "Category", "type": "text", "default": True}, + ] + + +def _stock_location_fields(prefix, label): + return [ + {"key": prefix + "_id", "label": label + " id", "type": "int", "default": False}, + {"key": prefix, "label": label, "type": "text", "default": True}, + {"key": prefix + "_usage", "label": label + " usage", "type": "text", "default": True}, + {"key": prefix + "_fba", "label": label + " FBA", "type": "checkbox", "default": True}, + ] + + +def stock_move_fields(): + return [_preset({"source": "overlay", **field}) for field in ( + [{"key": "move", "label": "Stock move", "type": "text", "pinned": True, "default": True}, + {"key": "odoo_id", "label": "Odoo ID", "type": "int", "default": False, + "description": "The `stock.move` id. Also this row's id."}] + + _stock_product_fields() + + [{"key": "qty", "label": "Moved quantity", "type": "int", "agg": "sum", "default": True, + "description": "Quantity completed in the move's unit of measure."}, + {"key": "move_uom", "label": "Move unit of measure", "type": "text", "default": True}, + {"key": "date", "label": "Date", "type": "date", "default": True}, + {"key": "state", "label": "State", "type": "text", "default": True}, + {"key": "picking_id", "label": "Odoo picking id", "type": "int", "default": False}] + + _stock_location_fields("source_location", "Source location") + + _stock_location_fields("destination_location", "Destination location") + )] + + +def stock_valuation_fields(): + return [_preset({"source": "overlay", **field}) for field in ( + [{"key": "odoo_id", "label": "Odoo ID", "type": "int", "pinned": True, "default": True, + "description": "The `stock.valuation.layer` id. Also this row's id."}] + + _stock_product_fields() + + [{"key": "qty", "label": "Quantity", "type": "int", "agg": "sum", "default": True}, + {"key": "value", "label": "Value", "type": "currency", "agg": "sum", "default": True}, + {"key": "date", "label": "Date", "type": "date", "default": True}, + {"key": "stock_move_id", "label": "Odoo stock move id", "type": "int", "default": False}] + + _stock_location_fields("source_location", "Source location") + + _stock_location_fields("destination_location", "Destination location") + )] + + +def stock_quant_fields(): + return [_preset({"source": "overlay", **field}) for field in ( + [{"key": "odoo_id", "label": "Odoo ID", "type": "int", "pinned": True, "default": True, + "description": "The `stock.quant` id. Also this row's id."}] + + _stock_product_fields() + _stock_location_fields("location", "Location") + + [{"key": "qty", "label": "On hand", "type": "int", "agg": "sum", "default": True}, + {"key": "reserved_qty", "label": "Reserved quantity", "type": "int", + "agg": "sum", "default": True}] + )] + + def product_fields(): """One row per `product.product`, keyed on its id — EVERY product, archived ones included. @@ -1886,6 +1958,12 @@ TABLES = ( # wave's own [[reachable-is-not-the-same-as-built]] shape. ("order_lines", ORDER_LINES_KEY, "Odoo order lines", order_line_fields), ("gl_lines", GL_LINES_KEY, "Odoo GL lines", gl_line_fields), + # S70-T19: definitions only. Every stock population stays in the windowed mirror. + ("stock_moves", STOCK_MOVES_KEY, "Odoo stock moves", stock_move_fields), + ("stock_valuation", STOCK_VALUATION_KEY, "Odoo stock valuation", stock_valuation_fields), + ("stock_quant", STOCK_QUANT_KEY, "Odoo stock on hand", stock_quant_fields), + ("purchase_orders", PURCHASE_ORDERS_KEY, "Odoo purchase orders", purchase_order_fields), + ("purchase_lines", PURCHASE_LINES_KEY, "Odoo purchase order lines", purchase_line_fields), ) # The grid declarations above are the stable mirror contract, not Odoo's schema. T03 snapshots @@ -1895,10 +1973,14 @@ ODOO_TABLE_MODELS = { INVOICES_KEY: "account.move", ORDERS_KEY: "sale.order", AGENTS_KEY: "res.partner", ACCOUNTS_KEY: "account.account", VENDORS_KEY: "res.partner", BILLS_KEY: "account.move", ORDER_LINES_KEY: "sale.order.line", GL_LINES_KEY: "account.move.line", + STOCK_MOVES_KEY: "stock.move", STOCK_VALUATION_KEY: "stock.valuation.layer", + STOCK_QUANT_KEY: "stock.quant", + PURCHASE_ORDERS_KEY: "purchase.order", PURCHASE_LINES_KEY: "purchase.order.line", } # Shipments deliberately stay outside this map and TABLES: its consumers route mapped models # through the DuckDB refresh and suppress their discovered-model reads. There is no picking # mirror. The live shipment writer below stamps its model directly on the stored connector. +_ODOO_FIELD_ATTRIBUTES = ("string", "type", "required", "readonly", "relation", "selection") def authoritative_schema_snapshot(): @@ -1913,7 +1995,7 @@ def authoritative_schema_snapshot(): seen.add(model) try: models[model] = get_odoo().fields_get( - model, attributes=("string", "type", "required", "readonly", "relation")) + model, attributes=_ODOO_FIELD_ATTRIBUTES) except Exception as exc: errors.append(f"{model}: {type(exc).__name__}: {exc}") return models, errors @@ -1930,10 +2012,45 @@ MODEL_PUBLISH_SECONDS = 10 MODEL_CATALOG_TTL = _dt.timedelta(hours=6) _MODEL_FIELD_TYPES = { "boolean": "checkbox", "integer": "int", "float": "number", "monetary": "currency", - "date": "date", "datetime": "datetime", + "date": "date", "datetime": "datetime", "selection": "select", } +def _selection_options(meta): + """Odoo selection keys, not display labels or the values on a sampled page.""" + selection = meta.get("selection") if isinstance(meta, dict) else None + out = [] + if isinstance(selection, (list, tuple)): + for pair in selection: + if not isinstance(pair, (list, tuple)) or len(pair) != 2: + continue + value = pair[0] + if isinstance(value, bool) or not isinstance(value, (str, int)): + continue + key = str(value) + if key and key not in out: + out.append(key) + return out + + +def _missing_selection_metadata(row): + fields = (row or {}).get("fields") or {} + return any(isinstance(meta, dict) and meta.get("type") == "selection" + and not _selection_options(meta) for meta in fields.values()) + + +def _selection_field_contracts(fields, catalogue_row): + """Enrich curated columns without changing their row source, links or namespace.""" + metadata = (catalogue_row or {}).get("fields") or {} + out = [] + for field in fields: + meta = metadata.get(field["key"]) or {} + options = _selection_options(meta) if meta.get("type") == "selection" else [] + out.append({**field, "type": "select", "options": options, "choicesFrom": "odoo"} + if options else dict(field)) + return out + + def discover_readable_models(client): """Read-only introspection over an injected Odoo client. @@ -1962,7 +2079,7 @@ def discover_readable_models(client): continue try: fields = client.fields_get( - model, attributes=("string", "type", "required", "readonly", "relation")) + model, attributes=_ODOO_FIELD_ATTRIBUTES) except Exception as exc: # noqa: BLE001 fields = {} errors.append(f"{model}: fields: {type(exc).__name__}: {exc}") @@ -1970,9 +2087,7 @@ def discover_readable_models(client): for name, meta in (fields or {}).items(): if not str(name).strip() or not isinstance(meta, dict): continue - clean_fields[str(name)] = {k: meta.get(k) for k in - ("string", "type", "required", "readonly", "relation") - if k in meta} + clean_fields[str(name)] = {k: meta.get(k) for k in _ODOO_FIELD_ATTRIBUTES if k in meta} rows.append({"model": model, "name": " ".join(str((item or {}).get("name") or model).split()), "fields": clean_fields}) @@ -2005,7 +2120,8 @@ def readable_model_catalog(rt, connection_id=None): def refresh_readable_model_catalog(rt, connection_id=None, client=None, force=False): """Populate one tenant's catalogue once, or explicitly refresh it; never widen on failure.""" current = readable_model_catalog(rt, connection_id) - if current["capturedAt"] and not force: + if (current["capturedAt"] and not force + and not any(_missing_selection_metadata(row) for row in current["models"])): try: captured = _dt.datetime.fromisoformat(current["capturedAt"]) if _dt.datetime.now(_dt.timezone.utc) - captured < MODEL_CATALOG_TTL: @@ -2063,14 +2179,12 @@ def refresh_model_catalog_entry(rt, model, connection_id=None, client=None): if not bool(readable): return None fields = bound_client.fields_get( - model, attributes=("string", "type", "required", "readonly", "relation")) + model, attributes=_ODOO_FIELD_ATTRIBUTES) clean_fields = {} for name, meta in (fields if isinstance(fields, dict) else {}).items(): if not str(name).strip() or not isinstance(meta, dict): continue - clean_fields[str(name)] = {k: meta.get(k) for k in - ("string", "type", "required", "readonly", "relation") - if k in meta} + clean_fields[str(name)] = {k: meta.get(k) for k in _ODOO_FIELD_ATTRIBUTES if k in meta} return clean_fields try: @@ -2539,6 +2653,17 @@ ODOO_SPEC = ( "links": ((JOIN_KEY, CUSTOMERS_KEY, _ROW_ID_COL, _M2O), ("move_id", INVOICES_KEY, _ROW_ID_COL, _M2O), (ACCOUNT_JOIN_KEY, ACCOUNTS_KEY, ACCOUNT_JOIN_KEY, _M2O))}, + {"key": STOCK_MOVES_KEY, "source": "stock_move", "label": "Odoo stock moves", + "pinned": "move", "identity": (_ROW_ID_COL, "odoo_id"), "links": ()}, + {"key": STOCK_VALUATION_KEY, "source": "stock_valuation_layer", "label": "Odoo stock valuation", + "pinned": "odoo_id", "identity": (_ROW_ID_COL, "odoo_id"), "links": ()}, + {"key": STOCK_QUANT_KEY, "source": "stock_quant", "label": "Odoo stock on hand", + "pinned": "odoo_id", "identity": (_ROW_ID_COL, "odoo_id"), "links": ()}, + {"key": PURCHASE_ORDERS_KEY, "source": "purchase_order", "label": "Odoo purchase orders", + "pinned": "order_no", "identity": (_ROW_ID_COL, "odoo_id"), "links": ()}, + {"key": PURCHASE_LINES_KEY, "source": "purchase_order_line", "label": "Odoo purchase order lines", + "pinned": "order_no", "identity": (_ROW_ID_COL, "odoo_id"), + "links": (("order_id", PURCHASE_ORDERS_KEY, _ROW_ID_COL, _M2O),)}, ) @@ -2826,6 +2951,11 @@ TABLE_SUBJECTS = { BILLS_KEY: "odoo:account.move.vendor", ORDER_LINES_KEY: "odoo:sale.order.line", GL_LINES_KEY: "odoo:account.move.line", + STOCK_MOVES_KEY: "odoo:stock.move", + STOCK_VALUATION_KEY: "odoo:stock.valuation.layer", + STOCK_QUANT_KEY: "odoo:stock.quant.internal", + PURCHASE_ORDERS_KEY: "odoo:purchase.order.confirmed", + PURCHASE_LINES_KEY: "odoo:purchase.order.line.confirmed", } # One receipt identity per curated database. The model is not enough: invoices/bills share @@ -2840,6 +2970,11 @@ TABLE_TOPICS = { BILLS_KEY: "odoo_bills", ORDER_LINES_KEY: "sales_lines", GL_LINES_KEY: "gl_lines", + STOCK_MOVES_KEY: "odoo_stock_moves", + STOCK_VALUATION_KEY: "odoo_stock_valuation", + STOCK_QUANT_KEY: "odoo_stock_quant", + PURCHASE_ORDERS_KEY: "odoo_purchase_orders", + PURCHASE_LINES_KEY: "odoo_purchase_lines", } _SOURCE_COUNT_SPECS = { @@ -2860,6 +2995,14 @@ _SOURCE_COUNT_SPECS = { "order_lines": {"model": "sale.order.line", "mode": "count", "domain": [ ("order_id.state", "in", ["sale", "done"])]}, "gl_lines": {"model": "account.move.line", "mode": "count", "domain": []}, + "stock_moves": {"model": "stock.move", "mode": "count", "domain": []}, + "stock_valuation": {"model": "stock.valuation.layer", "mode": "count", "domain": []}, + "stock_quant": {"model": "stock.quant", "mode": "count", "domain": [ + ("location_id.usage", "=", "internal")]}, + "purchase_orders": {"model": "purchase.order", "mode": "count", "domain": [ + ("state", "in", ["purchase", "done"])]}, + "purchase_lines": {"model": "purchase.order.line", "mode": "count", "domain": [ + ("order_id.state", "in", ["purchase", "done"])]}, } # The two canonical registry databases replaced the retired ``ut_odoo_customers`` and @@ -3174,6 +3317,271 @@ _READERS = { READ_THROUGH_KEYS = frozenset(key for bucket, key, _l, _f in TABLES if bucket not in _READERS) +# S70-T19 integration exports. These are declarations, not an import-time registration seam. +# COMBINE-AMEND: copy the two new specs into harness.datastore.ENTITIES, merge the field additions +# into each existing entity's fields AND backfill_fields, and extend NUMERIC_FIELDS. Do not import +# this API module from harness or mutate ENTITIES at runtime. Stock move/location already exist. +STOCK_MIRROR_ENTITIES = { + "stock_valuation_layer": { + "model": "stock.valuation.layer", "archivable": False, "optional": True, + "fields": ["product_id", "quantity", "value", "create_date", "stock_move_id", "write_date"], + }, + "stock_quant": { + "model": "stock.quant", "archivable": False, "optional": True, + "fields": ["product_id", "location_id", "quantity", "reserved_quantity", "write_date"], + }, +} +STOCK_MIRROR_FIELD_ADDITIONS = { + "stock_move": ("name", "product_uom"), + "stock_location": ("parent_path",), + "product_product": ("uom_id",), +} +STOCK_MIRROR_NUMERIC_FIELDS = ("quantity", "value", "reserved_quantity") + + +def stock_mirror_entity_specs(): + """Detached source declarations for the integrator; this never modifies the datastore.""" + from copy import deepcopy + return deepcopy(STOCK_MIRROR_ENTITIES) + + +def _stock_text(value): + return str(value or "") + + +def _stock_id(value): + return "" if value in (None, False, "") else int(value) + + +def _stock_number(value): + # The existing quantity field dialect is `int`, but values remain fractional, as on order + # lines. Never cast a quantity to int or round signed valuation values during projection. + return float(value or 0.0) + + +def _stock_flag(value): + return "1" if value else "" + + +def _stock_product_cols(alias): + return { + "product": (alias + ".product_name", _stock_text), + PRODUCT_JOIN_KEY: (alias + ".product_id", _stock_id), + "sku": ("pp.default_code", _stock_text), + "uom": ("pp.uom_name", _stock_text), + "standard_price": ("pp.standard_price", _stock_number), + "category": ("pp.categ_name", _stock_text), + } + + +def _stock_location_cols(prefix, alias, id_sql): + # Find FBA in THIS mirror's location hierarchy. The reference tenant's root is 18423, but + # that id cannot define FBA in another tenant. Descendants include all nested internal stock. + fba_sql = ( + "EXISTS (SELECT 1 FROM stock_location fba WHERE fba.name = 'FBA' " + "AND fba.usage = 'view' AND strpos('/' || COALESCE(" + alias + ".parent_path, ''), " + "'/' || CAST(fba.id AS VARCHAR) || '/') > 0)") + return { + prefix + "_id": (id_sql, _stock_id), + prefix: (alias + ".complete_name", _stock_text), + prefix + "_usage": (alias + ".usage", _stock_text), + prefix + "_fba": (fba_sql, _stock_flag), + } + + +def stock_source_specs(): + """Windowed SQL bindings for routes_odoo_tables._odoo_sources to merge before its return. + + No route import, registry mutation, tenant cursor or store read occurs here. Existing route + admission, tenant assertions, connection ownership, row permissions and aggregate doors + remain the consumers' responsibility. A declaration alone does not make a grid readable. + """ + locations = (" LEFT JOIN stock_location sl ON sl.id = sm.location_id" + " LEFT JOIN stock_location sd ON sd.id = sm.location_dest_id") + move_cols = { + "move": ("sm.name", _stock_text), "odoo_id": ("sm.id", _stock_id), + **_stock_product_cols("sm"), + "qty": ("sm.quantity_done", _stock_number), + "move_uom": ("sm.product_uom_name", _stock_text), "date": ("sm.date", _as_date), + "state": ("sm.state", _stock_text), "picking_id": ("sm.picking_id", _stock_id), + **_stock_location_cols("source_location", "sl", "sm.location_id"), + **_stock_location_cols("destination_location", "sd", "sm.location_dest_id"), + } + # A mirror whose stock_location predates the parent_path backfill serves a blank FBA flag; the + # EXISTS over an absent column is a DuckDB Binder error, which reached Data as a bare 500. + no_ancestry = {"absent": (("stock_location", "parent_path"),)} + blank_fba = ("''", _stock_flag) + valuation_cols = { + "odoo_id": ("svl.id", _stock_id), **_stock_product_cols("svl"), + "qty": ("svl.quantity", _stock_number), "value": ("svl.value", _stock_number), + "date": ("svl.create_date", _as_date), "stock_move_id": ("svl.stock_move_id", _stock_id), + **_stock_location_cols("source_location", "sl", "sm.location_id"), + **_stock_location_cols("destination_location", "sd", "sm.location_dest_id"), + } + return { + STOCK_MOVES_KEY: { + "from_sql": "(stock_move sm LEFT JOIN product_product pp ON pp.id = sm.product_id" + + locations + ")", + "tables": {"sm": "stock_move", "pp": "product_product", "sl": "stock_location", + "sd": "stock_location"}, + "where": "", "id": "sm.id", "cols": move_cols, + "optional": ({**no_ancestry, "without": {"cols": { + "source_location_fba": blank_fba, "destination_location_fba": blank_fba}}},), + }, + STOCK_VALUATION_KEY: { + "from_sql": "(stock_valuation_layer svl LEFT JOIN product_product pp " + "ON pp.id = svl.product_id LEFT JOIN stock_move sm " + "ON sm.id = svl.stock_move_id" + locations + ")", + "tables": {"svl": "stock_valuation_layer", "pp": "product_product", "sm": "stock_move", + "sl": "stock_location", "sd": "stock_location"}, + "where": "", "id": "svl.id", "cols": valuation_cols, + "optional": ({**no_ancestry, "without": {"cols": { + "source_location_fba": blank_fba, "destination_location_fba": blank_fba}}},), + }, + STOCK_QUANT_KEY: { + "from_sql": "(stock_quant sq LEFT JOIN product_product pp ON pp.id = sq.product_id " + "LEFT JOIN stock_location sl ON sl.id = sq.location_id)", + "tables": {"sq": "stock_quant", "pp": "product_product", "sl": "stock_location"}, + "where": "sl.usage = 'internal'", "id": "sq.id", + "cols": {"odoo_id": ("sq.id", _stock_id), **_stock_product_cols("sq"), + **_stock_location_cols("location", "sl", "sq.location_id"), + "qty": ("sq.quantity", _stock_number), + "reserved_qty": ("sq.reserved_quantity", _stock_number)}, + "optional": ({**no_ancestry, "without": {"cols": {"location_fba": blank_fba}}},), + }, + } + + +def _stock_period(date_from, date_to, field): + start = _dt.date.fromisoformat(date_from) + end = _dt.date.fromisoformat(date_to) + if start > end: + raise Refused("the stock validation period is reversed") + return [(field, ">=", start.isoformat() + " 00:00:00"), + (field, "<=", end.isoformat() + " 23:59:59")] + + +def _stock_live_totals(client, model, domain, measures): + """Uncapped, bounded-memory row read; a transport page is never a population limit.""" + import math + last_id, count = 0, 0 + totals = {key: 0.0 for key in measures} + while True: + page = client.search_read(model, list(domain) + [("id", ">", last_id)], + ["id", *measures.values()], limit=2000, order="id asc") + if not page: + break + for row in page: + rid = _relation_id(row.get("id")) + if rid is None or rid <= last_id: + raise Refused("the stock validation source ids did not advance") + last_id = rid + count += len(page) + for key, field in measures.items(): + values = [float(row.get(field) or 0.0) for row in page] + if not all(math.isfinite(value) for value in values): + raise Refused("the stock validation source returned a non-finite value") + totals[key] = math.fsum([totals[key], *values]) + return count, totals + + +def _stock_mirror_totals(cur, spec, measures, extra_where="", params=()): + """Aggregate the exported observation projection, never the independent source summary.""" + expressions = ["COUNT(*)", "COUNT(DISTINCT " + spec["id"] + ")"] + expressions += ["COALESCE(SUM(" + spec["cols"][key][0] + "), 0)" for key in measures] + where = " AND ".join("(" + term + ")" for term in (spec["where"], extra_where) if term) + row = cur.execute("SELECT " + ", ".join(expressions) + " FROM " + spec["from_sql"] + + (" WHERE " + where if where else ""), params).fetchone() + if int(row[0]) != int(row[1]): + raise Refused("the stock mirror projection duplicated or lost record identities") + return int(row[0]), dict(zip(measures, (float(value) for value in row[2:]))) + + +def validate(client, cur=None, date_from="2025-07-01", date_to="2026-06-30"): + """Read-only stock reconciliation, using the caller's own Odoo client and optional mirror. + + The caller must lend a cursor admitted for the SAME tenant/default connection as the client. + This helper opens no global client/cursor and writes nothing. Without a cursor it validates + live row transformations only; that posture never claims Data, Sheet or mirror observation. + Results contain errors/counts, never source rows or absolute monetary totals. + """ + import math + specs = stock_source_specs() + checks = [] + plane = "mirror" if cur is not None else "live_rows" + + def tie(name, actual, expected, tolerance=1e-9, absolute=1e-6, nonempty=True): + difference = abs(actual - expected) + error = difference / abs(expected) if expected else (0.0 if difference == 0 else None) + ok = (nonempty and math.isfinite(actual) and math.isfinite(expected) + and (difference <= absolute or (error is not None and error <= tolerance))) + checks.append({"check": name, "ok": ok, "relative_error": error, "observation": plane}) + + period = _stock_period(date_from, date_to, "create_date") + move_period = _stock_period(date_from, date_to, "date") + cases = ( + (STOCK_MOVES_KEY, "stock.move", move_period, {"qty": "quantity_done"}, + "sm.date >= ? AND sm.date <= ?", tuple(term[2] for term in move_period)), + (STOCK_VALUATION_KEY, "stock.valuation.layer", period, {"qty": "quantity", "value": "value"}, + "svl.create_date >= ? AND svl.create_date <= ?", tuple(term[2] for term in period)), + (STOCK_QUANT_KEY, "stock.quant", [("location_id.usage", "=", "internal")], + {"qty": "quantity", "reserved_qty": "reserved_quantity"}, "", ()), + ) + for key, model, domain, measures, where, params in cases: + try: + n, totals = (_stock_mirror_totals(cur, specs[key], measures, where, params) + if cur is not None else _stock_live_totals(client, model, domain, measures)) + source_count = int(client.search_count(model, domain)) + checks.append({"check": key + " complete population", "ok": n == source_count, + "observed_rows": n, "source_rows": source_count, "observation": plane}) + groups = client.read_group(model, domain, + [field + ":sum" for field in measures.values()], + ["product_id"], lazy=False) + for field_key, field in measures.items(): + summary = math.fsum(float(row.get(field) or 0.0) for row in groups) + tie(key + " " + field_key + " vs Odoo read_group", totals[field_key], summary, + absolute=0.01 if field_key == "value" else 1e-6, + nonempty=bool(groups) or source_count == 0) + except Exception as exc: # noqa: BLE001 + checks.append({"check": key + " reconciliation", "ok": False, + "error_type": type(exc).__name__, "observation": plane}) + + try: + # Gross CUSTOMER-DESTINATION layers only. Customer-source returns remain in the full + # valuation tie above, but must never net against this GL cost tie (inventory-flow §1). + shipment_domain = period + [("stock_move_id.location_dest_id.usage", "=", "customer")] + n, shipment_totals = ( + _stock_mirror_totals(cur, specs[STOCK_VALUATION_KEY], {"value": "value"}, + "svl.create_date >= ? AND svl.create_date <= ? AND sd.usage = 'customer'", + tuple(term[2] for term in period)) if cur is not None else + _stock_live_totals(client, "stock.valuation.layer", shipment_domain, {"value": "value"})) + shipment_count = int(client.search_count("stock.valuation.layer", shipment_domain)) + checks.append({"check": "customer shipments complete population", + "ok": n == shipment_count and n > 0, "observed_rows": n, + "source_rows": shipment_count, "observation": plane}) + shipment_groups = client.read_group("stock.valuation.layer", shipment_domain, + ["value:sum"], ["product_id"], lazy=False) + tie("customer shipment value vs Odoo read_group", shipment_totals["value"], + math.fsum(float(row.get("value") or 0.0) for row in shipment_groups), + absolute=0.01, nonempty=n > 0 and bool(shipment_groups)) + accounts = client.search_read("account.account", [("code", "=", "600000")], ["id"], limit=0) + account_ids = [_relation_id(row.get("id")) for row in accounts] + account_ids = [rid for rid in account_ids if rid is not None] + if not account_ids: + raise Refused("the product cost GL account is unavailable") + gl_domain = [("account_id", "in", account_ids), ("parent_state", "=", "posted"), + ("date", ">=", date_from), ("date", "<=", date_to)] + gl_groups = client.read_group("account.move.line", gl_domain, + ["balance:sum"], ["account_id"], lazy=False) + gl_cost = math.fsum(float(row.get("balance") or 0.0) for row in gl_groups) + tie("customer shipments at cost vs GL 600000", -shipment_totals["value"], gl_cost, + tolerance=0.005, absolute=0.0, nonempty=n > 0 and bool(gl_groups) and gl_cost > 0) + except Exception as exc: # noqa: BLE001 + checks.append({"check": "customer shipments at cost vs GL 600000", "ok": False, + "error_type": type(exc).__name__, "observation": plane}) + return checks + + # ═════════════════════════════════════════════════════════════════════════════════════════════ # ⭐⭐ W54-T46 / R4 / R7 / CONTRACT C4 — A TABLE BELONGS TO EXACTLY ONE CONNECTION # ═════════════════════════════════════════════════════════════════════════════════════════════ @@ -4249,9 +4657,11 @@ def apply_plan(rt, built, username="automation", today=None, report=None, # keeps it. A caller that wants to SAY what was retired passes a dict; `refresh` does. retired = [] conn_label = connection_label(rt, connection_id) + catalogue = {row["model"]: row for row in readable_model_catalog(rt, connection_id)["models"]} plans = [(table_key_for(key, connection_id), table_label_for(label, conn_label), - namespaced_fields(fields(), connection_id), + _selection_field_contracts(namespaced_fields(fields(), connection_id), + catalogue.get(ODOO_TABLE_MODELS.get(key))), built[bucket]) for bucket, key, label, fields in TABLES if bucket in built] @@ -4380,6 +4790,17 @@ def _ensure_table_inplace(cur, key, label, fields, rows, username, stamp): # an empty grid. if is_read_through_key(key): table["readThrough"] = True + if table.get("readThrough") is True and str(key).startswith("ut_odoo_"): + # A definition publication date is not a mirror sync. Read-through definitions omit + # this machine column; materialized rows retain their per-pass reconciliation stamp. + def unbacked_refreshed(field): + return (field.get("key") == "refreshed" + and (field.get("automation") or {}).get("preset") + and not ut.user_edited(field)) + + fields = [field for field in fields if not unbacked_refreshed(field)] + table["fields"] = [field for field in (table.get("fields") or []) + if not unbacked_refreshed(field)] # W33-T41 — the claim, made DURABLE on the definition. Written on every pass for the same # reason `readThrough` is: it is derived from this module's own structure, so the code is what # it has to agree with, and a definition that lost the stamp should regain it rather than go @@ -4425,8 +4846,13 @@ def _ensure_table_inplace(cur, key, label, fields, rows, username, stamp): if _ut().user_edited(stored): continue if (stored.get("automation") or {}).get("preset"): - for prop in ("label", "type", "options", "link", "rollup", "description", + for prop in ("label", "type", "options", "choicesFrom", "link", "rollup", "description", "agg", "pinned", "default"): + # A subsequent row-only refresh must not replace authoritative selections with + # an older compiled vocabulary while its metadata catalogue is unavailable. + if (stored.get("choicesFrom") == "odoo" and field.get("choicesFrom") != "odoo" + and prop in ("type", "options", "choicesFrom")): + continue if prop in field: stored[prop] = field[prop] else: @@ -4465,6 +4891,8 @@ def _model_field_specs(catalogue_row): "label": str(meta.get("string") or raw_name), "type": _MODEL_FIELD_TYPES.get(raw_type, "text"), "source": "odoo", + **({"choicesFrom": "odoo", "options": _selection_options(meta)} + if raw_type == "selection" else {}), **({"description": "Binary content is not loaded into this database."} if raw_type == "binary" else {}), "automation": {"preset": True}}) @@ -4482,7 +4910,7 @@ def _model_field_specs(catalogue_row): def publish_database_schema(rt, table_key, connection_id=None, username="automation", today=None): """Publish one permission-visible database definition without row work. - Make at most two source calls on one model, on the miss path only. + Make at most two source calls on one model when fields or selection metadata are missing. Enable owns the choice and a durable sync job owns materialization. This seam only merges the schema, so retrying it preserves existing rows, authored fields and the navigation rename. """ @@ -4504,15 +4932,17 @@ def publish_database_schema(rt, table_key, connection_id=None, username="automat raw = (_ut().odoo_schema_snapshot(rt).get("models") or {}).get(model) if isinstance(raw, dict): row = {"model": model, "fields": raw} - if not row or not row.get("fields"): + if not row or not row.get("fields") or _missing_selection_metadata(row): catalogue = refresh_model_catalog_entry(rt, model, connection_id=namespace) row = next((row for row in catalogue["models"] if row.get("model") == model), None) if not row or not row.get("fields"): raise Refused("the field catalogue is unavailable for this database. Refresh the catalogue first.") + if _missing_selection_metadata(row): + raise Refused("Odoo selection choices are unavailable. Refresh the catalogue first.") raw_fields = _model_field_specs(row) declared = next((fields() for _bucket, known_key, _label, fields in TABLES if known_key == (base or table_key)), []) - fields = namespaced_fields(declared, namespace) + fields = _selection_field_contracts(namespaced_fields(declared, namespace), row) have = {field["key"] for field in fields} fields.extend(field for field in raw_fields if field["key"] not in have) from core import connector_sync @@ -4834,8 +5264,8 @@ def reconciliation_drift(receipt): def blocking_failures(database_failures, report): """The database failures that make a sync NOT good, PURE. - A reconciliation failure whose count drift is within `reconciliation_drift`'s tolerance is - recorded against its database and does not fail the connection; every other failure does. + A reconciliation failure within `reconciliation_drift`'s tolerance is informational unless + its receipt carries a source failure. Original counts stay intact; every other failure blocks. """ receipts = {} for name in ("databases", "canonicalDatabases"): @@ -4845,8 +5275,10 @@ def blocking_failures(database_failures, report): out = {} for key, failure in (database_failures or {}).items(): if isinstance(failure, dict) and failure.get("code") == "reconciliation_incomplete": - drift = reconciliation_drift(receipts.get(key)) - if drift is not None and drift[0] <= drift[1]: + receipt = receipts.get(key) + drift = reconciliation_drift(receipt) + if (drift is not None and drift[0] <= drift[1] + and not receipt.get("sourceError")): continue out[key] = failure return out @@ -4982,6 +5414,13 @@ def refresh_curated(rt, cur, buckets, *, connection_id=None, username="automatio return built, written, errors, receipts, report +def _canonical_mirror_ready(rt): + # Initial source selection does not make unrelated canonical totals authoritative. + from connector_onboarding import selected_mirror_entities + from harness import datastore + return rt is None or selected_mirror_entities(rt) is None or datastore.ready() + + def refresh(rt, tenant, username="automation", cur=None, today=None, connection_id=None, all_connections=False, table_keys=None, on_apply=None): """THE entry point — the store-resync path and the route both call this. @@ -5073,8 +5512,8 @@ def refresh(rt, tenant, username="automation", cur=None, today=None, connection_ if ns is not None: cur = None elif cur is None: - from harness import datastore - cur = datastore.ro_con() + from connector_onboarding import mirror_cursor + cur = mirror_cursor(rt) requested = set(table_keys) if table_keys is not None else None if requested is not None: for key in requested: @@ -5098,7 +5537,7 @@ def refresh(rt, tenant, username="automation", cur=None, today=None, connection_ built, written, database_errors, database_receipts, plan_report = refresh_curated( rt, cur, keep, connection_id=ns, username=username, today=today, retire=requested is None, on_apply=on_apply) - if ns is None and requested is None and cur is not None: + if ns is None and requested is None and cur is not None and _canonical_mirror_ready(rt): canonical_receipts = canonical_reconciliation_receipts(cur) else: # The binding selects this connection's verified read-only client. Authentication failure @@ -5112,7 +5551,7 @@ def refresh(rt, tenant, username="automation", cur=None, today=None, connection_ built, written, database_errors, database_receipts, plan_report = refresh_curated( rt, cur, keep, connection_id=ns, username=username, today=today, retire=requested is None, on_apply=on_apply, source_client=source_client) - if ns is None and requested is None and cur is not None: + if ns is None and requested is None and cur is not None and _canonical_mirror_ready(rt): canonical_receipts = canonical_reconciliation_receipts( cur, source_client=source_client) # ⭐ W33-T44: a table this pass REMOVED is reported, not inferred from a grid going missing. diff --git a/api/routes_activity.py b/api/routes_activity.py index c605438c0ecf22dd0a485092f48bd583feadf9df..4a446fe14df7fba5b9a2834ce8c22bde94d5e7fb 100644 --- a/api/routes_activity.py +++ b/api/routes_activity.py @@ -331,6 +331,9 @@ async def ingest_activity(request: Request, session: Session = Depends(require_s clean.append({**event, "eventId": uuid.uuid4().hex}) dropped = len(events) - len(clean) clean.sort(key=lambda event: event["ts"]) + from core import freeze_notice + if freeze_notice.enabled(): + return {"accepted": 0, "dropped": len(events)} if clean: try: await run_in_threadpool(_record, session, session_id, clean, diff --git a/api/routes_admin.py b/api/routes_admin.py index b473a632876eed0766909b2a478c4f8f0a7667bf..fe7fc73dc29fd765a702dbd23e42e2fd9e83ca75 100644 --- a/api/routes_admin.py +++ b/api/routes_admin.py @@ -72,7 +72,7 @@ def system_data_inspector(session: Session = Depends(require_session)): #: ⚠ DELIBERATELY PERMISSIVE, because the store key is the thing being constrained and not the #: deliverability of the address: one `@`, a dot-bearing domain, and the same character class the #: slug already trusts. A stricter grammar here would refuse addresses that exist. -_UNAME_EMAIL_RE = re.compile(r"^[a-z0-9][a-z0-9._%+-]*@[a-z0-9][a-z0-9.-]*\.[a-z]{2,24}$") +_UNAME_EMAIL_RE = re.compile(r"^[a-z0-9][a-z0-9._%+-]*@[a-z0-9][a-z0-9.-]*\.[a-z]{2,24}$", re.IGNORECASE | re.ASCII) #: RFC 5321's cap on a whole address. The slug form keeps its own 32 via `_UNAME_RE`. _UNAME_MAX = 254 #: Password policy lives in core.users so this route and self-service rotation cannot disagree. @@ -115,7 +115,7 @@ def _view(uname, rec, governed=None, st=None, surface_keys=None): resolved once for the whole request; the single-record routes pass neither, because their caller fetches the editor payload separately and `GET /perms` is the authority there. """ - return {"username": uname, + return {"username": rec.get("username_original") or uname, "name": rec.get("name") or uname, "role": rec.get("role", "user"), "bus": rec.get("bus", "all"), @@ -208,12 +208,15 @@ def _clean_username(v): accounts above are administrable today despite being unconstructable. Arming this validator on those routes would look like hardening and would break five live accounts. """ - uname = str(v or "").strip().lower() - if len(uname) > _UNAME_MAX or not (_UNAME_RE.match(uname) or _UNAME_EMAIL_RE.match(uname)): + uname = str(v or "").strip() + is_email = bool(_UNAME_EMAIL_RE.fullmatch(uname)) + if len(uname) > _UNAME_MAX or not (_UNAME_RE.fullmatch(uname.lower()) or is_email): raise err(400, "bad_username", "a username is either 2 to 32 characters of lowercase letters, digits, dot, " "dash or underscore, or a full email address of up to 254 characters") - return uname + # D-385: validation preserves an email's spelling. The account registry still owns + # canonical login/workspace keys; changing that storage contract requires core.users. + return uname if is_email else uname.lower() def _clean_password(v, username=None, name=None, email=None, record=None, @@ -334,7 +337,10 @@ def create_user(body: dict = Body(default=None), session: Session = Depends(admi """ body = body or {} _require_store() - uname = _clean_username(body.get("username")) + requested_username = _clean_username(body.get("username")) + # core.users.create_user and every account lookup below use a canonical key. Keep + # collision checks and readback on that same key; _view exposes original spelling. + uname = requested_username.lower() name = str(body.get("name") or "").strip() or uname email = str(body.get("email") or "").strip() or None pw = _clean_password(body.get("password"), username=uname, name=name, email=email) @@ -375,7 +381,7 @@ def create_user(body: dict = Body(default=None), session: Session = Depends(admi # Wave 18 (C1-TENANT): an admin creates accounts in their OWN company, never another's — # the tenant is stamped from the session, not taken from the body. try: - users.create_user(uname, pw, name, role=role, bus=bus, modules=modules, + users.create_user(requested_username, pw, name, role=role, bus=bus, modules=modules, agent=agent, email=email, tenant=_tenant_of(session.user)) except users.PasswordPolicyError as exc: raise err(400, "weak_password", "That password does not meet the policy: " + str(exc)) @@ -585,7 +591,9 @@ def _wall_database_name(key, session): return module["label"] try: from core import user_tables - label = ((user_tables.all_defs(session.runtime) or {}).get(key) or {}).get("label") + label = next((row.get("label") for row in user_tables.nav_entries( + viewer=session.uname, is_admin=session.admin, st=session.runtime) + if row.get("key") == key), None) except Exception: # noqa: BLE001 label = None return label.strip() if isinstance(label, str) and label.strip() else "This database" @@ -734,7 +742,7 @@ def _apply_admin_only_filter_rule(row): **{_FILTER_REASON_KEY: _ADMIN_ONLY_FILTER_REASON.format(owner=owner)}) -def _admin_usernames(): +def _admin_usernames(roster=None): """Every account holding the `admin` role, lowercased — or `None` when that set could not be established. @@ -753,7 +761,7 @@ def _admin_usernames(): answer. """ try: - reg = users.registry() or {} + reg = (users.registry() if roster is None else roster) or {} except Exception: # noqa: BLE001 return None if not isinstance(reg, dict) or not reg: @@ -808,7 +816,7 @@ def _field_provenance(src, key, session): classes = {} admins = None if session is not None: - admins = _admin_usernames() + admins = _admin_usernames(getattr(session, "_permission_roster", None)) try: # ⛔ `grant_topic` IS THE MODULE KEY, NOT THE STORE BUCKET. `shares.field_oid` namespaces # a grant by the topic the write doors already use — `"customer_data"` / @@ -840,7 +848,7 @@ def _field_provenance(src, key, session): return out -def _module_fields(key, session=None, wall_verdicts=None): +def _module_fields(key, session=None, wall_verdicts=None, vocabulary_memo=None): """The field schema the editor builds its pickers from — the SAME vocabulary the grid uses, so an admin filters on exactly what the user sees. @@ -901,7 +909,8 @@ def _module_fields(key, session=None, wall_verdicts=None): f"{key!r} is not a database in this workspace") src = defn.get("fields") or [] else: - provider = _static_field_providers().get(key) + providers = _static_field_providers() + provider = providers.get(key) if provider is None: # Reached only by a caller that skipped `_perm_modules`. Loud, because the alternative # (`[]`) is an empty option list read as an ANSWER — wave 26 item 24, `if ([])` is @@ -940,7 +949,14 @@ def _module_fields(key, session=None, wall_verdicts=None): if isinstance(_uf, dict) and _uf.get("key") and _uf["key"] not in _have: _have.add(_uf["key"]) src.append(_uf) - known = _server_side_vocabularies(session=session) + # Share even an incomplete vocabulary result across this request's databases. Failed + # sources stay omitted and retry on the next request; only successes enter process caches. + if vocabulary_memo is not None and "result" in vocabulary_memo: + known = vocabulary_memo["result"] + else: + known = _server_side_vocabularies(session=session) + if vocabulary_memo is not None: + vocabulary_memo["result"] = known out = [] for f in src: if not isinstance(f, dict) or not f.get("key"): @@ -1591,7 +1607,7 @@ def _stale_hidden_keys(key, hidden, session=None, fields=None): - {str(h) for h in (hidden or ()) if str(h).startswith(_mpre)}) -def _prune_stale_filters(block, session=None): +def _prune_stale_filters(block, session=None, fields_by_module=None): """`(block, {module: [dropped keys]})` — owner I16's cascade over a whole stored perms block. ⭐⭐ *"If the field is deleted, its permission filter goes with it."* Run at the ADMIN DOOR, @@ -1610,7 +1626,8 @@ def _prune_stale_filters(block, session=None): for key, entry in (block or {}).items(): if not isinstance(entry, dict) or not entry.get("filter"): continue - vocab = _prunable_vocabulary(key, session=session) + vocab = _prunable_vocabulary(key, session=session, + fields=(fields_by_module or {}).get(key)) if vocab is None: continue tree, gone = perm_scope.prune_filter_to_fields(entry["filter"], vocab) @@ -2195,6 +2212,19 @@ def get_perms(username: str, session: Session = Depends(admin_gate)): # 404, never 403, because "that account exists in another company" is itself a leak. raise err(404, "no_such_user", f"no account named {uname!r}") import core.perm_scope as perm_scope + from copy import copy + from core import user_tables + from routes_customers import _AssemblyStore + + # One read-only request snapshot: definitions, grants and provenance are shared by the + # picker and cascade. It never enters a write door or outlives this request. Global account + # writes and their required post-write readback below continue through the original store. + session = copy(session) + session._permission_roster = reg + try: + session.runtime = user_tables.lend_defs(_AssemblyStore(session.runtime, schema_only=True)) + except Exception: + raise err(503, "schema_unavailable", "The permission field definitions are unavailable. Try again.") rec = reg[uname] stored = rec.get("perms") or {} @@ -2216,7 +2246,9 @@ def get_perms(username: str, session: Session = Depends(admin_gate)): # ⭐ ONE `_module_fields` PASS, SHARED BY THE PICKER AND THE CASCADE. It used to be built # inline at the return; hoisted so the prune below reads the SAME vocabulary the payload # offers rather than paying a second store round-trip per database to ask again. - fields_by_module = {k: _module_fields(k, session=session) for k in governed} + vocabulary_memo = {} + fields_by_module = {k: _module_fields(k, session=session, vocabulary_memo=vocabulary_memo) + for k in governed} # ⭐⭐ W46-T34 / OWNER INSTRUCTION 6 — THE METRIC COUNT, STATED PER DATABASE. The rows are # already inside `fields_by_module` (they are `metric: true` members of the hide vocabulary); # what could not be read off them is the DIFFERENCE between a database with no measures and @@ -2242,7 +2274,8 @@ def get_perms(username: str, session: Session = Depends(admin_gate)): # `stored` test alone would very nearly cover it (an un-migrated record carries no perms # block), so the marker is asked explicitly rather than inferred from an empty dict. if stored and perm_scope.is_migrated(rec): - _pruned, _gone = _prune_stale_filters(stored, session=session) + _pruned, _gone = _prune_stale_filters(stored, session=session, + fields_by_module=fields_by_module) if _gone: users.set_access(uname, perms=_pruned) # Re-read rather than trust the write: this route's whole job is to report what is @@ -2346,33 +2379,51 @@ def get_perms(username: str, session: Session = Depends(admin_gate)): def _sync_database_shares(session, uname, before, after): - """S68-T05 finding: "Access on" for a connector `ut_*` database opens it; "Access off" closes it. + """R15/C5: explicit ut_* access grants editor authority; explicit off removes the share. Two doors answer two questions: the access rule decides WHICH rows, a `core.shares` `database` - grant decides IF the member sees the database at all, so the editor's toggle writes both. Only a - TRANSITION in this save touches a share: an entry that was already on keeps its grants exactly, - and a member who already holds a share (at any role) is never re-written or downgraded. + grant decides IF the member sees and edits it, so the editor's toggle writes both. Re-saving + an enabled entry repairs old view-only grants. Other principals, owners and provenance stay + intact; an omitted entry does not revoke an independently held share. """ - from core import shares - - def is_on(block, key): - entry = (block or {}).get(key) - return isinstance(entry, dict) and entry.get("access") is True + from core import shares, user_tables - for key in sorted(set(before or {}) | set(after or {})): - if not str(key).startswith("ut_"): - continue - was, now = is_on(before, key), is_on(after, key) - if was == now: - continue - record = shares.grants("database", key, st=session.runtime) + changes = [(key, entry["access"]) for key, entry in sorted((after or {}).items()) + if str(key).startswith("ut_") and isinstance(entry, dict) + and type(entry.get("access")) is bool] + if not changes: + return + try: + bucket = session.runtime.get(shares.SHARES_KEY, fresh=True) + except Exception as exc: + raise err(503, "store_unavailable", "Database access could not be saved.") from exc + if bucket is None: + bucket = {} + database_grants = bucket.get("database", {}) if isinstance(bucket, dict) else None + if not isinstance(database_grants, dict): + raise err(503, "store_unavailable", "Database access could not be saved.") + lent = user_tables.lend(session.runtime, **{shares.SHARES_KEY: bucket}) + + for key, now in changes: + raw = database_grants.get(key, {}) + if not isinstance(raw, dict) or not isinstance(raw.get("entries", []), list): + raise err(503, "store_unavailable", "Database access could not be saved.") + record = shares.grants("database", key, st=lent) + if len(record["entries"]) != len(raw.get("entries", [])): + raise err(503, "store_unavailable", "Database access could not be saved.") held = [e for e in record["entries"] if e.get("user") == uname] - if now and held: + if now and (record.get("owner") == uname or any(e.get("role") == "edit" for e in held)): + continue + if not now and not held: continue entries = [e for e in record["entries"] if e.get("user") != uname] if now: - entries.append({"user": uname, "role": "view"}) - shares.set_grants("database", key, entries, st=session.runtime, granter=session.uname) + entries.append({**(held[0] if held else {}), "user": uname, "role": "edit"}) + try: + shares.set_grants("database", key, entries, st=session.runtime, + granter=session.uname, expect=record["entries"]) + except shares.GrantsChanged as exc: + raise err(409, "shares_changed", "Database sharing changed. Save access again.") from exc @router.put("/admin/users/{username}/perms") @@ -2479,7 +2530,9 @@ def settings(session: Session = Depends(require_session)): would answer a question about our customer list. """ modules = perms.allowed_modules(session.user) + from core import freeze_notice return { + "freezeNotice": freeze_notice.notice(), # The build this Space is running. Behind the session on purpose — see `main.py::health`, # which refuses it for being the first URL a scanner finds. `deploy_web.py` stamps it as a # Space secret at every deploy; absent means "somebody started this container by hand". diff --git a/api/routes_alerts.py b/api/routes_alerts.py index 408b5c4045f202873ee51bf3d08d323ac3768183..f0fa45dfac888f91b7188a54c020b24107636a2e 100644 --- a/api/routes_alerts.py +++ b/api/routes_alerts.py @@ -646,7 +646,7 @@ def run_alert(alert_id: str, session: Session = Depends(require_session)): @router.get("/notifications") def notifications(session: Session = Depends(require_session)): - """The inbox — RE-EVALUATED on read, which is a deliberate design choice. + """The inbox re-evaluates on read unless maintenance freezes writes. ⭐ A-S1-2 RESOLVED THE OTHER WAY, and the reason is structural rather than a shortcut. The plan was a push hook: the automation engine calls `after_write` when it lands rows. But @@ -673,12 +673,14 @@ def notifications(session: Session = Depends(require_session)): feature. ⚠ Making the read cheap by evaluating LESS is the obvious wrong fix and is not what this does: every alert is still evaluated, against the same rows, in the same order. """ - assemblies = {} - for rec in alerts.list_alerts(user=session.uname, is_admin=False, st=session.runtime): - try: - _evaluate(session, rec, assemblies=assemblies) - except Exception: # noqa: BLE001 - continue # one bad alert must not empty the pane + from core import freeze_notice + if not freeze_notice.enabled(): + assemblies = {} + for rec in alerts.list_alerts(user=session.uname, is_admin=False, st=session.runtime): + try: + _evaluate(session, rec, assemblies=assemblies) + except Exception: # noqa: BLE001 + continue # one bad alert must not empty the pane # ⭐ W32-T20 (C3): every item leaves through `inbox_view`, so a notification queued before # this wave carries a `target` too. See `notification_view`'s header for why it is derived. box = inbox_view(alerts.inbox(session.uname, st=session.runtime)) diff --git a/api/routes_assets.py b/api/routes_assets.py index e2a00d11a2623d1d950221c6b6122772eee6206e..c51bb922bc50a971a3dcc8ec58f9ff3b35b5aed8 100644 --- a/api/routes_assets.py +++ b/api/routes_assets.py @@ -36,6 +36,7 @@ from fastapi import APIRouter from fastapi.responses import FileResponse from deps import Session, err, module_gate, require_session +from core import platform_admin from routes_admin import admin_gate import application_registry as applications import semantic_registry as semantic_rt @@ -420,8 +421,14 @@ def editorial_asset(slug: str, q: str = "web", def editorial_upload(body: dict = Body(default=None), session: Session = Depends(admin_gate)): """Store one editorial image: `{slug, data}` with `data` base64 (JSON keeps the API free - of a multipart dependency; an admin upload is rare enough to wear the +33%). The bytes - must OPEN as an image — a corrupt file is refused here, not discovered on a printed page.""" + of a multipart dependency; a platform-admin upload is rare enough to wear the +33%). + This library is global, so only a Loopable platform operator may publish into it. + The bytes must OPEN as an image — a corrupt file is refused here, not discovered on a + printed page.""" + if (not platform_admin.is_platform_admin(session.user) + or str(session.tenant or "").strip().lower() != platform_admin.PLATFORM_TENANT): + raise err(403, "platform_admin_only", + "editorial uploads require a Loopable platform administrator") body = body or {} slug = str(body.get("slug") or "").strip().lower() if not _SLUG_RE.match(slug): @@ -479,8 +486,8 @@ def editorial_upload(body: dict = Body(default=None), # ⛔ THREE WALLS, and each exists because of a specific way this could go wrong: # # 1. SESSION-GATED, NOT ADMIN-GATED. R7 is explicit — a non-admin attaches a picture to their -# own record. The editorial upload above IS admin-only because it publishes into Royal's -# shared catalogue library; this writes a per-record attachment, which is the same class of +# own record. The editorial upload above IS platform-admin-only because it publishes into +# the shared catalogue library; this writes a per-record attachment, which is the same class of # act as typing a note. # 2. TENANT FROM THE SESSION, NEVER FROM THE STRING. The stored address is `/` and # both halves of it are composed server-side. A `rec:` reference pasted from another diff --git a/api/routes_auth.py b/api/routes_auth.py index 9965f0978ebcfd3a93766f74e767be9478ce44c8..73d075df2e14e3b772e3b380ba0554bb6792b893 100644 --- a/api/routes_auth.py +++ b/api/routes_auth.py @@ -13,7 +13,7 @@ import re as _re from fastapi import APIRouter, Body, Depends, Request, Response import aios_session -from core import audit +from core import audit, freeze_notice from deps import Session, err, require_session, users, perms router = APIRouter(prefix="/api/v1/auth") @@ -310,7 +310,7 @@ def login(request: Request, response: Response, body: dict = Body(default=None)) _clear_failures(throttle_key) session_epoch = int(user.get("epoch") or 0) epoch_bumped = False - if not user.get("qa_runner"): + if not user.get("qa_runner") and not freeze_notice.enabled(): try: bumped = users.bump_epoch(user["username"]) if bumped is not None: diff --git a/api/routes_connectors.py b/api/routes_connectors.py index 2ca47d1c135f9e4aaf9de2a0e77845ea468ba2d6..d805c8ba83637d5d0f3dbca93f9eaa664757c83e 100644 --- a/api/routes_connectors.py +++ b/api/routes_connectors.py @@ -22,6 +22,8 @@ from __future__ import annotations import os import copy +import logging +import re import ssl import time import threading @@ -174,7 +176,9 @@ _DATABASE_SAFE_ERRORS = { "freshness_unconfirmed": "Freshness could not be confirmed for this database.", "sync_status_unavailable": "Sync status is unavailable for this database.", "sync_stalled": "This database sync is stalled.", + "amazon_report_refused": "Amazon refused this report. Check seller eligibility and connection access, then retry sync.", "sync_state_unreadable": "This database's saved sync state could not be read.", + "database_dispatch_failed": "Database sync could not start.", "maps_credential_missing": "Add the scraper credential in Keychain before choosing Run now.", "scraper_credential_missing": "Add the scraper credential in Keychain before choosing Run now.", "maps_unbounded_request": "Google Maps discovery requires an explicit bounded result cap.", @@ -326,11 +330,8 @@ TOKEN_CONNECTORS = [ "hint": "Create a Production app client in the Solution Provider Portal, self authorize " "it, then paste client_id, client_secret, refresh_token, seller_id and " "marketplace_id.", - # S66-T27: storing is offline (`keychain._test_amazon_entry` probes only on an explicit - # Test), so this no longer claims the key was verified before it was stored. - "note": "Choose Test in the Keychain tab to verify the credential live against your " - "marketplace participations. Bulk reads use the Reports API, so a backfill is one " - "document rather than a page loop."}, + # R22: status comes from the stored verification state, without mechanism copy. + "note": ""}, ] #: ⭐⭐ WAVE 31 · R7 — THE NANGO BREADTH REGISTRY. Owner: *"for Nango, i want to explore as many @@ -851,6 +852,110 @@ def _amazon_rotation_expired(deadline): return False +AI_CONTACT_CLIENTS = ( + ("claude", "Claude.ai", ("claude",)), + ("chatgpt", "ChatGPT", ("chatgpt", "chat gpt")), + ("claude-code", "Claude Code", ("claude code", "claude-code", "claude_code")), + ("codex", "Codex", ("codex",)), + ("cursor", "Cursor", ("cursor",)), +) + + +def _external_client_id(connection): + """Match registered client metadata, with specific names ahead of broad brand names.""" + declared = connection.get("clientId") or connection.get("client_id") + if declared in {client[0] for client in AI_CONTACT_CLIENTS}: + return declared + label = str(connection.get("label") or "").lower() + matches = [(len(alias), key) for key, _label, aliases in AI_CONTACT_CLIENTS + for alias in aliases if alias in label] + return max(matches, default=(0, ""))[1] + + +def _external_active(record): + if record.get("revoked_at") or record.get("disconnected_at"): + return False + expiry = record.get("expires_at") + if not expiry: + return True + try: + stamp = datetime.fromisoformat(str(expiry).replace("Z", "+00:00")) + return stamp.tzinfo is not None and stamp > datetime.now(timezone.utc) + except (TypeError, ValueError): + return False + + +def _contact_rows(rt, uname, is_admin, incomplete): + """Read existing channel and external metadata through their tenant/user walls. No writes.""" + rows = [ + {"key": "whatsapp", "label": "WhatsApp", "kind": "channel", "manage": "channel"}, + {"key": "imessage", "label": "iMessage", "kind": "channel", "manage": ""}, + {"key": "slack", "label": "Slack", "kind": "channel", "manage": "channel"}, + *[{"key": key, "label": label, "kind": "mcp", "manage": "external", + "clientId": key} for key, label, _aliases in AI_CONTACT_CLIENTS], + {"key": "loopable-api", "label": "Loopable API", "kind": "api", "manage": "external"}, + ] + for row in rows: + row.update(section="tools" if row["kind"] == "api" else "contact", + state="available", action="connect", desc="", scope="personal") + by_key = {row["key"]: row for row in rows} + by_key["imessage"].update(state="soon", action="soon") + + def read(source, keys, fn): + try: + fn() + except Exception: # noqa: BLE001 + for key in keys: + by_key[key]["state"] = "unavailable" + incomplete.append({"source": source, "cause": "Connection status unavailable.", + "recommendation": "Try again."}) + + def whatsapp(): + import whatsapp_identity + if whatsapp_identity.status(rt.key, uname).get("state") == "connected": + by_key["whatsapp"]["state"] = "connected" + + def slack(): + from routes_slack import _slack_entries, _slack_links, _team_links + installs = _slack_entries(rt) + links = _slack_links(rt) + if any(is_admin or uname in _team_links(links, fields["team_id"]).values() + for _row, fields in installs): + by_key["slack"]["state"] = "connected" + + def mcp(): + from core import external_oauth + for grant in external_oauth.list_connections(rt, uname, rt.key): + key = _external_client_id(grant) + if key and _external_active(grant): + by_key[key]["state"] = "connected" + + def api(): + from core import external_access + if any(_external_active(key) for key in external_access.list_for_user(rt, uname, rt.key)): + by_key["loopable-api"]["state"] = "connected" + + read("whatsapp", ["whatsapp"], whatsapp) + read("slack", ["slack"], slack) + read("mcp", [client[0] for client in AI_CONTACT_CLIENTS], mcp) + read("loopable-api", ["loopable-api"], api) + for row in rows: + if row["state"] == "connected": + row["action"] = "settings" + return rows + + +def _directory_card_action(row, is_admin): + """C2: one card action; paid operations remain separate and behind the settings door.""" + if row.get("action") not in (None, "connect", "settings", "soon"): + row["operation"] = row["action"] + held = row.get("state") in ("connected", "reconnect", "expired") + row["section"] = "tools" + row["action"] = "settings" if held else "connect" + row["actionDisabled"] = not (held or row.get("manage") + or (row.get("operation") and is_admin)) + + def directory(rt, uname, is_admin=False, incomplete=None): """The composed row set. Pure over the session, so the gate drives it without a request. @@ -899,10 +1004,19 @@ def directory(rt, uname, is_admin=False, incomplete=None): # would have flipped a real connection to "available" — the silent case W31-T44 exists to # prevent, re-introduced by the fix for its sibling. def _odoo(): + # S70-T37 (R4/D-202): a DISCONNECT freezes the tenant's mirror durably, and a frozen tenant + # reads "disconnected" even while Royal's environment credential is still present. The read + # is fresh and tenant-bound, so one tenant's freeze never shows on another tenant's card. + try: + cfg = rt.get("odoo_connector_config", fresh=True) + frozen = isinstance(cfg, dict) and bool(cfg.get("frozen")) + except Exception: # noqa: BLE001 + frozen = False rows.append({ "key": "odoo", "label": "Odoo", "kind": "builtin", "desc": "Sales, invoices, products and customers from your Odoo ERP.", - "state": ("connected" if ("odoo" in have or env_odoo_available(rt)) + "state": ("disconnected" if frozen + else "connected" if ("odoo" in have or env_odoo_available(rt)) else "available"), # W32-T11 / C1 — always business: Odoo is what the whole workspace's databases are # read through, which is why `routes_keychain.TENANT_WIDE_TYPES` refuses to make it @@ -998,13 +1112,9 @@ def directory(rt, uname, is_admin=False, incomplete=None): # note IS the generic sentence, so theirs is unchanged). "note": t["note"], }) - # S66-T27 — WHAT `reconnect` MEANS, said on the card. On Amazon it is "a key is stored - # but no passing Test is on record for it" (`keychain.entry_verified`), measured on - # staging's admin view with the data still syncing. The bare word read as a broken - # connection with no way to tell what to do. + # R22: a stored Amazon key without a passing test carries a short status only. if rows[-1]["state"] == "reconnect" and t["entry"] == "amazon_sp": - rows[-1]["needs"] = ("Amazon access for the stored key is not confirmed yet. " - "Choose Test in the Keychain tab, or replace the key.") + rows[-1]["needs"] = "Needs a test" contribute("token", _tokens, "the token-connector registry could not be composed", @@ -1036,6 +1146,7 @@ def directory(rt, uname, is_admin=False, incomplete=None): "key": "webhooks", "label": "Webhooks", "kind": "builtin", "desc": "Let another system start an automation by calling a URL.", "state": "connected", "manage": "automation", + "tutorial": _CONNECT_TUTORIALS["webhooks"], }) contribute("builtin", _builtin, "a built-in connector row could not be composed", @@ -1109,7 +1220,14 @@ def directory(rt, uname, is_admin=False, incomplete=None): for row in rows: row["allowedScopes"] = list(allowed_scopes(_entry_type_for(row.get("key")))) _safe_connector_status(row.get("key"), row) - return rows + _directory_card_action(row, is_admin) + contact = _contact_rows(rt, uname, is_admin, short) + for row in contact: + # Eligibility rides every card, Contact's included, or the toggle's filter has nothing to read. + row["allowedScopes"] = list(allowed_scopes(_entry_type_for(row.get("key")))) + reserved = {row["key"] for row in contact} + # Slack's old sign-in-provider listing must not become a second card for the same channel. + return contact + [row for row in rows if row.get("key") not in reserved] def _oauth_row(slug, meta, row_st): @@ -1274,7 +1392,8 @@ def connectors_directory(session: Session = Depends(require_session)): # `connectionKey` with `graphRefusal(selectedKey, ...)`, so a card with no envelope on the # wire renders exactly as it did before. The list comprehension holds the SAME dicts, so the # in-place attachment still lands on the native rows. - attach_relationship_graphs([r for r in rows if r.get("provider") != "monid"], + attach_relationship_graphs([r for r in rows if r.get("provider") != "monid" + and r.get("kind") not in ("channel", "mcp", "api")], session.user, session.runtime, session.uname, bool(session.admin)) # ⛔⛔ W49-T18 / C4 — THE ONE PLACE THE SERVER ROW BECOMES THE CLIENT DTO. `provider` @@ -1323,6 +1442,8 @@ def connectors_directory(session: Session = Depends(require_session)): _ODOO_SCHEMA_ENABLEMENTS = "odoo_schema_enablements" _ODOO_SYNC_STATUS = "odoo_sync_status" _ODOO_HEALTH_STATUS = "odoo_health_status_v1" +_DATABASE_DISPATCH_STATUS = "connector_database_dispatch_status_v1" +_DATABASE_JOBS_LOG = logging.getLogger(__name__) _ODOO_HEALTH_STAGES = ("source", "mirrorPull", "materialization", "reconciliation", "freshness") _SYNC_REQUEST_CONNECTIONS = {} _SYNC_REQUEST_CONNECTION_LOCK = threading.Lock() @@ -1624,6 +1745,8 @@ def odoo_health_receipt(rt, connection_id=None, *, now=None): except Exception: # noqa: BLE001 document = {} stored = dict(((document.get("connections") or {}).get(cid)) or {}) + dispatch = rt.get(_DATABASE_DISPATCH_STATUS) or {} + dispatch_failure = dispatch.get("failure") if dispatch.get("state") == "failed" else None stages = _empty_odoo_stages() stages.update({key: dict(value) for key, value in (stored.get("stages") or {}).items() if key in stages and isinstance(value, dict)}) @@ -1717,12 +1840,12 @@ def odoo_health_receipt(rt, connection_id=None, *, now=None): last_success = max(successes, default=stored.get("lastSuccess")) row_counts = [row.get("rowCount") for row in databases.values() if row.get("rowCount") is not None] - failure = (_odoo_stage_failure(stages) or stored.get("failure") or next( + failure = (dispatch_failure or _odoo_stage_failure(stages) or stored.get("failure") or next( (row.get("failure") for row in databases.values() if row.get("failure")), None)) return {"connectionId": cid, "cadenceSeconds": cadence, "lastSuccess": last_success, "currentLagSeconds": _lag_seconds(last_success, now), "rowCount": sum(row_counts) if row_counts else None, - "failure": failure, "stages": stages, + "failure": failure, "stages": stages, "dispatch": dispatch or None, "databases": [databases[key] for key in sorted(databases)]} @@ -1762,8 +1885,9 @@ def _sync_payload(connector_key, connection_id, row=None): def _odoo_result_database_failures(rt, connection_id, result): - """Map provider-reported model/table failures to their durable database keys.""" + """Map blocking provider failures to durable database keys without changing the report.""" import odoo_relational as rel + result = rel.health_view(result) try: choices = rel.grid_choices(rt, connection_id) except Exception: # noqa: BLE001 @@ -1807,7 +1931,7 @@ def _odoo_result_database_failures(rt, connection_id, result): failures[key] = _odoo_failure(raw, "materialization") elif limits: unmatched.append(str(limits)) - return failures, unmatched + return rel.blocking_failures(failures, result), unmatched def _odoo_sync_runner(session, connection_id): @@ -2074,15 +2198,16 @@ def _run_database_job(rt, candidate): username="automation", table_keys={key}, on_progress=lambda rows, total: connector_sync.progress( fenced, job, rows=rows, total=total))} - discovered = result.get("discovered") or {} + health = rel.health_view(result) + discovered = health.get("discovered") or {} published = {**(result.get("tables") or {}), **(discovered.get("tables") or {})} - receipt = (result.get("databases") or {}).get(key) + receipt = {**(result.get("databases") or {}), + **(result.get("canonicalDatabases") or {})}.get(key) receipt = receipt if isinstance(receipt, dict) else None - database_errors = result.get("databaseErrors") or {} - success = (key in published and not result.get("failed") and not result.get("frozen") - and not result.get("limits") and not discovered.get("errors") - and key not in database_errors - and (receipt is None or receipt.get("state") == "succeeded")) + database_errors, unmatched_errors = _odoo_result_database_failures(fenced, source_id, health) + success = (key in published and not health.get("failed") and not health.get("frozen") + and not health.get("limits") and not discovered.get("errors") + and key not in database_errors and not unmatched_errors) counts = published.get(key) if isinstance(published.get(key), dict) else {} nrows = (receipt.get("servingCount") if receipt is not None else counts.get("rows") if isinstance(counts, dict) else None) @@ -2229,9 +2354,25 @@ def _dispatch_database_jobs(rt): continue +def _record_database_dispatch(rt, exception_type=None): + """Keep a tenant-local dispatch receipt without changing queued jobs or source freshness.""" + if exception_type is None and (rt.get(_DATABASE_DISPATCH_STATUS) or {}).get("state") != "failed": + return # Healthy scans do not create a write every fifteen seconds. + failure = ({"code": "database_dispatch_failed", "type": exception_type, + "message": _DATABASE_SAFE_ERRORS["database_dispatch_failed"]} + if exception_type is not None else None) + receipt = {"state": "failed" if failure else "ready", + "at": datetime.now(timezone.utc).isoformat(), "failure": failure} + rt.update(_DATABASE_DISPATCH_STATUS, lambda _current: receipt, flush="sync") + + @router.on_event("startup") def _start_database_jobs(): """Discover durable work at boot, including tenants that have no active browser session.""" + from core import freeze_notice + if freeze_notice.enabled(): + print("[aios-api] maintenance freeze holds connector database jobs") + return global _materialization_thread def run(): global _materialization_thread @@ -2249,12 +2390,33 @@ def _start_database_jobs(): for tenant in tenants: if _materialization_stop.is_set(): break + rt = None try: - _dispatch_database_jobs(runtime.get_runtime(tenant)) - except Exception: + rt = runtime.get_runtime(tenant) + _dispatch_database_jobs(rt) + except Exception as exc: + exception_type = type(exc).__name__ + _DATABASE_JOBS_LOG.error( + "connector database dispatch failed: tenant=%s type=%s", + tenant, exception_type) + if rt is not None: + try: + _record_database_dispatch(rt, exception_type) + except Exception as receipt_exc: + _DATABASE_JOBS_LOG.error( + "connector database dispatch receipt failed: tenant=%s type=%s", + tenant, type(receipt_exc).__name__) continue # Durable queued/leased work is retried by the next scan. - except Exception: - pass + else: + try: + _record_database_dispatch(rt) + except Exception as receipt_exc: + _DATABASE_JOBS_LOG.error( + "connector database dispatch recovery receipt failed: tenant=%s type=%s", + tenant, type(receipt_exc).__name__) + except Exception as exc: + _DATABASE_JOBS_LOG.error( + "connector database tenant scan failed: type=%s", type(exc).__name__) _materialization_wake.wait(15) with _materialization_lifecycle: _materialization_stop.clear() @@ -2373,6 +2535,11 @@ def odoo_status(connection_id: str = None, session: Session = Depends(require_se lifecycle = connector_sync.read(session.runtime, "odoo", resolved_connection) durable = connector_sync.job_summary(_connection_jobs(session.runtime, "odoo", connection_id)) receipt = odoo_health_receipt(session.runtime, resolved_connection) + dispatch_failure = (receipt.get("dispatch") or {}).get("failure") + if dispatch_failure and durable: + durable = {**durable, "state": "error", + "errors": [dispatch_failure["message"], *(durable.get("errors") or [])][:20], + "errorsTotal": durable.get("errorsTotal", 0) + 1} if durable: return {"schedule": cfg.get("syncEvery"), "health": "paused" if cfg.get("frozen") else durable["state"], @@ -2391,6 +2558,10 @@ def odoo_status(connection_id: str = None, session: Session = Depends(require_se or "queued") errors_total = ((lifecycle or {}).get("errorsTotal", 0) if named or lifecycle else len(errors)) + if dispatch_failure: + shown_errors = [dispatch_failure["message"], *shown_errors] + shown_phase, state = "error", "error" + errors_total += 1 return {"schedule": cfg.get("syncEvery"), "health": "paused" if cfg.get("frozen") else shown_phase if named else "error" if shown_errors else shown_phase, @@ -2954,10 +3125,14 @@ _AMAZON_FIELD_LABELS = {"client_id": "Client ID", "client_secret": "Client secre "refresh_token": "Refresh token", "seller_id": "Seller ID", "marketplace_id": "Marketplace ID"} _CONNECT_FIELDS = { - "odoo": [{"key": "url", "label": "Server URL (https://...)", "secret": False}, - {"key": "db", "label": "Database", "secret": False}, - {"key": "user", "label": "API user", "secret": False}, - {"key": "api_key", "label": "API key", "secret": True}], + "odoo": [{"key": "url", "label": "Server URL", "secret": False, + "hint": "Your Odoo address, starting with https://."}, + {"key": "db", "label": "Database", "secret": False, + "hint": "The database name from your Odoo administrator."}, + {"key": "user", "label": "API user", "secret": False, + "hint": "The login of the user who created the API key."}, + {"key": "api_key", "label": "API key", "secret": True, + "hint": "The key from My profile, Account security, API keys."}], # ⭐ THE KEYS AND THEIR ORDER ARE DERIVED, NEVER TYPED. `amazon_client.CREDENTIAL_FIELDS` is # the one roster, and lane G's `verify_amazon` asserts this sequence against it, so a field # added there appears here rather than drifting. Labels are the one thing NOT derived: @@ -2967,15 +3142,86 @@ _CONNECT_FIELDS = { {"key": key, "label": _AMAZON_FIELD_LABELS.get(key, key.replace("_", " ").capitalize()), "secret": key in ("client_secret", "refresh_token"), + "hint": {"client_id": "The Production app's Login with Amazon client ID.", + "client_secret": "The same app's Login with Amazon client secret.", + "refresh_token": "The refresh token returned when you self-authorize the app.", + "seller_id": "Your selling partner ID from Seller Central.", + "marketplace_id": "The marketplace this seller account can access."}.get(key, ""), **({"default": amazon_client.US_MARKETPLACE_ID} if key == "marketplace_id" else {})} for key in amazon_client.CREDENTIAL_FIELDS ], + "stripe": [{"key": "api_key", "label": "API key", "secret": True, + "hint": "A restricted key with read access."}], + "shopify": [{"key": "api_key", "label": "Admin API token", "secret": True, + "hint": "The installed custom app's Admin API access token."}], + "meta_ads": [{"key": "api_key", "label": "Access token", "secret": True, + "hint": "A Marketing API token with ads_read for your ad account."}], } #: Everything else is one pasted token. The field name is `api_key` because that is the first of #: `core.keychain._PREVIEW_FIELDS`, so the stored entry gets a preview rather than a blank cell — #: the wave-31 defect that comment records, avoided rather than repeated. _CONNECT_TOKEN_FIELD = [{"key": "api_key", "label": "API key", "secret": True}] +# S70-T09: instructions accompany the canonical field descriptor. No credentials, probes or +# writers live here. Test descriptions reflect what core.keychain.test_entry actually checks. +_CONNECT_TUTORIALS = { + "odoo": { + "need": "An Odoo URL, database, user and API key.", + "steps": ["Copy your Odoo URL and ask your administrator for the database name.", + "Sign in as the API user. Open My profile, then Account security and API keys.", + "Create a key and copy it now. Keep this user's login with the key."], + "failureAction": "Open the server URL in a browser and confirm it is reachable. " + "Then check the database name and that the API key belongs to the API user.", + "testNote": "Checks server reachability only. It does not verify the user or API key.", + }, + "amazon_sp": { + "need": "A Production app client and access to your Amazon seller account.", + "steps": ["Open Solution Provider Portal, then Apps. Create or open your Production app.", + "Open its Login with Amazon credentials. Copy the client ID and client secret.", + "Self-authorize the app for your seller account and copy the refresh token.", + "In Seller Central, open Settings, Account info and copy your selling partner ID. " + "Choose the marketplace that account sells in."], + "failureAction": "In Solution Provider Portal, confirm the client ID and secret belong to " + "the same Production app. Self-authorize that app again for this seller " + "and check its marketplace access. Correct the saved key in Settings.", + "testNote": "Checks marketplace access. Saves the key before testing.", + }, + "stripe": { + "need": "Access to your Stripe dashboard's API keys.", + "steps": ["Open the Stripe dashboard, then Developers and API keys.", + "Create a restricted key with read access and copy it."], + "failureAction": "Open Stripe's API keys and copy a current restricted key with read access.", + "testNote": "Checks that a key is stored. Stripe access is not verified.", + }, + "shopify": { + "need": "Permission to create a custom app in your Shopify store.", + "steps": ["In your store admin, open Settings, Apps and sales channels, then Develop apps.", + "Create a custom app, configure its read permissions and install it.", + "Open API credentials and copy its Admin API access token."], + "failureAction": "Open the installed custom app's API credentials and copy its Admin API token.", + "testNote": "Checks that a token is stored. Shopify access is not verified.", + }, + "meta_ads": { + "need": "A Meta app and access to the ad account you want to read.", + "steps": ["Open Meta for Developers and select your app.", + "Use Graph API Explorer to get a Marketing API token with ads_read.", + "Confirm the token's user can access your ad account, then copy the token."], + "failureAction": "Generate a current Marketing API token with ads_read and confirm its " + "user has access to the ad account.", + "testNote": "Checks access to your ad accounts. Saves the token before testing.", + }, + "webhooks": { + "need": "An automation and a system that can send an HTTP POST.", + "steps": ["Go to Automations and create or open an automation.", + "Choose the Webhooks connector trigger. Saving it creates its token.", + "Use your Loopable host followed by /api/v1/automations/{automation_id}/hook/{token}. " + "Use this automation's ID and trigger token; keep the token private.", + "Paste that URL into your sending system."], + "failureAction": "Check the automation ID and token in the URL, then confirm the trigger is enabled.", + "testNote": "Sending to the hook runs the automation. Check its run history after an intentional send.", + }, +} + def _safe_connector_status(connector_key, payload): """Apply the provider's disclosure guard at the status boundary, outside fallback catches.""" @@ -3059,7 +3305,7 @@ def _odoo_database_sync_health(raw, enabled=True, *, now=None): row_count=row.get("rowCount"), error=failure, now=now) -def _amazon_database_sync_health(raw, enabled=True, *, now=None): +def _amazon_database_sync_health(raw, enabled=True, *, now=None, database_key=""): if not enabled: return _database_sync_health(enabled=False) if not isinstance(raw, dict): @@ -3067,7 +3313,15 @@ def _amazon_database_sync_health(raw, enabled=True, *, now=None): state="failed", error={"code": "sync_status_unavailable", "message": "Sync status is unavailable for this database."}) success = raw.get("lastSuccess") - if raw.get("stuck") is True: + table = str(database_key).removeprefix("ut_amzn_") + from harness.amazon_store import is_refusal_reason + refused = (raw.get("refused") is True or is_refusal_reason(raw.get("unknownCause")) or any(item.get("table") == table for item in + ((raw.get("firstLoad") or {}).get("refused") or ()) if isinstance(item, dict)) + ) + if refused: + state = "partial" if success else "failed" + error = {"code": "amazon_report_refused"} + elif raw.get("stuck") is True: state = "partial" if success else "failed" error = {"code": "sync_stalled", "message": "This database sync is stalled."} @@ -3108,8 +3362,18 @@ def _detail_connect(entry_type, is_admin): return None fields = list(_CONNECT_FIELDS.get(entry_type) or _CONNECT_TOKEN_FIELD) tenant_wide = entry_type in TENANT_WIDE_TYPES + hint = "" + try: + hint = next((str(token.get("hint") or "") for token in TOKEN_CONNECTORS + if token.get("entry") == entry_type), "") + except Exception: # A failed token registry must not take away Odoo's setup descriptor. + pass + from connector_onboarding import choices return {"type": entry_type, + "initialDatabases": choices(entry_type), "fields": [dict(field) for field in fields], + "hint": hint, + "tutorial": _CONNECT_TUTORIALS.get(entry_type), # A member may always store a personal one; business-wide is an administrator's to # give (R4), and a tenant-wide type is business-wide by construction. "scopes": (["business"] if tenant_wide @@ -3756,6 +4020,9 @@ def _detail_databases(rt, is_admin, connector_key, uname="", connection_id=None, "databaseKey": key, "state": "unknown", "rowCount": None, "lastSuccess": None, "currentLagSeconds": None, "recordRefreshed": None, "failure": None, "stages": _empty_odoo_stages()} + dispatch_failure = (health.get("dispatch") or {}).get("failure") + if choice.get("enabled") and dispatch_failure: + database_health = {**database_health, "failure": dispatch_failure} out.append({"key": key, "identity": connector_sync.database_identity( "odoo", resolved_connection_id, key, model), @@ -3881,6 +4148,42 @@ def _amazon_databases(rt, is_admin, spec=None): # what `native` carries: the vendor's own name for the thing, never one we invented. It is # empty for a table whose report type is unknown, and the tab says so rather than guessing. raw_sync = (states or {}).get(name) if is_admin else None + if isinstance(raw_sync, dict): + from harness.amazon_store import plain_refusal_reason, is_refusal_reason + raw_sync = dict(raw_sync) + raw_sync["refused"] = (raw_sync.get("refused") is True + or is_refusal_reason(raw_sync.get("unknownCause"))) + load = raw_sync.get("firstLoad") + diagnostics = {} + if isinstance(load, dict) and isinstance(load.get("refused"), list): + reports = [] + for item in load["refused"]: + if not isinstance(item, dict): + continue + reason = str(item.get("reason") or "") + reports.append({**item, "reason": plain_refusal_reason( + reason, status=item.get("amazonStatus"), table=item.get("table", "")), + "diagnostic": item.get("diagnostic") or reason}) + if item.get("table") == name: + raw_sync["refused"] = True + diagnostics["refusal"] = item.get("diagnostic") or reason + previous_cause = str(raw_sync.get("unknownCause") or "").strip() + if (previous_cause and reason.strip() + and (previous_cause.startswith(reason.strip()) + or reason.strip().startswith(previous_cause))): + diagnostics["unknownCause"] = previous_cause + raw_sync["unknownCause"] = reports[-1]["reason"] + raw_sync["firstLoad"] = {**load, "refused": reports} + cause = str(raw_sync.get("unknownCause") or "").strip() + own = next((item for item in ((raw_sync.get("firstLoad") or {}).get("refused") or ()) + if item.get("table") == name), {}) + if cause and ((raw_sync["refused"] and not own) or is_refusal_reason(cause) or re.search( + r"REFUSED:|FATAL|\b\w*(?:Error|Exception)\b|GET_[A-Z_]+|[{}]|SQLSTATE|SELECT\s|report[ _-]?id\b", cause, re.I)): + diagnostics["unknownCause"] = cause + raw_sync["unknownCause"] = plain_refusal_reason( + cause, status=own.get("amazonStatus"), table=name) + if diagnostics: + raw_sync["diagnostics"] = diagnostics is_enabled = enabled.get(key) is True out.append({"key": key, "label": str(declared.get("label") or LABELS_AMAZON.get(key) or key), @@ -3892,7 +4195,7 @@ def _amazon_databases(rt, is_admin, spec=None): # definition untouched: only this provider config document changes. "enabled": is_enabled, **({"sync": raw_sync} if is_admin else {}), - "syncHealth": _amazon_database_sync_health(raw_sync, is_enabled), + "syncHealth": _amazon_database_sync_health(raw_sync, is_enabled, database_key=key), "fields": fields, "excluded": excluded, "sharesFieldsWith": [k for k in by_report.get(rep, ()) if k != key]}) diff --git a/api/routes_customers.py b/api/routes_customers.py index f385a34468a2c66c213336cbbeac992fe0c77359..e54c4d8b5b3581c65240c284f1d41aa644cf8a0d 100644 --- a/api/routes_customers.py +++ b/api/routes_customers.py @@ -22,7 +22,7 @@ owner-blocked on B-3), and no test here may claim read-your-writes ACROSS runtim proof is single-process and would report green on exactly the thing that is still broken. """ import datetime as _dt -from copy import deepcopy +from copy import copy, deepcopy from dataclasses import is_dataclass, replace import hashlib import json @@ -70,16 +70,42 @@ def _assembly_pg_store(st): return handle if isinstance(handle, store_pg.PgStore) else None -def _assembly_revisions(runtime, names, uses_roster=False): +def _assembly_roster_stamp(tenant): + """Only permission and visible identity changes invalidate workspace metadata.""" + from core import users + from routes_admin import _tenant_of + roster = users.registry() + if not isinstance(roster, dict) or not roster: + # A failed roster read must never validate a previously permitted answer. + return time.time_ns() + keys = ("tenant", "name", "email", "avatar", "active", "role", "bus", "modules", + "agent", "perms", "perms_v", "platform_admin") + visible = {str(name): {key: record.get(key) for key in keys} + for name, record in roster.items() + if isinstance(record, dict) and _tenant_of(record) == tenant} + return hashlib.sha256(json.dumps(visible, sort_keys=True, separators=(",", ":"), + default=str).encode("utf-8")).hexdigest() + + +def _assembly_revisions(runtime, names, uses_roster=False, roster_tenant=None, roster_memo=None): """Validate all tracked PostgreSQL documents in one round trip, with identical tokens.""" from core import store, store_pg names = tuple(sorted(set(names))) handle = _assembly_pg_store(runtime) users_stamp = None + + def roster_stamp(revision): + token = json.dumps(revision, sort_keys=True, default=str) + if roster_memo is None: + return _assembly_roster_stamp(roster_tenant) + if token not in roster_memo: + roster_memo[token] = _assembly_roster_stamp(roster_tenant) + return roster_memo[token] + if handle is None: tokens = {name: _assembly_revision(runtime, name) for name in names} if uses_roster: - users_stamp = _assembly_revision(store, "users") + users_stamp = roster_stamp(_assembly_revision(store, "users")) return tokens, users_stamp key = getattr(runtime, "store_key", str) @@ -111,8 +137,8 @@ def _assembly_revisions(runtime, names, uses_roster=False): absent = {"rev": 0, "updated_at": None, "token": "pg:0"} tokens = {name: dict(found.get(mapped, absent)) for name, mapped in keyed.items()} if uses_roster: - users_stamp = (dict(found.get(roster_key, absent)) if same_roster - else _assembly_revision(store, "users")) + users_stamp = roster_stamp(dict(found.get(roster_key, absent)) if same_roster + else _assembly_revision(store, "users")) if isinstance(runtime, _AssemblyStore): # The page may read these documents after its memo hit. Reuse this request's pre-read # tokens without retaining any document bytes across requests. @@ -269,13 +295,17 @@ def shared_fields(st=None): """ from core import field_permissions, shared_overlay try: + document = shared_overlay.document(_shared_key(), st=st) if getattr(st, "schema_only", False) else None field_permissions.migrate_legacy_fields( - _shared_key(), st=st, grant_topic="customer_data", shared_key=_shared_key()) - fields = shared_overlay.fields(_shared_key(), st=st) + _shared_key(), st=st, grant_topic="customer_data", shared_key=_shared_key(), + shared_document=document) + fields = shared_overlay.fields(_shared_key(), st=st, document=document) # Normalized Routes use an invisible field-shaped bridge so the established Share dialog # and grant registry can be reused. It is object metadata, never a customer column. return {key: value for key, value in fields.items() if not _is_route_share_bridge(value)} + except shared_overlay.SchemaUnavailable: + raise except Exception: # noqa: BLE001 # Lenient like every other display read: an unreachable store degrades to "nothing is # shared yet", never to a 500 on a grid that would otherwise render. The WALL does not @@ -385,8 +415,9 @@ def _mark_offerability(fields): class _AssemblyStore: """One build's borrowed reads and their pre-read revisions, never a second cache.""" - def __init__(self, runtime): + def __init__(self, runtime, *, schema_only=False): self._runtime = runtime + self.schema_only = bool(schema_only) self.docs, self.tokens = {}, {} self._validated_tokens = {} # A lent/test reader without its own coherent revision door can share a flight only. @@ -408,12 +439,20 @@ class _AssemblyStore: except AttributeError: self.cacheable = False key = (name, shape) - if fresh or not self.cacheable or key not in self.docs: + if fresh or (not self.cacheable and not self.schema_only) or key not in self.docs: self.docs[key] = read() # TableStore readers can mutate nested projections; lend an independent read each time. return deepcopy(self.docs[key]) def get(self, name, fresh=False): + if self.schema_only and fresh: + self.docs = {key: value for key, value in self.docs.items() if key[0] != name} + if self.schema_only and name.endswith("_table_workspace"): + return self._read(name, ("projection", ("overlays",)), + lambda: self._runtime.get_projection(name, drop=("overlays",)), fresh=fresh) + if self.schema_only and name.endswith("__shared"): + from core import shared_overlay + return shared_overlay.document(name[:-len("__shared")], st=self) return self._read(name, "whole", lambda: ( self._runtime.get(name, fresh=True) if fresh else self._runtime.get(name)), fresh=fresh) @@ -422,10 +461,17 @@ class _AssemblyStore: lambda: self._runtime.get_projection(name, drop=drop)) def get_entry(self, name, entry): + if self.schema_only: + backend = self._runtime._store() if callable(getattr(self._runtime, "_store", None)) else self._runtime + if not callable(getattr(backend, "get_entry", None)): + from core.shared_overlay import SchemaUnavailable + raise SchemaUnavailable("field definitions cannot be read without loading row cells") return self._read(name, ("entry", str(entry)), lambda: self._runtime.get_entry(name, entry)) def _read_strict(self, name): + if self.schema_only and name.endswith("_table_workspace"): + return self.get_projection(name, drop=("overlays",)) reader = getattr(self._runtime, "_read_strict", self._runtime.get) return self._read(name, "strict", lambda: reader(name)) @@ -438,11 +484,17 @@ class _AssemblyStore: return token def update(self, name, *args, **kwargs): + if self.schema_only: + from core.grid_events import StoreUnavailable + raise StoreUnavailable("field definitions need a full workspace refresh before they can be read") self.cacheable = False self.docs = {key: value for key, value in self.docs.items() if key[0] != name} return self._runtime.update(name, *args, **kwargs) def put(self, name, *args, **kwargs): + if self.schema_only: + from core.grid_events import StoreUnavailable + raise StoreUnavailable("field definitions need a full workspace refresh before they can be read") self.cacheable = False self.docs = {key: value for key, value in self.docs.items() if key[0] != name} return self._runtime.put(name, *args, **kwargs) @@ -461,26 +513,33 @@ def _wall_stamp(session, table_key): def grid_assembly(session: Session, scope: str = "customer", storage_key: str = "", - consume_corrections: bool = False, *, build=None, pool_stamp=None): + consume_corrections: bool = False, *, build=None, pool_stamp=None, + with_rows: bool = True, needed=None): """C2: one wall- and revision-scoped memo for customer, product and stored envelopes.""" global grid_assembly_shared rt = session.runtime + needed = None if needed is None else frozenset(str(key) for key in needed) if build is None: - build = lambda borrowed: _compute_grid_assembly(borrowed, scope, storage_key, False) + build = lambda borrowed: _compute_grid_assembly( + borrowed, scope, storage_key, False, with_rows=with_rows, needed=needed) if pool_stamp is None: team_id, agent = _team_agent(session) - pool_stamp = lambda: _pool_stamp(rt, team_id, agent) + pool_stamp = lambda: _pool_stamp(rt, team_id, agent) if with_rows else 0 table_key = scope if scope.startswith("ut_") else ( "product_data" if scope == "product" else MODULE) + # Pre/post-build validation shares one roster read only while its revision is unchanged. + roster_memo = {} def base_key(revisions=()): # _field_usage reads users.registry() from this global account store, including on pg. - uses_roster = scope != "product" + uses_roster = scope != "product" and not getattr(session, "user", {}).get("qa_runner") prefix = ("workspace_assembly", getattr(session, "tenant", None), scope, getattr(session, "uname", None), getattr(session, "admin", False), json.dumps(getattr(session, "user", {}), sort_keys=True, separators=(",", ":"), default=str), - _wall_stamp(session, table_key) if memoable else None, pool_stamp()) + _wall_stamp(session, table_key) if memoable else None, pool_stamp(), + ("full" if needed is None else ("columns", tuple(sorted(needed)))) + if with_rows else "fields") if not memoable: result = ((*prefix, json.dumps(None)), {}) return result @@ -489,7 +548,8 @@ def grid_assembly(session: Session, scope: str = "customer", storage_key: str = for memo_key in list(rt.pool_cache): if memo_key[:-2] == prefix: names.update(name for name, _token in memo_key[-1]) - tokens, users_stamp = _assembly_revisions(rt, names, uses_roster) + tokens, users_stamp = _assembly_revisions(rt, names, uses_roster, + getattr(session, "tenant", None), roster_memo) result = ((*prefix, json.dumps(users_stamp, sort_keys=True, default=str)), tokens) return result @@ -505,7 +565,10 @@ def grid_assembly(session: Session, scope: str = "customer", storage_key: str = if not memoable: # Partial sessions and revision-less readers keep their original build semantics, # but concurrent callers can still borrow this flight's result. - out = (build(session), (), False) + borrowed = copy(session) + if not with_rows: + borrowed.runtime = _AssemblyStore(rt, schema_only=True) + out = (build(borrowed), (), False) return out lent = assembly_rt result = build(replace(session, runtime=lent)) @@ -520,11 +583,15 @@ def grid_assembly(session: Session, scope: str = "customer", storage_key: str = # Without a pool cache there is no runtime-scoped flight identity. if not hasattr(rt, "pool_cache"): - out = own_copy(build(session)) + borrowed = copy(session) + if not with_rows: + borrowed.runtime = _AssemblyStore(rt, schema_only=True) + out = own_copy(build(borrowed)) return out memoable = (is_dataclass(session) and all(hasattr(session, name) for name in ("tenant", "uname", "admin", "user"))) - assembly_rt = (rt if isinstance(rt, _AssemblyStore) else _AssemblyStore(rt)) if memoable else None + assembly_rt = (rt if isinstance(rt, _AssemblyStore) and rt.schema_only == (not with_rows) + else _AssemblyStore(rt, schema_only=not with_rows)) if memoable else None memoable = memoable and assembly_rt.cacheable key, checked_tokens = base_key() @@ -620,7 +687,7 @@ def grid_assembly(session: Session, scope: str = "customer", storage_key: str = def _compute_grid_assembly(session: Session, scope: str = "customer", storage_key: str = "", - consume_corrections: bool = True): + consume_corrections: bool = True, *, with_rows: bool = True, needed=None): """ONE assembly of this session's grid state, shared by `/customers`, `/workspace` and the events route (2026-07-31 — the standalone measure gap, owner item 1). @@ -653,7 +720,7 @@ def _compute_grid_assembly(session: Session, scope: str = "customer", storage_ke rt = session.runtime team_id, agent = _team_agent(session) - rows_src = _pool_for(rt, team_id, agent) + rows_src = _pool_for(rt, team_id, agent) if with_rows else [] # ⛔ THE ROW WALL, APPLIED BEFORE `pids` IS TAKEN. Everything downstream is bounded by that # frozenset — `allowed_pids` for the workspace, cohort membership, measure resolution — so # scoping here means a row this account may not see never enters ANY of them, rather than @@ -676,8 +743,9 @@ def _compute_grid_assembly(session: Session, scope: str = "customer", storage_ke # ⛔ THE SAME HANDLE GOES TO `allowed_pids` BELOW, AND THE PAIR IS NOT OPTIONAL. That is the # WRITE wall to this one's READ wall; lending it here alone would make a user-generated rule # narrow what an account SEES while leaving what it may PATCH untouched. - rows_src = perm_scope.apply_row_scope(rows_src, session.user, MODULE, aios_grid.FIELDS, - st=rt) + if with_rows: + rows_src = perm_scope.apply_row_scope(rows_src, session.user, MODULE, aios_grid.FIELDS, + st=rt) pids = frozenset(r["pid"] for r in rows_src if r.get("pid") is not None) # ⭐ W38-T20 — THE COLUMN DEFINITIONS ARE READ **ONCE** PER ASSEMBLY AND THREADED, because # `core.store.get` deep-copies whatever it hands back on every call. Three consumers want this @@ -701,7 +769,7 @@ def _compute_grid_assembly(session: Session, scope: str = "customer", storage_ke # number, so a per-user leftover under the same key is stale by construction. It is also what # makes D-423 recoverable rather than permanent: a pre-fix per-user edit is shadowed, not # promoted. - _shared = shared_cells(pids, st=rt) + _shared = shared_cells(pids, st=rt) if with_rows else {} if _shared: _ov = dict(ws.get("overlays") or {}) for _pid, _cells in _shared.items(): @@ -839,7 +907,7 @@ def _compute_grid_assembly(session: Session, scope: str = "customer", storage_ke today = time.strftime("%Y-%m-%d") fields = aios_grid.project_date_recurrences(fields, today=today) - stamp = _pool_stamp(rt, team_id, agent) + stamp = _pool_stamp(rt, team_id, agent) if with_rows else 0 # ⭐⭐ W38-T19 — THE METRICS CAPABILITY, AND THIS GRAIN NEEDS **THREE** GUARDS WHERE THE # OTHER TWO NEED ONE. `routes_products` and `routes_tables` funnel their cells and their # condition answers through helpers that short-circuit on an empty `offer`, so emptying the @@ -853,10 +921,10 @@ def _compute_grid_assembly(session: Session, scope: str = "customer", storage_ke measures = measure_resolve.offer(team_id, on_error=_measure_err) if may_metrics else [] measure_sets = measure_resolve.condition_sets( [v.get("config") or {} for v in (views or [])], None, team_id, pids, today, stamp, - rt.mset_memo, on_error=_measure_err) if may_metrics else {} + rt.mset_memo, on_error=_measure_err) if may_metrics and with_rows and needed is None else {} # The derived channel: cohort membership cells + measure column values, ONE dict — the # same read-only channel the embed host hands to rows_from_pool. - derived = aios_grid.cohort_cells(lists) + derived = aios_grid.cohort_cells(lists) if with_rows else {} # ⭐ W33-T43 / owner item 12 ("One unique ID per database always"). The customer grid carried # NO Odoo id column at all, while its retiring twin `ut_odoo_customers` carried `partner_id` # as its join key — so the merge would have lost the one value every Odoo document joins on. @@ -876,9 +944,11 @@ def _compute_grid_assembly(session: Session, scope: str = "customer", storage_ke # grains already do under an empty offer. Deleting the column instead would be a second, # louder behaviour for the same fact on one surface out of three, and it would destroy a # definition the admin can restore with one tick. + materialized_fields = fields if needed is None else [f for f in fields if f.get("key") in needed] for pid, cells in (measure_resolve.column_values( - fields, team_id, pids, today, stamp, rt.measure_memo, - on_error=_measure_err) if may_metrics else {}).items(): + materialized_fields, team_id, pids, today, stamp, rt.measure_memo, + on_error=_measure_err) if may_metrics and with_rows and + (needed is None or aios_grid.measure_fields_of(materialized_fields)) else {}).items(): derived.setdefault(pid, {}).update(cells) # Route stops and Zone membership are normalized system relations. They project into two diff --git a/api/routes_external.py b/api/routes_external.py index ef15bf41343c1552a6e56ae899e8b1c1bc73e2d6..df9625a99fe9e8527b0f45a8fbef068a44baeb3a 100644 --- a/api/routes_external.py +++ b/api/routes_external.py @@ -8,6 +8,8 @@ import hmac import importlib import inspect import json +from pathlib import Path +import re import time import uuid from urllib.parse import parse_qs, quote, unquote, urlencode, urlsplit, urlunsplit @@ -1370,28 +1372,43 @@ def _maybe_session(request: Request, response: Response): _PAGE_STYLE = """ -body{margin:0;background:#f6f5fb;font-family:Inter,system-ui,-apple-system,Segoe UI,sans-serif; -color:#1d1b2e;display:flex;justify-content:center;padding:48px 16px} -.card{background:#fff;border:1px solid #e4e1f0;border-radius:12px;max-width:460px;width:100%; -padding:28px;box-shadow:0 1px 3px rgba(20,16,50,.06)} -h1{font-size:20px;margin:0 0 8px}p{line-height:1.5;margin:0 0 16px;color:#4a4760} -fieldset{border:1px solid #e4e1f0;border-radius:8px;margin:0 0 16px;padding:12px 14px} -legend{font-weight:600;padding:0 4px}label{display:block;margin:6px 0;cursor:pointer} -small{display:block;color:#6d6a82;margin-left:24px}.dbs{margin:6px 0 0 24px;max-height:220px; -overflow:auto}.row{display:flex;gap:10px;justify-content:flex-end;margin-top:8px} -button{font:inherit;border-radius:8px;padding:9px 18px;cursor:pointer;border:1px solid #d6d2e8; -background:#fff;color:#1d1b2e}button.primary{background:#5b3fd6;border-color:#5b3fd6;color:#fff} -.who{font-size:13px;color:#6d6a82;margin-top:16px} +body.oauth-page{margin:0;background:var(--lp-surface-2); +font-family:Inter,system-ui,-apple-system,Segoe UI,sans-serif;font-size:var(--lp-fs-sm); +color:var(--lp-ink);display:flex;justify-content:center;padding:48px 16px} +.oauth-card{background:var(--lp-surface);border:1px solid var(--lp-line); +border-radius:var(--lp-r-lg);max-width:460px;width:100%;padding:24px} +.oauth-card h1{font-size:var(--lp-fs-lg);font-weight:600;margin:0 0 8px} +.oauth-card p{line-height:var(--lp-lh);margin:0 0 16px;color:var(--lp-muted)} +.oauth-card .row{display:flex;gap:8px;justify-content:flex-end;margin-top:8px} +.oauth-card button{border:0;background:transparent;font:inherit;color:inherit; +border-radius:var(--lp-r-md);padding:8px 16px;cursor:pointer} +.oauth-card button:hover{background:var(--lp-surface-2)} +.oauth-card button.primary{background:var(--lp-primary);color:var(--lp-surface);font-weight:600} +.oauth-card button.primary:hover{background:var(--lp-primary-hover)} +.oauth-card button:focus-visible{outline:2px solid var(--lp-blue-deep);outline-offset:2px} +.oauth-card .who{font-size:var(--lp-fs-xs);color:var(--lp-muted);margin:16px 0 0} """ +def _page_stylesheets() -> str: + """Borrow the emitted app stylesheet, including its canonical tokens and Inter font.""" + index = Path(__file__).resolve().parents[1] / "web" / "dist" / "index.html" + try: + document = index.read_text(encoding="utf-8") + except OSError: + return "" + paths = re.findall(r']*\bhref=[\"\'](/assets/[^\"\']+\.css)[\"\']', document) + return "".join(f'' + for path in dict.fromkeys(paths)) + + def _page(title: str, body: str, status: int = 200): from fastapi.responses import HTMLResponse return HTMLResponse( f"" f"" - f"{_html.escape(title)}" - f"
{body}
", status_code=status, + f"{_html.escape(title)}{_page_stylesheets()}" + f"
{body}
", status_code=status, headers={"Cache-Control": "no-store", "X-Frame-Options": "DENY", "Content-Security-Policy": "frame-ancestors 'none'"}) @@ -1447,35 +1464,18 @@ def _granted_scopes(requested, access: str) -> str: def _consent_page(request: Request, session: Session, meta: dict, fields: dict): name = _html.escape(meta["n"]) host = _html.escape(urlsplit(fields["redirect_uri"]).netloc) - try: - rows = [row for row in _resolve_external_scope(session) if row.get("permitted")] - except Exception: - rows = [] - dbs = "".join( - f"" for row in rows) token = _html.escape(_consent_token(session, fields)) action = _html.escape(str(request.url.path)) return _page(f"Connect {meta['n']} to Loopable", ( f"

Connect {name} to Loopable

" - f"

{name} wants to use your Loopable account. After you choose, you go back to " - f"{host}.

" + f"

Allow {name} to connect?

" + f"

Return to {host}.

" f"
" f"" - "
Access" - "" - "" - "
Databases" - "
{dbs or 'No databases are available to you.'}" - "
" + "
" "" "
" - f"

Signed in as {_html.escape(session.uname)}. You can disconnect it at any " - "time under AI API.

")) + f"

Signed in as {_html.escape(session.uname)}.

")) @router.get("/oauth/callback") @@ -1557,16 +1557,9 @@ async def oauth_consent(request: Request, session: Session = Depends(require_ses if (form.get("decision") or [""])[-1] != "allow": return _oauth_redirect(fields["redirect_uri"], error="access_denied", state=fields["state"]) - access = (external_access.ACCESS_READWRITE - if (form.get("access") or [""])[-1] == "readwrite" else external_access.ACCESS_READ) - chosen = [value for value in form.get("database") or [] if value] - if (form.get("scope_kind") or ["all"])[-1] == "some": - if not chosen: - return _refusal_page("Choose at least one database, or allow all of them. Go back " - "and try again.") - databases = chosen - else: - databases = external_access.ALL_DATABASES + # R15/C5: Allow approves the connection. Old/tampered picker fields cannot set a cap; + # every bearer call resolves the account's current permission record instead. + access, databases = external_access.ACCESS_READWRITE, external_access.ALL_DATABASES reason = perm_scope.ai_apps_refusal(session.user, session.runtime) if reason: return _refusal_page(reason, 403) diff --git a/api/routes_forms.py b/api/routes_forms.py index 8971004b3c471f25deb8ce239faade9649ab46d9..8bcab13ef04e69ea054bdb7f0141898ee63daba1 100644 --- a/api/routes_forms.py +++ b/api/routes_forms.py @@ -187,6 +187,9 @@ def _tokens(rt) -> dict: def _resolve(token: str): """`(runtime, tenant_slug, table_key, form_spec)` for a token, or None. + A hashed setup capability returns `table_key=None` and server-owned setup metadata; + ordinary forms retain their persistent view pointer and row-append path. + ⚠ THE LOOP DOES NOT SHORT-CIRCUIT ON THE TENANT, only on the match. A `break` on the first tenant whose store answered would make "the third tenant's form" depend on the first two being healthy, and a form that stops working because somebody else's store blipped is the @@ -207,9 +210,24 @@ def _resolve(token: str): rt = _rt.get_runtime(slug) except Exception: # noqa: BLE001 continue - for stored, where in _tokens(rt).items(): + index = _tokens(rt) + from core import setup_links + hashed = setup_links.digest(token) + if hashed and hashed in index: + try: + _setup_store(rt, slug) + record = setup_links.read_record(index, token, slug) + if record is None: + return None + _setup_owner(rt, slug, record["owner"], record["scope"]) + return rt, slug, None, record + except Exception: + return None + for stored, where in index.items(): if not _same(str(stored), token) or not isinstance(where, dict): continue + if where.get("kind") == setup_links.KIND: + return None table_key = str(where.get("table") or "") view, _owner = _find_view(rt, table_key, str(where.get("view") or "")) spec = _spec_of(view) @@ -403,6 +421,162 @@ def _link_of(token: str) -> str: return f"{_public_base()}/#/form/{token}" +def _setup_store(rt, tenant): + if not tenant or getattr(rt, "key", None) != tenant or not rt.available(): + raise err(503, "setup_unavailable", "Unavailable") + + +def _setup_owner(rt, tenant, owner, scope): + """Re-read the account registry; a minted capability does not freeze permissions.""" + from core import perms, users + + _setup_store(rt, tenant) + try: + registry = users.registry() + record = registry.get(owner) if isinstance(registry, dict) else None + if (not isinstance(record, dict) or not record.get("active", True) + or users._public(owner, record).get("tenant") != tenant): + raise _refuse() + if scope == "business" and not perms.is_admin(record): + raise err(403, "setup_admin_required", "Ask an admin") + return Session(tenant=tenant, user=users._public(owner, record), claims={}, runtime=rt) + except Exception as exc: + # Preserve our refusals, but never return a registry exception or its content. + if getattr(exc, "status_code", None) is not None: + raise + raise _refuse() from None + + +def _setup_fields(connector): + from core import keychain + from routes_connectors import _CONNECT_FIELDS, _CONNECT_TOKEN_FIELD + + if connector not in keychain.ENTRY_TYPES: + raise _refuse() + return [dict(field) for field in (_CONNECT_FIELDS.get(connector) or _CONNECT_TOKEN_FIELD)] + + +def _public_setup(record): + from core import keychain, setup_links + + fields = [] + for field in _setup_fields(record["connector"]): + fields.append({"key": field["key"], "label": field["label"], + "type": ("password" if field.get("secret") else + "url" if field["key"] == "url" else "string"), + "secret": bool(field.get("secret")), + "required": bool(field.get("required", True)), + **({"hint": field["hint"]} if field.get("hint") else {})}) + return {"kind": setup_links.KIND, "title": keychain.kind_label(record["connector"]), + "desc": "", "submitLabel": "Connect", "fields": fields, + "honeypot": HONEYPOT_FIELD, "singleUse": True} + + +@router.post("/connector-setup-link") +def mint_setup_link(body: dict, session: Session = Depends(require_session)): + """Mint a credential-free setup capability for the authenticated asking user only.""" + from core import keychain, setup_links + + if not isinstance(body, dict) or set(body) - {"connector", "scope"}: + raise err(400, "bad_setup_request", "Invalid request") + connector = body.get("connector") + if not isinstance(connector, str) or connector not in keychain.ENTRY_TYPES: + raise err(400, "bad_connector", "Unavailable") + scope = body.get("scope", "business" if connector in keychain.TENANT_WIDE_TYPES else "personal") + if scope not in ("personal", "business"): + raise err(400, "bad_scope", "Invalid scope") + current = _setup_owner(session.runtime, session.tenant, session.uname, scope) + if scope == "personal" and connector in keychain.TENANT_WIDE_TYPES: + raise err(403, "setup_admin_required", "Ask an admin") + if not _setup_fields(connector): + raise err(400, "bad_connector", "Unavailable") + try: + return setup_links.mint(session.runtime, tokens_key=TOKENS_KEY, tenant=current.tenant, + owner=current.uname, connector=connector, scope=scope, + origin=_public_base()) + except setup_links.InvalidSetupLink: + raise err(503, "setup_unavailable", "Unavailable") from None + except Exception: + raise err(503, "setup_unavailable", "Unavailable") from None + + +def _starter_selection(rt, provider): + """S70-T10 through the S70-T11 door: a first connection syncs the starter set, never everything. + + The link form has no database step, so it takes the wizard's preselected rows. An existing + connection or an earlier choice document is left alone (Settings owns a change).""" + from connector_onboarding import choices, install_selection + from core import keychain + import routes_keychain as keys + + starter = [row["key"] for row in choices(provider) if row.get("selected")] + if not starter: + return False + if any(entry.get("type") == provider for entry in keychain.list_entries(rt)) \ + or (provider == "odoo" and keys.env_odoo_available(rt)): + return False + try: + install_selection(rt, provider, starter, existing=False) + except ValueError: + return False + return True + + +def _write_setup_credentials(rt, record, fields): + """Keep API-owned scoping and reconnect effects out of the lower core module.""" + from core import keychain + import routes_keychain as keys + + current = _setup_owner(rt, record["tenant"], record["owner"], record["scope"]) + # Before the credential, as the wizard does, so no runner sees a key with all-on defaults. + selected = _starter_selection(rt, record["connector"]) + keychain.add_entry(rt, None, record["connector"], fields, current.uname, + scope=record["scope"], write_scope=keys._write_scope) + if selected: + from connector_onboarding import seal_selection + seal_selection(rt, record["connector"]) + if record["connector"] == "odoo": + try: + keys._rel_reconnect(rt) + except Exception: + pass + elif record["connector"] == "amazon_sp": + try: + from harness import amazon_store + amazon_store.cancel_deletion_trigger(rt.key, rt=rt, principal=current.uname) + except Exception: + pass + try: + from core import connector_sync + connector_sync.request_sync("amazon_sp", rt.key, requested_by=current.uname) + except Exception: + pass + + +def _submit_setup(rt, token, record, body): + from core import setup_links + + _setup_owner(rt, record["tenant"], record["owner"], record["scope"]) + if set(body) - {"values"} or not isinstance(body.get("values"), dict): + raise err(400, "invalid_submission", "Invalid fields") + values = dict(body["values"]) + if values.pop(HONEYPOT_FIELD, ""): + raise err(400, "invalid_submission", "Invalid fields") + try: + return setup_links.submit(rt, token, record, values, tokens_key=TOKENS_KEY, + field_specs=_setup_fields(record["connector"]), + write_credentials=lambda meta, clean: + _write_setup_credentials(rt, meta, clean)) + except setup_links.InvalidSetupLink: + raise _refuse() from None + except setup_links.InvalidSetupValues: + raise err(400, "invalid_submission", "Invalid fields") from None + except setup_links.SetupWriteFailed: + raise err(503, "setup_write_failed", "Save failed") from None + finally: + values.clear() + + def _may_administer_view(session: Session, table_key: str, view_id: str): """`(view, spec)` when this caller may publish THIS view, else raises. @@ -513,6 +687,8 @@ def get_form(token: str, request: Request): if not found: raise _refuse() rt, _slug, table_key, spec = found + if table_key is None: + return _public_setup(spec) return _public_form(rt, table_key, spec) @@ -562,6 +738,8 @@ async def submit_form(token: str, request: Request): if not found: raise _refuse() rt, _slug, table_key, spec = found + if table_key is None: + return _submit_setup(rt, token, spec, body) values = (body or {}).get("values") if not isinstance(values, dict): diff --git a/api/routes_grid.py b/api/routes_grid.py index f1666fa73cd275d0e5f14313070a24c40c7bb40c..5afd14e059f0402b5b99ef46f1a97ffd774645b4 100644 --- a/api/routes_grid.py +++ b/api/routes_grid.py @@ -319,9 +319,34 @@ def _map_setup(scope, fields, runtime): return database_setup.map_for(_database_key_for_scope(scope), fields, st=runtime) +def _workspace_values(values, session, module, fields): + """Project stored cell maps onto this request's served field contract. + + Group containers may carry embedded rows/values. Only those containers recurse; + the JSON value of a visible field stays verbatim, even if it uses a hidden key. + """ + from core import perm_scope + + visible = {field.get("key") for field in fields if isinstance(field, dict)} + values = perm_scope.visible_overlays( + values or {}, session.user, module, fields, st=session.runtime) + + def cells(raw): + if isinstance(raw, list): + return [cells(row) for row in raw] + if not isinstance(raw, dict): + return raw + return {key: value if key in visible else cells(value) + for key, value in raw.items() + if key in visible or (key in {"values", "rows", "groups", "children"} + and isinstance(value, (dict, list)))} + + return {str(pid): cells(raw) for pid, raw in values.items()} + + @router.get("/workspace") def workspace(scope: str = "customer", - session: Session = Depends(require_session)): + session: Session = Depends(require_session), fields_only: bool = False): """The durable table workspace for this session: views, fields, overlays, folders. ⛔ WAVE 21 (C4): the wall is TOPIC-SHAPED, so the DEPENDENCY is session-only and each scope @@ -343,7 +368,7 @@ def workspace(scope: str = "customer", wave-9 leak rule. Omitting it would hand a Fisch-scoped user a member list built by a full-access user. """ - from core import grid_events + from core import grid_events, shared_overlay from routes_customers import grid_assembly scope = _scope_or_400(scope) @@ -366,13 +391,18 @@ def workspace(scope: str = "customer", # bought nothing and cost everything — it is why `?scope=ut_odoo_gl_lines` answered # `409 window_required` six times out of six on the live deploy while the ROWS door for # the same table served a page in ~150 ms, i.e. a grid that could not be opened at all. - g = ut_assembly(session, scope, storage_key=storage_key, with_rows=False) - grid_events.workspace_corrections(session.runtime, scope + '_table_workspace', - session.uname, g) + g = ut_assembly(session, scope, storage_key=storage_key, with_rows=False, + fields_only=fields_only) + if not fields_only: + grid_events.workspace_corrections(session.runtime, scope + '_table_workspace', + session.uname, g) + except shared_overlay.SchemaUnavailable as exc: + raise err(503, "schema_unavailable", str(exc)) except grid_events.StoreUnavailable: raise err(503, "store_unavailable", "the tenant store is unavailable") workspace = g["workspace"] - workspace["overlays"] = g["ws"].get("overlays") or {} + workspace["overlays"] = _workspace_values( + g["ws"].get("overlays"), session, scope, g["fields"]) # The field contract rides the cheap re-read on EVERY topic — see the customer branch # below for why (a user table's first cohort changes its contract too: `_cohorts(ctx)` # is scope-parameterized, so `ut_` surfaces have their own sets). @@ -385,7 +415,7 @@ def workspace(scope: str = "customer", # them away, so the Locked-views column arrived on the cheap re-read with permanently # empty values. A column that exists and can never have one is worse than an absent # column: it reads as "this record is in no locked view", which is a claim. - workspace["derived"] = {str(pid): cells for pid, cells in (g["derived"] or {}).items()} + workspace["derived"] = _workspace_values(g["derived"], session, scope, g["fields"]) workspace["viewer"] = {"name": session.uname, "isAdmin": bool(session.admin)} try: from core import users as _users @@ -399,7 +429,8 @@ def workspace(scope: str = "customer", workspace["permissionUserOptions"] = [] workspace["userAvatars"] = {} workspace["scopeKey"] = scope - workspace["auditTimestamps"] = _audit_timestamps(scope, g["pids"], set(), session.runtime) + workspace["auditTimestamps"] = ({} if fields_only else + _audit_timestamps(scope, g["pids"], set(), session.runtime)) map_setup = _map_setup(scope, workspace["fields"], session.runtime) if map_setup: workspace["mapSetup"] = map_setup @@ -409,6 +440,8 @@ def workspace(scope: str = "customer", # `missing` on a shortened cohort). An unannounced empty scope is the silent limit R6 # forbids; this is the announcement, and W31-T22 is the ticket that renders it. workspace["limits"] = g.get("limits") or [] + if fields_only: + workspace["fieldsOnly"] = True return {"workspace": workspace} # ── Wave 16 C-TOPIC: the PRODUCT surface has its own assembly (own pool, own field @@ -424,13 +457,18 @@ def workspace(scope: str = "customer", session.require(PRODUCT_MODULE) storage_key = f"{session.tenant}:product-list:{session.uname}:all:all" try: - g = product_assembly(session, scope=scope, storage_key=storage_key) - grid_events.workspace_corrections(session.runtime, 'product_table_workspace', - session.uname, g) + g = product_assembly(session, scope=scope, storage_key=storage_key, + with_rows=not fields_only) + if not fields_only: + grid_events.workspace_corrections(session.runtime, 'product_table_workspace', + session.uname, g) + except shared_overlay.SchemaUnavailable as exc: + raise err(503, "schema_unavailable", str(exc)) except grid_events.StoreUnavailable: raise err(503, "store_unavailable", "the tenant store is unavailable") workspace = g["workspace"] - workspace["overlays"] = g["ws"].get("overlays") or {} + workspace["overlays"] = _workspace_values( + g["ws"].get("overlays"), session, PRODUCT_MODULE, g["fields"]) # R9 made cohorts per-topic, so the PRODUCT surface has its own sets and its own # first-cohort contract change. Same reason as the customer branch below. workspace["fields"] = g["fields"] @@ -442,7 +480,7 @@ def workspace(scope: str = "customer", # reason: `product_assembly` builds this topic's cohort cells and this route discarded # them. The measure half stays empty on this topic by descope, which is a different # statement and one the offer already makes. - workspace["derived"] = {str(pid): cells for pid, cells in (g["derived"] or {}).items()} + workspace["derived"] = _workspace_values(g["derived"], session, PRODUCT_MODULE, g["fields"]) workspace["viewer"] = {"name": session.uname, "isAdmin": bool(session.admin)} try: from core import users as _users @@ -456,10 +494,13 @@ def workspace(scope: str = "customer", workspace["permissionUserOptions"] = [] workspace["userAvatars"] = {} workspace["scopeKey"] = scope - workspace["auditTimestamps"] = _audit_timestamps(scope, g["pids"], set(), session.runtime) + workspace["auditTimestamps"] = ({} if fields_only else + _audit_timestamps(scope, g["pids"], set(), session.runtime)) map_setup = _map_setup(scope, workspace["fields"], session.runtime) if map_setup: workspace["mapSetup"] = map_setup + if fields_only: + workspace["fieldsOnly"] = True return {"workspace": workspace} # The storage key mirrors the host's own convention byte-for-byte (app.py's `_table_grid` @@ -483,9 +524,12 @@ def workspace(scope: str = "customer", # never rendered, `cohortMode` never arrived, and the Cohort route silently drew the Customer # surface. One projection for both servers is the fix that cannot drift. try: - g = grid_assembly(session, scope=scope, storage_key=storage_key) - grid_events.workspace_corrections(session.runtime, 'customer_table_workspace', - session.uname, g) + g = grid_assembly(session, scope=scope, storage_key=storage_key, with_rows=not fields_only) + if not fields_only: + grid_events.workspace_corrections(session.runtime, 'customer_table_workspace', + session.uname, g) + except shared_overlay.SchemaUnavailable as exc: + raise err(503, "schema_unavailable", str(exc)) except grid_events.StoreUnavailable: raise err(503, "store_unavailable", "the tenant store is unavailable") workspace = g["workspace"] @@ -497,7 +541,8 @@ def workspace(scope: str = "customer", # /customers rows, but this route is the cheap read-back a write can be verified against # (verify_api's overlay probe) without paying the pool call. Per-user by construction — # `table_workspace` is this session's workspace. - workspace["overlays"] = g["ws"].get("overlays") or {} + workspace["overlays"] = _workspace_values( + g["ws"].get("overlays"), session, MODULE, g["fields"]) # ⭐ THE FIELD CONTRACT, 2026-08-04 — and it is NOT decoration. # # `fields_from_workspace(ws, cohorts=bool(cohort_lists))` appends the derived "Locked @@ -522,7 +567,7 @@ def workspace(scope: str = "customer", workspace["measureSets"] = g["measure_sets"] # Derived cells (cohort column + measure columns), keyed by pid. JSON object keys are # strings; the client indexes with String(pid). - workspace["derived"] = {str(pid): cells for pid, cells in g["derived"].items()} + workspace["derived"] = _workspace_values(g["derived"], session, MODULE, g["fields"]) # Who is looking (permissions verdicts) + the assignee choices for `user`-typed columns — # the two other host-only render args the shell was missing (fail-closed without them: # restricted fields uneditable, assignee picker empty). @@ -546,8 +591,12 @@ def workspace(scope: str = "customer", # ⚠ Deliberately NOT `hideViews`: `cohortMode` is what swaps the Views panel for the cohort # list, and two switches for one behaviour would drift. workspace["scopeKey"] = scope - ctx = _ctx(session, g["fields"], g["pids"], scope_key=scope) - workspace["auditTimestamps"] = _audit_timestamps(scope, g["pids"], ctx.hidden_keys, session.runtime) + if fields_only: + workspace["auditTimestamps"] = {} + workspace["fieldsOnly"] = True + else: + ctx = _ctx(session, g["fields"], g["pids"], scope_key=scope) + workspace["auditTimestamps"] = _audit_timestamps(scope, g["pids"], ctx.hidden_keys, session.runtime) map_setup = _map_setup(scope, workspace["fields"], session.runtime) if map_setup: workspace["mapSetup"] = map_setup diff --git a/api/routes_keychain.py b/api/routes_keychain.py index 072e16463a4fb6eb487f36df68b0b1aba74a5f4d..b3e0dbf6adf3e8de2c0ce3088479d934aa846ac6 100644 --- a/api/routes_keychain.py +++ b/api/routes_keychain.py @@ -939,6 +939,16 @@ def add_key(body: dict = Body(default=None), session: Session = Depends(require_ f"a {etype} connection is what this whole workspace's databases are read " f"through, so it is always business-wide and cannot be a personal connection. " f"An administrator can add it for everyone.") + if "initialDatabases" in body: + from connector_onboarding import install_selection + try: + existing = any(entry.get("type") == etype for entry in kc.list_entries(session.runtime)) + existing = existing or (etype == "odoo" and env_odoo_available(session.runtime)) + install_selection(session.runtime, etype, body["initialDatabases"], existing=existing) + except ValueError as exc: + raise err(400, "bad_initial_selection", str(exc)) + except Exception: + raise err(503, "store_unavailable", "The database choices were not saved. Try again.") try: row = kc.add_entry(session.runtime, body.get("label"), body.get("type"), body.get("fields"), session.uname) @@ -949,6 +959,13 @@ def add_key(body: dict = Body(default=None), session: Session = Depends(require_ raise err(400, "bad_entry", str(e)) except Exception: raise err(503, "store_unavailable", "the entry was not saved; try again") + if "initialDatabases" in body: + from connector_onboarding import seal_selection + try: + seal_selection(session.runtime, etype) + except Exception: + raise err(503, "store_unavailable", + "The key was saved, but setup did not finish. Open Settings before trying again.") # ⛔⛔ A PERSONAL ENTRY THAT LOSES ITS SCOPE ROW READS AS BUSINESS-WIDE — i.e. the failure mode # of a side bucket is to publish a credential, not to hide one. So the second write is not # best-effort: if it does not land, the entry is REMOVED and the caller is told nothing was @@ -1577,11 +1594,11 @@ def odoo_disconnect(session: Session = Depends(require_session)): return cur # ⭐⭐ W33-T65 — THE FREEZE HAD A HOLE, AND THE NOTE BELOW WAS THE THING THAT MADE IT A DEFECT - # RATHER THAN A LIMIT. `rel.frozen` has exactly ONE consumer, `odoo_relational.refresh`, which + # RATHER THAN A LIMIT. `rel.frozen` originally had one consumer, `odoo_relational.refresh`, which # materialises the EIGHT copied grids. The other two (`ut_odoo_order_lines`, # `ut_odoo_gl_lines` — `READ_THROUGH_KEYS`) do not go through `refresh` at all: they read # THROUGH the per-tenant DuckDB mirror, and the mirror is advanced by `datastore.sync_all`, - # which gates on the connector PAUSE flag and has never heard of `frozen`. So a disconnected + # which originally gated only on the connector PAUSE flag. So a disconnected # workspace kept serving LIVE, still-moving rows in its two biggest grids while this route's # own sentence promised *"nothing is being refreshed"*. # @@ -1594,6 +1611,9 @@ def odoo_disconnect(session: Session = Depends(require_session)): # · the snapshot is captured BEFORE the flag flips, so there is a last-successful-sync to # serve; a failed capture leaves the connector live rather than paused-with-nothing. _, flag_key = _resolved_odoo_key(session.runtime) + if not flag_key: + raise err(503, "source_unavailable", + "nothing was disconnected; the Odoo source could not be resolved for pausing") snapshots = 0 if flag_key: try: @@ -1602,14 +1622,16 @@ def odoo_disconnect(session: Session = Depends(require_session)): # A snapshot is a nicety; the freeze is the promise. Reported, never fatal. snapshots = 0 - # ⚠ THE FLAG FIRST, THE CREDENTIAL SECOND. If the flag write fails, nothing has happened and - # the connector is still live; if the delete failed AFTER the flag landed, the tenant is + # ⚠ BOTH FLAGS FIRST, THE CREDENTIAL SECOND. If either flag write fails, the key is kept; + # if the delete failed AFTER the flags landed, the tenant is # frozen with an unused key, which is recoverable from the Keychains pane. The reverse order # can leave a workspace with no key and a connector that still tries to sync. try: - session.runtime.update(rel.CONFIG_KEY, _up, flush="sync") + frozen = session.runtime.update(rel.CONFIG_KEY, _up, flush="sync") + if not isinstance(frozen, dict) or frozen.get("frozen") is not True: + raise RuntimeError("the Odoo freeze was not confirmed") except Exception: - raise err(503, "store_unavailable", "nothing was disconnected; try again") + raise err(503, "store_unavailable", "nothing was disconnected; try again") from None paused_mirror = False if flag_key: @@ -1618,10 +1640,15 @@ def odoo_disconnect(session: Session = Depends(require_session)): cur[str(flag_key)] = {"paused": True} return cur try: - session.runtime.update(_CONNECTOR_FLAGS_KEY, _pause, flush="sync") + flags = session.runtime.update(_CONNECTOR_FLAGS_KEY, _pause, flush="sync") + saved = flags.get(str(flag_key)) if isinstance(flags, dict) else None + if not isinstance(saved, dict) or saved.get("paused") is not True: + raise RuntimeError("the Odoo pause was not confirmed") paused_mirror = True except Exception: # noqa: BLE001 - paused_mirror = False + raise err(503, "store_unavailable", + "the databases are frozen but the Odoo pause could not be saved; " + "no credential was removed; try again") from None removed = "" if entry is not None: @@ -1641,10 +1668,6 @@ def odoo_disconnect(session: Session = Depends(require_session)): # the limit that cannot be removed is REPORTED, with what to do instead. note = ("Your Odoo databases are frozen: every row and every column you had is still there and " "still readable, and nothing is being refreshed.") - if not paused_mirror and flag_key: - note += (" ⚠ The live mirror could not be paused, so the two read-through databases " - "(order lines and GL lines) may keep advancing; pause the Odoo connector under " - "Keychains to stop them.") note += (" Reconnecting adds the key back and resumes into the same databases." if entry else " This workspace's Odoo credential comes from the deployment environment, so " "there was no stored key to remove; the databases are frozen and Reconnect " diff --git a/api/routes_nav.py b/api/routes_nav.py index f5495fb916fbdbd4d8e2a70361b3b73d635e07c8..e280ab37de7a0dee8bcc6dd296c786943fa33bd3 100644 --- a/api/routes_nav.py +++ b/api/routes_nav.py @@ -562,10 +562,14 @@ def _odoo_connection_folders(runtime): return {}, None try: import core.keychain as keychain + import core.user_tables as user_tables import odoo_relational as odoo_rel + # Both existing metadata resolvers see this one request's captured document. The + # keychain bucket is explicitly supplied; it never joins the automatic lend list. + borrowed = user_tables.lend(runtime, **{keychain.KEY: runtime.get(keychain.KEY) or {}}) connections = 0 labels = {} - for row in keychain.list_entries(runtime): + for row in keychain.list_entries(borrowed): if str(row.get("type") or "") != "odoo": continue connections += 1 @@ -574,8 +578,8 @@ def _odoo_connection_folders(runtime): if cid and label and label.lower() not in _RESERVED_GROUP_LABELS: labels[cid] = label if connections < 2: - return {}, odoo_rel.default_connection_id(runtime) - return labels, odoo_rel.default_connection_id(runtime) + return {}, odoo_rel.default_connection_id(borrowed) + return labels, odoo_rel.default_connection_id(borrowed) except Exception: # noqa: BLE001 return {}, None @@ -891,6 +895,9 @@ def nav_opened(body: dict = Body(default=None), key = str(body.get("key") or "").strip()[:60] if not key: raise err(400, "bad_request", "no page was named") + from core import freeze_notice + if freeze_notice.enabled(): + return {"key": key, "at": _now()} if not session.runtime.available(): raise err(503, "store_unavailable", "the tenant store is unavailable. Nothing was recorded.") diff --git a/api/routes_odoo_tables.py b/api/routes_odoo_tables.py index 04c2defd682b611a2070405f623f20a4979bb8fe..a517b853df47256ff553f419a375742ac18e8c52 100644 --- a/api/routes_odoo_tables.py +++ b/api/routes_odoo_tables.py @@ -112,7 +112,10 @@ def _table_cursor(rt, table_key=None): if rt is not None: rt.assert_datastore_matches() return datastore.ro_cursor() - return _rt().mirror_cursor(rt) if rt is not None else datastore.ro_con() + if rt is not None: + from connector_onboarding import mirror_cursor + return mirror_cursor(rt, table_key) + return datastore.ro_con() @router.post("/odoo-tables/refresh") @@ -192,10 +195,9 @@ def odoo_tables_status(brief: bool = Query(False), without anybody running a refresh to find out. `brief=1` is the grid's one question at open time: which declared tables read through the - mirror? It deliberately does not borrow the tenant document, take a mirror cursor, or count - rows. Those would be work solely for metadata the client never reads. The source registry - remains the authority, so a future conversion is still discovered server-side rather than by - a client-side key list. + mirror? It reads only definitions, never stored rows or mirror counts. Persisted Amazon + stubs retain their routing when the local mirror has not loaded; the rows door then names + that availability failure rather than painting an empty stored table. """ rel = _rel() if brief: @@ -208,6 +210,15 @@ def odoo_tables_status(brief: bool = Query(False), # serves is read-through; none is listed when this container's mirror holds no Amazon rows. tables.update({key: {"readThrough": True} for key in sources if str(key).startswith("ut_amzn_") and key not in tables}) + ut = _ut() + from core import perm_scope + definitions = ut.all_defs(st=session.runtime) + lent = ut.lend(session.runtime, **{ut.STORE_KEY: definitions}) + for key, definition in definitions.items(): + if (str(key).startswith("ut_amzn_") and isinstance(definition, dict) + and definition.get("readThrough") is True + and perm_scope.may_read(session.user, key, st=lent)): + tables[key] = {"readThrough": True} return {"ok": True, "tables": tables} import core.user_tables as user_tables @@ -725,25 +736,21 @@ def _sources(): return GRID_SOURCES -def _learn_amazon_sources(): - """Publish the Amazon provider's keys into a registry that was built without any of them. - - HOTFIX 2026-09-27 (moved here by S67-T28 so every door shares it): a registry built at boot - BEFORE the Amazon mirror restore holds no Amazon key, and only `refresh_state` invalidates - it. `_source_for` learned them on demand; `_materialised_table` did not, so a read-through - Amazon LINK TARGET (orders, 559k rows) was dispatched as a stored table and refused as more - than one window. A registry that already knows one Amazon key is not re-asked. - """ +def _learn_amazon_sources(cur=None): + """Publish the complete current Amazon binding set, including restored missing entries.""" _sources() - if any(k.startswith("ut_amzn_") for k in list(GRID_SOURCES)): - return - extra = _amazon_sources() + if cur is None: + extra = _amazon_sources() + else: + import amazon_relational + extra = amazon_relational.grid_sources(cur) if not extra: return with _SOURCES_LOCK: - if not any(k.startswith("ut_amzn_") for k in list(GRID_SOURCES)): - GRID_SOURCES.update(extra) - _ut().register_connected(*extra) + # One known key cannot certify every history table. Replace old missing bindings as + # one publication, so the first rows AND aggregates request sees the restored schema. + GRID_SOURCES.update(extra) + _ut().register_connected(*extra) def _odoo_sources(): @@ -1092,6 +1099,8 @@ def _odoo_sources(): "move_type": ("move_type", _s), }, } + GRID_SOURCES.update(rel.stock_source_specs()) + GRID_SOURCES.update(rel.purchase_source_specs()) return GRID_SOURCES @@ -1160,7 +1169,7 @@ def require_amazon_mirror(table_key, session, definition=None, st=None): # `_sources()` skip its build, so the grid resolved while its link targets # (orders, read-through) were dispatched as stored tables: a first-page 500. if spec is None or spec.get("missing"): - _learn_amazon_sources() + _learn_amazon_sources(cur) with _SOURCES_LOCK: _sources() held = GRID_SOURCES.get(table_key) @@ -1400,13 +1409,13 @@ def _source_for(cur, table_key): if module_cache_source(key) is not None: return None spec = _sources().get(key) - if not spec and key.startswith("ut_amzn_"): + if (not spec or spec.get("missing")) and key.startswith("ut_amzn_"): # HOTFIX 2026-09-27: a registry built at boot BEFORE the Amazon mirror restore holds no # Amazon key, and only `amazon_relational.refresh_state` invalidates it, which the resync # runs AFTER the Odoo rebuild. On staging that rebuild hung for 14+ minutes, so every - # read-through Amazon grid answered "no read-through binding". Ask the Amazon provider - # alone, once it can answer; a registry that already knows one Amazon key is not re-asked. - _learn_amazon_sources() + # read-through Amazon grid answered "no read-through binding". S70-T12 re-asks the + # complete Amazon provider on this restored cursor, including cached missing entries. + _learn_amazon_sources(cur) spec = GRID_SOURCES.get(key) if not spec: return None @@ -1974,7 +1983,8 @@ def apply_filtered_bulk(table_key: str, body: dict = Body(default=None), definitions = shared_overlay.fields(table_key, st=session.runtime) if key not in definitions: raise err(400, "field_not_shared", "bulk updates on connected data require an existing shared field") - if key in perm_scope.hidden_keys(session.user, table_key, list(definitions.values())): + if key in perm_scope.hidden_keys(session.user, table_key, list(definitions.values()), + st=session.runtime): raise err(403, "field_hidden", "you do not have access to that field") value = body.get("value") if isinstance(value, (dict, list, tuple, set)): @@ -3203,7 +3213,7 @@ def _compiled_read(table_key, session, *, filters=None, filterConj="and", sorts= # two doors. import core.perm_scope as perm_scope wall_sql, wall_params = None, [] - _hidden = perm_scope.hidden_keys(session.user, table_key, fields) + _hidden = perm_scope.hidden_keys(session.user, table_key, fields, st=session.runtime) # `row_scope_applies` RATHER THAN AN ADMIN TEST SPELLED OUT HERE: it is this repo's ONE # statement of when the row wall bites (False for an admin, False for a record with no # declared filter), and a second spelling of it beside the first is how two of them come @@ -3721,13 +3731,13 @@ def odoo_table_rows(table_key: str, # in an overlay column that reads a hidden mirror column was outside its reach - and a formula # carrying a hidden value is the leak `hidden_keys` exists to close, wearing a second door's # name. Same wall, asked once the field list is whole. - _hidden = perm_scope.hidden_keys(session.user, table_key, fields) + _hidden = perm_scope.hidden_keys(session.user, table_key, fields, st=session.runtime) if _hidden: # BOTH WIRES, and the field list is narrowed BEFORE the rows are built so a hidden column # is never assembled rather than assembled and then removed. `strip_row` runs anyway on # the overlay-merged result, because a shared-overlay cell arrives by a different door # than the mirror window and one narrowing cannot speak for both. - fields = perm_scope.visible_fields(fields, session.user, table_key) + fields = perm_scope.visible_fields(fields, session.user, table_key, st=session.runtime) rows_src = [perm_scope.strip_row(r, _hidden) for r in rows_src] overlays = {pid: {k: v for k, v in cells.items() if k not in _hidden} for pid, cells in overlays.items()} @@ -3755,12 +3765,8 @@ def odoo_table_rows(table_key: str, # changes as you scroll, and nothing on screen says so. # ══════════════════════════════════════════════════════════ -#: At most this many groups ride one `/aggregates` response. -#: -#: ⛔ STANDING RULE 1'S SECOND SENTENCE IS THE WHOLE OF THIS CONSTANT'S CONTRACT: over the cap -#: the response carries the largest 1,000 by row count AND a `limits` entry naming the true -#: distinct count, never a silent 1,000 that reads as the whole answer. A cap that does not report -#: itself is the failure this number exists to avoid rather than to cause. +#: Maximum requested group PAGE size, never a ceiling on reachable groups. Legacy callers +#: that omit groupLimit receive the complete grouping; card modes page columns explicitly. MAX_GROUPS = 1_000 #: One request asks for at most this many summaries. Each is one more aggregate expression in a @@ -3889,7 +3895,8 @@ def _agg_plan(cols, aggs, limits): return plan -def _aggregate_over(cur, spec, cols, where, params, frm, aggs, group_by=None, limits=None): +def _aggregate_over(cur, spec, cols, where, params, frm, aggs, group_by=None, limits=None, + group_offset=0, group_limit=None): """The C1 summary itself: `{total_rows, aggregates, groups, limits}` over ONE compiled `where`. ⛔⛔ PURE OF THE SESSION ON PURPOSE. Everything session-shaped (both walls, the @@ -3931,10 +3938,8 @@ def _aggregate_over(cur, spec, cols, where, params, frm, aggs, group_by=None, li gexpr = _fs().group_sql(gspec) # ⛔⛔ THE OVERALL SUMMARY IS COMPUTED WHETHER OR NOT THIS REQUEST GROUPS, AND IT IS **NOT** - # RECOVERED FROM THE GROUPS. Two reasons, either of which is fatal on its own: the groups may - # be TRUNCATED at the cap, so adding them up would answer a summary bar over the largest 1,000 - # buckets while calling it the total; and a `median` or a `percentile` does not compose from - # per-group medians at all, at any cap. The first cut of this function returned an all-`null` + # RECOVERED FROM THE GROUPS. A page may hold only some groups, and a `median` or a + # `percentile` does not compose from per-group medians at all. The first cut returned an all-`null` # `aggregates` beside a populated `groups`, which would have put blanks in the summary bar of # every grouped view - caught by the gate leg that drives the same call ungrouped. # ⚠ AND `total_rows` COMES FROM **THIS** STATEMENT IN BOTH SHAPES. The grouped read below runs @@ -3951,37 +3956,22 @@ def _aggregate_over(cur, spec, cols, where, params, frm, aggs, group_by=None, li return {"total_rows": total_rows, "aggregates": answers, "groups": None, "limits": limits} - # ⛔ `ORDER BY 2 DESC, 1 ASC` IS A **TOTAL** ORDER AND THE SECOND KEY IS NOT DECORATION. - # `count DESC` alone ties, and a tie under `LIMIT 1001` means WHICH groups survive the cap - # varies between two runs of the same request. `compile_order_by` carries the same lesson for - # LIMIT/OFFSET paging, and this file's `string_agg` scar is the same defect wearing a third - # face. Positional keys, so an alias cannot be shadowed by a column of the same name. - # ⚠ `LIMIT MAX_GROUPS + 1` DETECTS THE CAP WITHOUT PAYING FOR IT: a 1,001st row is the only - # evidence needed that there are more, and the distinct count is only then worth a query. - fold = datastore.window( - select=", ".join([f'{gexpr} AS "_k"', 'count(*) AS "_c"'] + agg_proj), - where=where, params=tuple(params), order_by="2 DESC, 1 ASC", - group_by=gexpr, limit=MAX_GROUPS + 1, cur=cur, **frm) - rows = list(fold["rows"]) - if len(rows) > MAX_GROUPS: - rows = rows[:MAX_GROUPS] - # ⚠ `+ 1 WHEN THERE ARE NULLS`, because a NULL is a BUCKET on this door and - # `count(DISTINCT)` drops it. `group_sql` coalesces text and dates but deliberately leaves - # a numeric NULL alone (a missing amount is not a zero), so a numeric group column can - # carry a blank group. Without this the sentence would understate by one, which is a - # small lie in the one place whose entire job is to state how much is missing. - distinct = _agg_number(datastore.window( - select=(f"count(DISTINCT {gexpr}) + CASE WHEN count(*) - count({gexpr}) > 0 " - f"THEN 1 ELSE 0 END"), - where=where, params=tuple(params), - order_by="1", limit=1, cur=cur, **frm)["rows"][0][0]) or 0 - limits.append({ - "subject": gcol, "effect": "groups_truncated", - "cause": f"this column has {int(distinct):,} distinct values under the current " - f"filter and one response carries at most {MAX_GROUPS:,} groups, so the " - f"{MAX_GROUPS:,} with the most rows are here and the rest are not", - "recommendation": "narrow the filter first, or group by a column with fewer distinct " - "values"}) + # S70-T12: paging bounds response memory without discarding any column. Count the NULL + # bucket too. All SQL expressions/from clauses come from the already walled compiler. + distinct = int(datastore.window( + select=(f"count(DISTINCT {gexpr}) + CASE WHEN count(*) - count({gexpr}) > 0 " + f"THEN 1 ELSE 0 END"), + where=where, params=tuple(params), order_by="1", limit=1, cur=cur, **frm)["rows"][0][0] or 0) + projection = ", ".join([f'{gexpr} AS "_k"', 'count(*) AS "_c"'] + agg_proj) + if group_limit is not None: + rows = datastore.window( + select=projection, where=where, params=tuple(params), order_by="2 DESC, 1 ASC", + group_by=gexpr, offset=group_offset, limit=group_limit, cur=cur, **frm)["rows"] + else: + source = frm.get("from_sql") or frm["table"] + tail = f" WHERE {where}" if where else "" + rows = cur.execute(f"SELECT {projection} FROM {source}{tail} GROUP BY {gexpr} " + "ORDER BY 2 DESC, 1 ASC", tuple(params)).fetchall() groups = [] for r in rows: vals = {col_id: (_agg_number(r[2 + idx]) if (2 + idx) < len(r) else None) @@ -3989,7 +3979,8 @@ def _aggregate_over(cur, spec, cols, where, params, frm, aggs, group_by=None, li groups.append({"key": _group_key(r[0]), "label": _group_label(r[0]), "count": int(r[1] or 0), "aggregates": vals}) return {"total_rows": total_rows, "aggregates": answers, "groups": groups, - "limits": limits} + "total_groups": distinct, "group_offset": group_offset, + "group_limit": group_limit, "limits": limits} @router.post("/odoo-tables/{table_key}/aggregates") @@ -3997,8 +3988,9 @@ def odoo_table_aggregates(table_key: str, body: dict = Body(default=None), session: Session = Depends(require_session)): """SERVER-SIDE SUMMARIES over the WHOLE predicate a server-windowed grid is showing (C1). - Body `{filters, filterConj, search, related?, groupBy?, aggs: [{colId, agg, p?}]}`; response - `{ok, total_rows, aggregates, groups?, limits}`. + Body `{filters, filterConj, search, related?, groupBy?, groupOffset?, groupLimit?, + aggs: [{colId, agg, p?}]}`; response `{ok, total_rows, aggregates, groups?, limits}` + plus `total_groups`, `group_offset`, `group_limit` when grouped. No group is capped away. ⚠ `related` IS A BODY KEY HERE AND A JSON QUERY STRING ON `/rows`. Same object either way (`{table, record, via}`), same compiler, two transports - because this door is a POST and that @@ -4013,7 +4005,7 @@ def odoo_table_aggregates(table_key: str, body: dict = Body(default=None), ⛔ Anything unanswerable is a `limits` sentence with a cause and a recommendation, in the SAME four-key shape `/rows` uses, and never a silent 0: an unknown column, a hidden one, a numeric summary over text, a percentile with no `p`, a second summary for one column, an - ungroupable `groupBy`, and the 1,000-group cap. + ungroupable `groupBy`. Group pages have a bounded size and every group remains reachable. """ payload = body if isinstance(body, dict) else {} # ⚠ THE WIRE SHAPE IS VALIDATED BY THE SAME HELPER THE BULK DOORS USE, not re-checked by @@ -4032,12 +4024,23 @@ def odoo_table_aggregates(table_key: str, body: dict = Body(default=None), group_by = payload.get("groupBy") if group_by is not None and not isinstance(group_by, str): raise err(400, "bad_selection", "groupBy must be a column id") + group_offset, group_limit = payload.get("groupOffset", 0), payload.get("groupLimit") + if type(group_offset) is not int or group_offset < 0: + raise err(400, "bad_selection", "groupOffset must be a nonnegative integer") + if group_limit is not None and (type(group_limit) is not int or not 1 <= group_limit <= MAX_GROUPS): + raise err(400, "bad_selection", f"groupLimit must be a page size from 1 to {MAX_GROUPS}") + if group_offset and group_limit is None: + raise err(400, "bad_selection", "Send groupLimit when paging groups") + + page = _page_defs(session) if str(table_key).startswith("ut_amzn_") else None + require_amazon_mirror(table_key, session, st=page[0] if page else None) if _materialised_table(table_key): with _materialised_read(table_key, session, query, payload.get("related"), ids=ids) as read: out = _aggregate_over(read["cur"], read["spec"], read["cols"], read["where"], read["params"], read["frm"], aggs, group_by=group_by, - limits=read["limits"]) + limits=read["limits"], group_offset=group_offset, + group_limit=group_limit) return {"ok": True, **out, "scope": {"table": table_key, "readThrough": True}} # ⛔⛔ THE PIN RIDES THE **SAME** COMPILER, AND THIS ONE LINE IS THE WHOLE OF CORRECTION (3). @@ -4049,15 +4052,17 @@ def odoo_table_aggregates(table_key: str, body: dict = Body(default=None), # come from here) no longer re-reads every definition while `user_tables` is unchanged. read = _compiled_read(table_key, session, tree=query["filters"], filterConj=query["filterConj"], search=query["search"], - related=payload.get("related"), st=_page_defs(session)[0]) + related=payload.get("related"), st=page[0] if page else _page_defs(session)[0]) read = _restrict_row_ids(read, ids) out = _aggregate_over(read["cur"], read["spec"], read["cols"], read["where"], read["params"], read["frm"], aggs, group_by=group_by, - limits=read["limits"]) + limits=read["limits"], group_offset=group_offset, + group_limit=group_limit) body_out = {"ok": True, "total_rows": out["total_rows"], "aggregates": out["aggregates"], "limits": out["limits"], "scope": {"table": table_key, "readThrough": True}} if out["groups"] is not None: body_out["groups"] = out["groups"] + body_out.update({k: out[k] for k in ("total_groups", "group_offset", "group_limit")}) return body_out diff --git a/api/routes_products.py b/api/routes_products.py index e0ae226135a635e963c44f110e17242e8a450c8a..173d6ec5c284cbd930aa0a09dd9827cca47822fb 100644 --- a/api/routes_products.py +++ b/api/routes_products.py @@ -109,7 +109,7 @@ def _pool_for(rt, team_id): return scope_cache.get(rt.pool_cache, key, _CACHE_TTL, build) -def scoped_pool(session: Session): +def scoped_pool(session: Session, *, with_rows: bool = True): """`(pids, team_id, rows_src, fields_base)` — THE PRODUCT WALL, on its own. Extracted from `product_assembly` (wave 19, item 12) so a caller that needs only "which @@ -124,7 +124,7 @@ def scoped_pool(session: Session): team_id, _agent = perm_scope.derive_pool_scope(session.user, MODULE) try: - rows_src = _pool_for(session.runtime, team_id) + rows_src = _pool_for(session.runtime, team_id) if with_rows else [] except Exception as e: raise err(503, "pool_unavailable", f"the product catalogue could not be built; {str(e)[:160]}") @@ -132,7 +132,8 @@ def scoped_pool(session: Session): # the field wall runs so every reader receives the same metadata and the # Leadership account owns the permission editor from the first request. import modules.product_data as pd - pd.image_field_definition(st=session.runtime) + if with_rows: + pd.image_field_definition(st=session.runtime) fields_base = pd_fields(consolidated=team_id is None, st=session.runtime) # The SAME wall the customer assembly applies, in the same order: rows first (before pids # are taken, so an out-of-filter row never enters allowed_pids), then the field closure, @@ -148,9 +149,10 @@ def scoped_pool(session: Session): fields_base = list(fields_base) + [ field for field in enabled_raw if field.get("key") not in field_keys ] - rows_src = _project_enabled_raw_rows(rows_src, enabled_raw, drop_source=True) - rows_src = perm_scope.apply_row_scope(rows_src, session.user, MODULE, fields_base, - st=session.runtime) + if with_rows: + rows_src = _project_enabled_raw_rows(rows_src, enabled_raw, drop_source=True) + rows_src = perm_scope.apply_row_scope(rows_src, session.user, MODULE, fields_base, + st=session.runtime) pids = frozenset(r["pid"] for r in rows_src if r.get("pid") is not None) return pids, team_id, rows_src, fields_base @@ -397,17 +399,19 @@ def _project_enabled_raw_rows(rows, enabled, *, drop_source=False): def product_assembly(session: Session, scope: str = "product", storage_key: str = "", - consume_corrections: bool = False): + consume_corrections: bool = False, *, with_rows: bool = True, needed=None): from routes_customers import grid_assembly from core import perm_scope team_id, _agent = perm_scope.derive_pool_scope(session.user, MODULE) return grid_assembly(session, scope, storage_key, - build=lambda borrowed: _compute_product_assembly(borrowed, scope, storage_key, False), - pool_stamp=lambda: _measure_stamp(session.runtime, team_id)) + build=lambda borrowed: _compute_product_assembly( + borrowed, scope, storage_key, False, with_rows=with_rows, needed=needed), + pool_stamp=lambda: _measure_stamp(session.runtime, team_id) if with_rows else 0, + with_rows=with_rows, needed=needed) def _compute_product_assembly(session: Session, scope: str = "product", storage_key: str = "", - consume_corrections: bool = False): + consume_corrections: bool = False, *, with_rows: bool = True, needed=None): """The product topic's mirror of `routes_customers.grid_assembly` — SAME g-dict keys, so the /workspace and events routes consume either interchangeably. @@ -437,7 +441,7 @@ def _compute_product_assembly(session: Session, scope: str = "product", storage_ import aios_grid import core.perm_scope as perm_scope import modules.product_data as pd - pids, team_id, rows_src, fields_base = scoped_pool(session) + pids, team_id, rows_src, fields_base = scoped_pool(session, with_rows=with_rows) shared_defs = pd.shared_fields(st=session.runtime) ctx = grid_events.EventCtx( @@ -457,7 +461,7 @@ def _compute_product_assembly(session: Session, scope: str = "product", storage_ # shared value can therefore never leak INTO the per-user bucket by way of this merge. # ⚠ SHARED WINS per key — these are contract columns, so the tenant-wide value is the answer # and any per-user leftover under the same key is stale by construction. - _shared = pd.shared_cells(pids, st=session.runtime) + _shared = pd.shared_cells(pids, st=session.runtime) if with_rows else {} if _shared: _ov = dict(ws.get("overlays") or {}) for _pid, _cells in _shared.items(): @@ -513,15 +517,17 @@ def _compute_product_assembly(session: Session, scope: str = "product", storage_ today = time.strftime("%Y-%m-%d") fields = aios_grid.project_date_recurrences(fields, today=today) - stamp = _measure_stamp(session.runtime, team_id) + stamp = _measure_stamp(session.runtime, team_id) if with_rows else 0 measures = product_measures(session) # R9: the Cohorts column's cells, built from THIS topic's lists. ⭐ W37-T10 — the measure # half of this same read-only channel is no longer empty; the customer assembly merges its # measure columns here for exactly the same reason and through the same door. - derived = aios_grid.cohort_cells(lists) - for pid, cells in _measure_cells(fields, rows_src, team_id, today, stamp, - session.runtime.measure_memo, measures).items(): + derived = aios_grid.cohort_cells(lists) if with_rows else {} + materialized_fields = fields if needed is None else [f for f in fields if f.get("key") in needed] + for pid, cells in (_measure_cells(materialized_fields, rows_src, team_id, today, stamp, + session.runtime.measure_memo, measures) + if with_rows and (needed is None or aios_grid.measure_fields_of(materialized_fields)) else {}).items(): derived.setdefault(pid, {}).update(cells) # ⛔ THE CONDITION CHANNEL SHIPS WITH THE OFFER, NOT AFTER IT. `routes_grid` derives @@ -529,14 +535,16 @@ def _compute_product_assembly(session: Session, scope: str = "product", storage_ # filter builder offers measure conditions — and an id missing from `measureSets` renders as # PENDING ("Calculating…") and matches nothing, permanently. Serving the offer without this # would trade one honest absence for a spinner that never resolves. - measure_sets = _measure_condition_sets(views, measures, rows_src, team_id, today) + measure_sets = (_measure_condition_sets(views, measures, rows_src, team_id, today) + if with_rows and needed is None else {}) from core import user_tables as _zones fields = _zones.with_zone_field("product_data", fields, st=session.runtime) # Existing supplier-master defaults and imported overlay cells predate W43's write-time # choice hydration. Build their effective, already-walled row values before returning the # field contract so a populated select never opens as "no choices yet". - fields = _hydrate_effective_choice_options(fields, rows_src, ws.get("overlays"), derived) + if with_rows: + fields = _hydrate_effective_choice_options(fields, rows_src, ws.get("overlays"), derived) # ⭐⭐ D-590 (wave 46 hotfix) — **THE PER-TENANT SCHEMA OVERLAY, APPLIED TO THE GRID READ.** # ⛔ THIS SURFACE IS THE ONE W46-T40 FAILED ON, and the staged spec named only the customer and # user-table assemblies because lane E traced `routes_grid.py::workspace` alone. T40's evidence diff --git a/api/routes_sheet.py b/api/routes_sheet.py index d77b3cb53c070b5df2d91a3929c8555dcde551ba..999c072023c1c79e0694bc164a7672cc48d96524 100644 --- a/api/routes_sheet.py +++ b/api/routes_sheet.py @@ -76,7 +76,7 @@ def _require_database(session: Session, database) -> str: if not key: raise err(400, "sheet_database_required", "That cell does not name a database.") import core.perm_scope as perm_scope - if not perm_scope.nav_may_open(session.user, key, st=session.runtime): + if not perm_scope.nav_may_open(session.user, key, st=_sheet_permission_reader(session)): raise err(403, "database_unreadable", f"You cannot read {key}.") tcfg = getattr(session.runtime.tenant, "config", None) or {} tmods = tcfg.get("modules", "all") @@ -96,11 +96,19 @@ def _refuse_unapplied_audience(session: Session, database) -> None: declared (`audience_declared`), so this fails closed. """ import core.perm_scope as perm_scope + memo = getattr(session, "_sheet_read_memo", None) + if not isinstance(memo, dict): + memo = getattr(session, "_sheet_scope_context", None) + slot = ("audience", database) + if isinstance(memo, dict) and slot in memo: + return if perm_scope.audience_declared(database, st=session.runtime): raise err(409, "scope_not_applicable", f"{database} has an audience rule that limits which records each person may " "see, and a Sheet binding cannot apply that rule yet. Open the database from " "the navigation, where the rule is applied, or ask an administrator.") + if isinstance(memo, dict): + memo[slot] = True def _mirror(session: Session): @@ -326,7 +334,7 @@ def _amazon_stored_spec(cur, table_key): return spec -def _registry_assembly(session: Session, table_key: str): +def _registry_assembly(session: Session, table_key: str, *, with_rows: bool = True, needed=None): """The `g` dict `/workspace` builds for one compiled registry grid, or a NAMED refusal. ⭐⭐ W53-T13 — THIS IS THE FIX, AND IT IS A SECOND CALLER OF AN ESTABLISHED PATH RATHER THAN A @@ -366,22 +374,27 @@ def _registry_assembly(session: Session, table_key: str): if memo is None: memo = {} session._sheet_assemblies = memo - if table_key in memo: - return memo[table_key] + needed = None if needed is None else frozenset(str(key) for key in needed) + shape = ("full" if needed is None else ("columns", tuple(sorted(needed)))) if with_rows else "fields" + slot = (table_key, shape) + if slot in memo: + return memo[slot] try: # W62 C2: a placed ut_* view needs its own field vocabulary, without fetching its rows. if table_key.startswith("ut_"): from routes_tables import ut_assembly - memo[table_key] = ut_assembly(session, table_key, consume_corrections=False, with_rows=False) - return memo[table_key] + memo[slot] = ut_assembly(session, table_key, consume_corrections=False, + with_rows=False, fields_only=not with_rows, needed=needed) + return memo[slot] if table_key == "product_data": from routes_products import product_assembly - memo[table_key] = product_assembly(session, scope="product", - consume_corrections=False) - return memo[table_key] + memo[slot] = product_assembly(session, scope="product", + consume_corrections=False, with_rows=with_rows, needed=needed) + return memo[slot] from routes_customers import grid_assembly - memo[table_key] = grid_assembly(session, scope="customer", consume_corrections=False) - return memo[table_key] + memo[slot] = grid_assembly(session, scope="customer", consume_corrections=False, + with_rows=with_rows, needed=needed) + return memo[slot] except perm_scope.Denied: raise err(403, "database_unreadable", f"You cannot read {table_key}.") except HTTPException: @@ -401,6 +414,12 @@ def _registry_assembly(session: Session, table_key: str): "shown rather than shown from an older set of columns. Try again in a moment.") +def _sheet_permission_reader(session): + """Lend a captured schema/grant snapshot only inside this request's offer pass.""" + memo = getattr(session, "_sheet_read_memo", None) + return memo.get("permission_reader", session.runtime) if isinstance(memo, dict) else session.runtime + + def _related_declarations(session, database): """The same permission-scoped catalogue for reads, rewrites and the picker. @@ -416,12 +435,14 @@ def _related_declarations(session, database): from core import perm_scope, relations, user_tables from routes_grid import _source_fields - fields = _source_fields(session, database) + reader = _sheet_permission_reader(session) + fields = (_source_fields(session, database) if reader is session.runtime else + _source_fields(session, database, st=reader)) defs = {} - for key, definition in (user_tables.all_defs(st=session.runtime) or {}).items(): - if perm_scope.nav_may_open(session.user, key, st=session.runtime): + for key, definition in (user_tables.all_defs(st=reader) or {}).items(): + if perm_scope.nav_may_open(session.user, key, st=reader): declared = definition.get("fields") or [] - hidden = perm_scope.hidden_keys(session.user, key, declared, st=session.runtime) + hidden = perm_scope.hidden_keys(session.user, key, declared, st=reader) defs[key] = {**definition, "fields": [f for f in declared if f.get("key") not in hidden]} entries = relations.related(database, fields=fields, defs=defs) # S66-T13 / C2: `relations.bridge_entries` lists a bridge only from its SOURCE, so @@ -457,12 +478,19 @@ def _legacy_linked_keys(database, entry): def _related_condition_fields(session, database): from core import perm_scope + memo = getattr(session, "_sheet_read_memo", None) + slot = ("condition_fields", database) + if isinstance(memo, dict) and slot in memo: + return memo[slot] _require_database(session, database) - fields = list(_registry_assembly(session, database).get("fields") or []) - hidden = perm_scope.hidden_keys(session.user, database, fields, st=session.runtime) - return [field for field in fields if field.get("key") not in hidden - and field.get("type") not in {"formula", "link", "lookup", "rollup", "metric", "measure"} - and not field.get("measure")] + fields = list(_registry_assembly(session, database, with_rows=False).get("fields") or []) + hidden = perm_scope.hidden_keys(session.user, database, fields, st=_sheet_permission_reader(session)) + answer = [field for field in fields if field.get("key") not in hidden + and field.get("type") not in {"formula", "link", "lookup", "rollup", "metric", "measure"} + and not field.get("measure")] + if isinstance(memo, dict): + memo[slot] = answer + return answer def _rewrite_related_filters(session, database, nodes): @@ -496,8 +524,15 @@ def _rewrite_related_filters(session, database, nodes): def _related_filter_database_label(session, defs, target): from core import registry from routes_nav import _read_nav_meta + memo = getattr(session, "_sheet_read_memo", None) + if isinstance(memo, dict): + if "nav_meta" not in memo: + memo["nav_meta"] = _read_nav_meta(session.runtime) + metadata = memo["nav_meta"] + else: + metadata = _read_nav_meta(session.runtime) topic = registry.BY_KEY.get(target) or {} - return (_read_nav_meta(session.runtime).get(target, {}).get("name") + return (metadata.get(target, {}).get("name") or defs.get(target, {}).get("label") or topic.get("label") or target) @@ -520,6 +555,156 @@ def _distinct_page(cur, sql, params, offset, limit, search, *, numeric=False): "limit": limit, "hasMore": has_more} +def _pivot_fields(pivot): + from modules import sheet as sheet_mod + return (set(pivot.get("rows", [])) | set(pivot.get("columns", [])) + | {value["field"] for value in pivot["values"]} + | sheet_mod.filter_columns_named(pivot.get("filters", []))) + + +def _pivot_unsupported(): + """Fields the workspace schema offers that no mirror column backs, keyed to their label. + + Today that is the routing Zone overlay `user_tables.with_zone_field` appends.""" + from core import user_tables + return {user_tables.ZONE_FIELD_KEY: "Zone"} + + +def _check_pivot_fields(session, pivot, spec, columns, *, filter_fields=None): + """Reject every unreadable axis, value or filter, including COUNT's named field.""" + from core import perm_scope + from modules import sheet as sheet_mod + fields = spec.get("fields") or [] + named = set(pivot.get("rows", [])) | set(pivot.get("columns", [])) + named.update(value["field"] for value in pivot["values"]) + named.update(sheet_mod.filter_columns_named(pivot.get("filters", [])) + if filter_fields is None else filter_fields) + hidden = perm_scope.hidden_keys(session.user, pivot["database"], fields, st=session.runtime) + unsupported = _pivot_unsupported() + for field in sorted(named): + if field not in hidden and field in unsupported \ + and (field not in columns or not columns[field].get("sql")): + # Readable but derived outside the mirror: a named limit, never a permission refusal, + # which the pivot client reads as lost access and clears every offered field. + raise err(409, "pivot_field_unsupported", + f"{unsupported[field]} cannot be used in a pivot yet. Choose another field.") + if field in hidden or field not in columns or not columns[field].get("sql"): + raise err(403, "field_unreadable", + f"You cannot read field {field} in {pivot['database']}.") + definitions = {field["key"]: field for field in fields} + for field in set(pivot.get("rows", [])) | set(pivot.get("columns", [])): + if (definitions.get(field) or {}).get("type") == "formula": + refusal = sheet_mod.aggregate_refusal({"fn": "sum"}, definitions[field]) + raise err(409, refusal.code, refusal.message) + for value in pivot["values"]: + # C6 cells are numbers or null. MIN/MAX over text would violate that wire contract. + fn = "sum" if value["agg"] in ("min", "max") else value["agg"] + refusal = sheet_mod.aggregate_refusal({"fn": fn}, definitions.get(value["field"])) + if refusal is not None: + raise err(409, refusal.code, refusal.message) + + +#: One pivot's dense cell matrix, which the tab paints as one table. Customers by month (about +#: 148,000 cells for tenant #0) fits; a 1.9 million cell pivot never painted in the browser, and +#: row ids by order numbers asked for billions in one process. +_PIVOT_MAX_CELLS = 250_000 + + +def _pivot_result(cur, spec, columns, pivot, count_sql, params, limits): + """Group the whole scoped source on the server, with no row or group-page truncation. + + Axes are arrays of field-value tuples in the requested field order. With multiple values, + each column tuple ends in {field, agg}, in authored value order. Missing intersections are + null. With no fields on an axis its tuple is [], including an empty population's total. + """ + from modules import sheet as sheet_mod + import routes_odoo_tables as rot + + row_fields, column_fields = pivot.get("rows", []), pivot.get("columns", []) + groups = [rot._fs().group_sql(columns[field]) for field in [*row_fields, *column_fields]] + expressions = [] + for value in pivot["values"]: + field = value["field"] + # Native payloads can store numeric fields as JSON text. Cast the trusted source + # expression, as the Data aggregate compiler does, without rounding or filling nulls. + numeric = {**columns[field], "sql": f"TRY_CAST({columns[field]['sql']} AS DOUBLE)"} + expressions.append(sheet_mod.aggregate_sql( + spec, {"fn": value["agg"], "field": field}, {**columns, field: numeric})[0]) + total_rows = int(cur.execute(count_sql, params).fetchone()[0]) + tail = count_sql[len("SELECT COUNT(*)"):] + sql = "SELECT " + ", ".join([*groups, *expressions]) + tail + if groups: + positions = ", ".join(str(index + 1) for index in range(len(groups))) + sql += " GROUP BY " + positions + " ORDER BY " + positions + records = cur.execute(sql, params).fetchall() + row_keys, column_keys = {}, {} + if not row_fields: + row_keys[()] = 0 + if not column_fields: + column_keys[()] = 0 + values = {} + for record in records: + row_key = tuple(rot._group_key(value) for value in record[:len(row_fields)]) + column_key = tuple(rot._group_key(value) + for value in record[len(row_fields):len(groups)]) + row_keys.setdefault(row_key, len(row_keys)) + column_keys.setdefault(column_key, len(column_keys)) + values[row_key, column_key] = [rot._agg_number(value) for value in record[len(groups):]] + # The matrix below is dense, so its size is rows x columns x values whatever the data holds. + # Standing rule 1: a bound that cannot be lifted is reported with its cause and its fix. + cells_needed = len(row_keys) * len(column_keys) * len(pivot["values"]) + if cells_needed > _PIVOT_MAX_CELLS: + raise err(409, "pivot_too_large", + f"This pivot needs {len(row_keys):,} rows by {len(column_keys):,} columns, " + f"{cells_needed:,} cells, and one pivot holds at most {_PIVOT_MAX_CELLS:,}. " + "Move a field to Filters, or use a field with fewer distinct values.") + # SQL orders the entire tuple, but a column tuple may first occur in a later row group. + # Sort each axis independently using its JSON spelling for deterministic mixed/null keys. + ordered_rows = sorted(row_keys, key=lambda key: json.dumps(key, ensure_ascii=False)) + ordered_columns = sorted(column_keys, key=lambda key: json.dumps(key, ensure_ascii=False)) + multi = len(pivot["values"]) > 1 + headers = [list(key) + ([{"field": value["field"], "agg": value["agg"]}] if multi else []) + for key in ordered_columns for value in pivot["values"]] + cells = [[values.get((row, column), [None] * len(pivot["values"]))[index] + for column in ordered_columns for index in range(len(pivot["values"]))] + for row in ordered_rows] + return {"ok": True, "rows": [list(key) for key in ordered_rows], "columns": headers, + "cells": cells, "total_rows": total_rows, "limits": list(limits)} + + +@router.post("/sheets/pivot") +def pivot_sheet(body: dict = Body(default=None), session: Session = Depends(require_session)): + """C6: read a pivot definition without saving a workbook or changing source records.""" + from core import sheets + import odoo_relational as rel + import routes_odoo_tables as rot + try: + pivot = sheets.clean_pivot(body) + except sheets.SheetRefusal as exc: + raise _refuse(exc) + database = _require_database(session, pivot["database"]) + _refuse_unapplied_audience(session, database) + # Storage retains authored bytes; access decisions always use the admitted canonical key. + pivot = {**pivot, "database": database} + with _sheet_request_scope(session): + # Native Data databases have no connector mirror. Reuse the Data aggregate door's + # full, already walled payload and compiler; its temporary cursor closes on exit. + if database.startswith("ut_") and database != rel.SHIPMENTS_KEY \ + and rot._materialised_table(database): + query = {"filters": pivot.get("filters", []), "filterConj": "and", "search": ""} + with rot._materialised_read(database, session, query, None) as read: + spec = {**read["spec"], "fields": read["payload"]["fields"]} + _check_pivot_fields(session, pivot, spec, read["cols"]) + count_sql = "SELECT COUNT(*) FROM " + spec["table"] + if read["where"]: + count_sql += " WHERE " + read["where"] + return _pivot_result(read["cur"], spec, read["cols"], pivot, count_sql, + read["params"], read["limits"]) + binding = {"database": database, "aggregate": {"fn": "count"}, + "scope": {"filter": pivot.get("filters", [])}} + return _resolve_binding(session, binding, _pivot=pivot) + + @router.get("/sheets/distinct/{database}") def distinct_values(database: str, field: str = Query(...), offset: int = Query(0, ge=0), limit: int = Query(200, ge=1, le=1000), @@ -575,6 +760,29 @@ def distinct_values(database: str, field: str = Query(...), @router.get("/sheets/filter-fields/{database}") def linked_filter_fields(database: str, session: Session = Depends(require_session)): """Offer ordinary conditions grouped by their readable related database.""" + from copy import copy + from core import shares, user_tables + from routes_customers import _AssemblyStore + borrowed = copy(session) + borrowed.runtime = _AssemblyStore(session.runtime, schema_only=True) + with _sheet_request_scope(borrowed) as memo: + # The permission wall reads definitions and grants only. get_one on the assembly + # reader would fetch each target's full stored entry, including its rows. The lend + # preserves the canonical wall while reusing rows-free definitions and one current + # grant document; it dies with this request and is never used for writes or rows. + definitions = user_tables.all_defs(st=borrowed.runtime) + try: + grants = borrowed.runtime.get(shares.SHARES_KEY, fresh=True) or {} + except Exception: + grants = {} # The canonical field/database grant wall fails closed on a read error. + reader = user_tables.lend(borrowed.runtime, **{ + user_tables.STORE_KEY: definitions, shares.SHARES_KEY: grants}) + if isinstance(memo, dict): + memo["permission_reader"] = reader + return _linked_filter_fields(database, borrowed) + + +def _linked_filter_fields(database, session): from core import perm_scope, relations database = _require_database(session, database) _fields, defs, entries = _related_declarations(session, database) @@ -596,7 +804,7 @@ def linked_filter_fields(database: str, session: Session = Depends(require_sessi if not candidate.get("served"): omit(candidate, "unavailable") continue - if not perm_scope.nav_may_open(session.user, candidate["target"], st=session.runtime): + if not perm_scope.nav_may_open(session.user, candidate["target"], st=_sheet_permission_reader(session)): omit(candidate, "no_access") continue identity = relations.edge_identity(candidate) @@ -655,9 +863,10 @@ def _registry_mirror(session: Session, cur, table_key: str, metric_window, neede be a second answer to that question, from the one door that never asked it. ⭐⭐ W54-T36 — `needed` IS THE COLUMN SET THIS CELL CAN POSSIBLY READ, AND `None` IS EVERY - COLUMN. It narrows what gets MATERIALISED, never what gets assembled: `g` is the same scoped - assembly, `rows` are the same scoped rows, and `spec["fields"]` below is still the FULL list - so a refusal can name a column the mirror did not carry. + COLUMN. The existing assembly keeps its full permitted definitions and row-wall inputs, + while unused metric cells and view-condition sets are not computed. Rows retain the same + authorized ids and `spec["fields"]` keeps the full list so a refusal can name a column the + mirror did not carry. The canonical selection belongs to the existing assembly identity. ⛔⛔ IT IS INTERSECTED WITH THE ASSEMBLY'S OWN FIELDS AND MUST STAY THAT WAY. `cols` is what `_resolve_binding` subtracts a row wall's columns from to decide whether the wall can be @@ -670,11 +879,15 @@ def _registry_mirror(session: Session, cur, table_key: str, metric_window, neede import routes_odoo_tables as rot from modules import sheet as sheet_mod - g = _registry_assembly(session, table_key) + g = _registry_assembly(session, table_key, needed=needed) fields = list(g.get("fields") or []) - derived, bounded, limits = _metric_derived(session, table_key, g, metric_window) - rows = aios_grid.rows_from_pool(g["rows_src"], fields, (g.get("ws") or {}).get("overlays"), - derived=derived) + kept = (fields if needed is None else [f for f in fields if str(f.get("key") or "") in needed]) + metric_input = g if needed is None else {**g, "fields": kept} + derived, bounded, limits = _metric_derived(session, table_key, metric_input, metric_window) + # Authorization has already scoped rows using the full wall vocabulary. Restrict only + # cell materialization; an empty selection is ids-only, not rows_from_pool's all-fields default. + rows = (aios_grid.rows_from_pool(g["rows_src"], kept, (g.get("ws") or {}).get("overlays"), + derived=derived) if kept else [{"pid": row["pid"]} for row in g["rows_src"]]) if table_key == "customer_data" and (needed is None or "agent_id" in needed): from core import perm_scope agent_field = {"key": "agent_id", "type": "int", "label": "Current agent ID"} @@ -1144,7 +1357,7 @@ def _role_on_workbook(session: Session, book): def _require_reader(session: Session, book): - """R28: use the same predicate as the index; hidden and missing ids answer identically.""" + """R31 / C7: share the index predicate; hidden and missing ids answer identically.""" from core import sheets if not sheets.may_read_workbook(book, _role_on_workbook(session, book)): raise _refuse(sheets.SheetRefusal("workbook_missing", "That workbook no longer exists.")) @@ -1377,7 +1590,9 @@ def _reresolved_for_reader(session: Session, book, *, endings_only=False): try: cache = sheet_binding.read_refresh_cache(_store(session), book["id"]) except Exception: - cache = {} + # Preserve the legacy projection fallback on a failed whole read, once per workbook. + from core import sheets as _sheets + cache = _sheets._read(_store(session), _sheets._scope_cache_key(book["id"])) seen = {} today = _sheet_today(session) changed_values = False @@ -1414,11 +1629,12 @@ def _reresolved_for_reader(session: Session, book, *, endings_only=False): # new cache from the gateway instead was tried and REJECTED: it makes # `refreshCells` non-empty, which retires the pre-C2 passthrough below for every # OTHER cell and blanked a bound cell that had a perfectly good stored value. - try: - from core import sheets as _sheets - legacy = _sheets.cached_scope_value( - _store(session), book["id"], str(tab.get("id")) + "!" + addr, slot) - except Exception: + # Both cache formats share this document. The fresh request read above already + # holds the legacy slots too; reopening it for every bound cell costs hundreds + # of copies/PG reads on Daily Sales without improving permission freshness. + legacy_slots = (cache.get("cells") or {}).get(str(tab.get("id")) + "!" + addr) + legacy = legacy_slots.get(slot) if isinstance(legacy_slots, dict) else None + if not isinstance(legacy, dict): legacy = None if legacy is not None: value = legacy.get("value") @@ -1486,8 +1702,8 @@ def _response_workbook(book): def _read_for(session: Session, book): """Project saved results under the current access wall without resolving any binding. - W60-T09, 2026-09-14: R28 checks visibility before this projection. Admitted legacy readers - and grantees receive a proven scoped snapshot or a request to Refresh. + R31 / C7 checks visibility before this projection. Admitted non-owners receive a proven + scoped snapshot or a request to Refresh. """ book = _response_workbook(book) return _reresolved_for_reader(session, book) @@ -1552,10 +1768,9 @@ def create_sheet_from_template(key: str, body: dict = Body(default=None), # ⚠ `Depends(require_session)` ONLY, like every other door in this file, and that is correct: the # Sheet is granted to everyone, and the rail's folders name no database. # -# W60-T09, 2026-09-14: R28 supersedes the W51-T25 amendment finding. Both lists and all three -# workbook read shapes use core.sheets.may_read_workbook with _role_on_workbook. New workbooks -# are private to their creator and grantees; administrators see all. Unmarked or creatorless -# documents retain legacy visibility. Admitted non-owners still re-resolve under their own scope. +# R31 / C7: both lists and all three workbook read shapes use core.sheets.may_read_workbook. +# Only creators, grantees and administrators may read, including legacy documents. Admitted +# non-owners still receive their own scoped results through the existing projection. @router.get("/sheets/index") @@ -1582,12 +1797,10 @@ def write_sheet_index(body: dict = Body(default=None), payload, and the rail says so in a sentence rather than letting a workbook reappear at the top level with no explanation (R16). Dropping this key is what makes a silent repair. - ⭐⭐ D-639 — `rev` IN THE BODY MAKES THIS WRITE CONDITIONAL, and it is the half of the guard - that lives on the server. Send the `rev` the GET handed you and a concurrent reorganisation + ⭐⭐ D-639 — `rev` IS REQUIRED FOR A WHOLESALE WRITE. Send the `rev` the GET handed you and + a concurrent reorganisation answers 409 `index_revision_stale` carrying `rev_current`, which `_refuse`'s existing - `_stale` branch shapes for free. ⚠ Omitting it is still legal and still means last writer - wins, exactly as it does on the workbook PUT: a client that has never read the rail cannot - send one. + `_stale` branch shapes for free. A placements-only delta keeps its independent merge door. """ from core import sheets raw = dict(body or {}) @@ -1615,6 +1828,11 @@ def write_sheet_index(body: dict = Body(default=None), "That workbook no longer exists.") out = sheets.merge_placements(_store(session), raw["placements"]) else: + revision = raw.get("rev") + if isinstance(revision, bool) or not isinstance(revision, int) or revision < 0: + raise sheets.SheetRefusal( + "index_revision_required", + "A Sheet rail revision is required for this save. Reload the rail and try again.") folders = raw.get("folders") item_folders = raw.get("itemFolders") if not session.admin: @@ -1680,7 +1898,7 @@ def write_sheet_index(body: dict = Body(default=None), folders = incoming out = sheets.save_folders(_store(session), folders, item_folders, starred=raw.get("starred"), - expect_rev=raw.get("rev"), + expect_rev=revision, writable_ids=None if session.admin else visible_ids, writable_folder_ids=(None if session.admin else visible_folder_ids)) @@ -1816,14 +2034,17 @@ def _import_sentence(report): """What a finished import SAYS, in digits (R16). No em dash and no en dash.""" compared = int(report.get("compared") or 0) matched = int(report.get("matched") or 0) - line = ("Imported " + str(int(report.get("cells") or 0)) + " cells and " - + str(int(report.get("formulas") or 0)) + " formulas.") + cells = int(report.get("cells") or 0) + formulas = int(report.get("formulas") or 0) + line = (f"Imported {cells} cell{'' if cells == 1 else 's'} and " + f"{formulas} formula{'' if formulas == 1 else 's'}.") if compared: - line += (" " + str(matched) + " of " + str(compared) - + " recalculated cells match the answers the file had stored.") + line += (f" Matched {matched} of {compared} recalculated cell{'' if compared == 1 else 's'} against the " + f"{'answer' if compared == 1 else 'answers'} the file had stored.") if report.get("unsupported"): - line += (" " + str(len(report["unsupported"])) - + " kinds of thing in that file could not be carried over.") + kinds = len(report["unsupported"]) + line += (f" {kinds} kind{'' if kinds == 1 else 's'} of thing " + "in that file could not be carried over.") return line @@ -1832,7 +2053,7 @@ async def start_sheet_import(file: UploadFile = File(...), session: Session = Depends(require_session)): """Take an .xlsx upload and START a job (contract C4). Answers `{job, state, progress}`. - ⚠ THE TWO REFUSALS HERE ARE SYNCHRONOUS ON PURPOSE. A wrong file type and a file over the cap + ⚠ UPLOAD REFUSALS ARE SYNCHRONOUS ON PURPOSE. A wrong file type and a file over the cap are both knowable before any work starts, so answering them as a job the caller has to poll would make the client wait to be told something the server already knew. """ @@ -1842,6 +2063,13 @@ async def start_sheet_import(file: UploadFile = File(...), if not name.lower().endswith(".xlsx"): raise err(400, "import_not_xlsx", "Only an .xlsx workbook can be imported. Save your file as .xlsx and try again.") + declared_size = getattr(file, "size", None) + if isinstance(declared_size, int) and declared_size > sheet_import.MAX_IMPORT_BYTES: + raise HTTPException(status_code=413, detail={"error": { + "code": "import_too_large", + "message": ("That file is " + _mb(declared_size) + " and an import takes at most " + + _mb(sheet_import.MAX_IMPORT_BYTES) + + ", so it cannot be read. Split it into two workbooks and try again.")}}) # Read at most one byte past the cap. UploadFile may be backed by a large temporary file; an # unbounded read would allocate hostile input merely to discover that it is too large. data = await file.read(sheet_import.MAX_IMPORT_BYTES + 1) @@ -2327,11 +2555,10 @@ def share_sheet(workbook_id: str, body: dict = Body(default=None), rec = shares.set_grants("workbook", book["id"], entries, owner=(None if held.get("owner") else _workbook_owner(book)), granter=session.uname, st=_store(session), expect=prior) - # A successful explicit share narrows legacy visibility; ordinary saves never do. + # Preserve explicit sharing's document marker; R31 admission uses ownership and grants. if "access" not in book: - # W60-T09, 2026-09-14: the first share decides VISIBILITY, making the owner gate load bearing. - # A non-owner must not reach this narrowing on an ownerless legacy workbook - # and lock themselves out by sharing it. + # Keep the existing first-share stamp on legacy documents. The owner gate above + # still guards who may change grants, including on an ownerless workbook. sheets.narrow_workbook(_store(session), book["id"]) except sheets.SheetRefusal as exc: raise _refuse(exc) @@ -2803,8 +3030,14 @@ def _forget_sheet_database(session, database): getattr(session, "_sheet_assemblies", {}).pop(database, None) memo = getattr(session, "_sheet_read_memo", None) if isinstance(memo, dict): - for slot in [slot for slot in memo if isinstance(slot, tuple) and slot[0] == "related"]: + for slot in [slot for slot in memo if isinstance(slot, tuple) + and slot[0] in {"related", "condition_fields"}]: memo.pop(slot, None) + memo.pop(("audience", database), None) + memo.pop("permission_reader", None) + context = getattr(session, "_sheet_scope_context", None) + if isinstance(context, dict): + context.pop(("audience", database), None) mirrors = getattr(session, "_sheet_mirrors", {}) for slot in list(mirrors): if slot[0] == database: @@ -2944,10 +3177,14 @@ def _sheet_request_scope(session): reads of the same document object (the Daily Sales repair projects in rounds and does not). """ previous = getattr(session, "_sheet_read_memo", None) + previous_assemblies = getattr(session, "_sheet_assemblies", None) + previous_context = getattr(session, "_sheet_scope_context", None) memo = {"today": _sheet_today(session), "stats": {"engines": 0, "resolves": 0, "input_hits": 0}} try: session._sheet_read_memo = memo + session._sheet_assemblies = {} + session._sheet_scope_context = {} except Exception: # noqa: BLE001 yield None return @@ -2955,6 +3192,8 @@ def _sheet_request_scope(session): yield memo finally: session._sheet_read_memo = previous + session._sheet_assemblies = previous_assemblies + session._sheet_scope_context = previous_context def _request_scoped(route): @@ -3318,7 +3557,7 @@ def _read_binding_cell(session, ref, *, book=None, tab=None, today=None, seen=() cell = cells.get(key) if not any(fr.a1_key("A1", name).rpartition("!")[0] == key.rpartition("!")[0] for name in names): - refuse("the cell or its sheet is missing or blank.") + refuse("the referenced sheet is missing.") if isinstance(cell, dict) and cell.get("state") in ("refused", "stale") \ and cell.get("kind") != "formula" and cell.get("kind") != "bound": refuse((cell.get("refusal") or {}).get("message") or @@ -3456,7 +3695,7 @@ def _binding_related_entry(session, database, name): entry = relations.pick(entries, via) if entry is None: candidates = [item["key"] for item in relations.via_candidates(entries, via) - if perm_scope.nav_may_open(session.user, item["target"], st=session.runtime)] + if perm_scope.nav_may_open(session.user, item["target"], st=_sheet_permission_reader(session))] offered = (" Available relationships: " + ", ".join(candidates) + ".") if candidates else "" # Write our own sentence: the shared pin refusal does not name via.key. raise err(409, "unknown_relation", @@ -3596,7 +3835,7 @@ def _binding_filter_nodes(session, database, binding, *, memo=None): def _resolve_binding(session: Session, raw_binding, *, today=None, view_memo=None, - book=None, tab=None, ending_seen=()): + book=None, tab=None, ending_seen=(), _pivot=None): """Evaluate ONE C2 binding and return `{value, limits, sql_columns}`. The order below is the contract, not a preference: @@ -3652,10 +3891,26 @@ def _resolve_binding(session: Session, raw_binding, *, today=None, view_memo=Non | sheet_mod.filter_columns_named(wall_nodes)) if binding["aggregate"].get("field"): needed.add(str(binding["aggregate"]["field"])) + if _pivot is not None: + needed.update(_pivot_fields(_pivot)) related_bags = _related_leaf_bags(session, database, scope_nodes, needed) cur, spec, cols = _mirror(session)(database, metric_window=window, needed=needed) cols = dict(cols) + if _pivot is not None: + filter_fields = (sheet_mod.filter_columns_named(scope_nodes) + - sheet_mod.related_filter_ids(scope_nodes)) + _check_pivot_fields(session, _pivot, spec, cols, filter_fields=filter_fields) + # A known column does not prove that its row-wall condition was compiled. Refuse a + # skipped or aggregate wall rather than returning a pivot over a wider population. + try: + wall = sheet_mod.compile_scope(wall_nodes, wall_conj, cols, today=today) + if wall_nodes and (wall is None or wall.uses_aggregate + or sheet_mod.widen_limits(wall_nodes, cols, wall)): + raise ValueError("unapplied pivot row wall") + except (ValueError, sheet_mod.SheetRefusal): + raise err(409, "scope_not_applicable", + f"Your row restriction cannot be applied to {database}. Ask an administrator to review it.") # ⛔ THE COLUMN IS CHECKED BEFORE THE SQL IS BUILT (W53-T13). Now that every tenant column is a # real column of the materialised table, a `sum` over a text column reaches DuckDB as a binder @@ -3718,6 +3973,11 @@ def _resolve_binding(session: Session, raw_binding, *, today=None, view_memo=Non "live connection, so the number is not being shown rather than shown wider " "than you asked for. Edit the cell's scope and try again.") + if _pivot is not None: + pivot_limits = list(limits) + [item for field, item in (spec.get("unbounded") or {}).items() + if field in needed] + return _pivot_result(cur, spec, cols, _pivot, sql, params, pivot_limits) + row = cur.execute(sql, params).fetchone() value = row[0] if row else None # ⭐⭐ EVERY WAY THIS ANSWER CAME OUT WIDER THAN THE BINDING ASKED FOR TRAVELS WITH IT diff --git a/api/routes_tables.py b/api/routes_tables.py index b536ffdec56e952e3eac2f0c76a8779180136121..a87a2bfb4119658048a198aca9c3d59a3fefac86 100644 --- a/api/routes_tables.py +++ b/api/routes_tables.py @@ -199,8 +199,11 @@ def _ut(): def _table_lend(runtime, table_key, defs_only=False): """Borrow one entry for the wall and page; old snapshot stores retain their lend.""" ut = _ut() + if defs_only: + # Metadata must enter through the store projection, even on entry-capable stores. + return ut.lend_defs(runtime) if _ut()._entry_reader(runtime) is None: - return ut.lend_defs(runtime) if defs_only else ut.lend(runtime) + return ut.lend(runtime) try: entry = ut.get_one(table_key, st=runtime) except Exception as exc: @@ -1348,17 +1351,21 @@ def _field_usage(session, table_key, fields, defn, ws_st, ws_key, st=None): def ut_assembly(session: Session, table_key: str, storage_key: str = "", - consume_corrections: bool = False, with_rows: bool = True, st=None): + consume_corrections: bool = False, with_rows: bool = True, st=None, + *, fields_only: bool = False, needed=None): if not with_rows and st is None: from routes_customers import grid_assembly return grid_assembly(session, table_key, storage_key, - build=lambda borrowed: _compute_ut_assembly(borrowed, table_key, storage_key, False, False), - pool_stamp=lambda: 0) - return _compute_ut_assembly(session, table_key, storage_key, False, with_rows, st) + build=lambda borrowed: _compute_ut_assembly( + borrowed, table_key, storage_key, False, False, fields_only=fields_only, needed=needed), + pool_stamp=lambda: 0, with_rows=not fields_only, needed=needed) + return _compute_ut_assembly(session, table_key, storage_key, False, with_rows, st, + fields_only=fields_only, needed=needed) def _compute_ut_assembly(session: Session, table_key: str, storage_key: str = "", - consume_corrections: bool = False, with_rows: bool = True, st=None): + consume_corrections: bool = False, with_rows: bool = True, st=None, + *, fields_only: bool = False, needed=None): """The user-table mirror of `grid_assembly` / `product_assembly` — SAME g-dict keys, so `/workspace` and the events route consume any of the three interchangeably. @@ -1393,7 +1400,11 @@ def _compute_ut_assembly(session: Session, table_key: str, storage_key: str = "" # reading that bucket once per question asked of it. if st is None: st = _table_lend(session.runtime, table_key, defs_only=not with_rows) - if with_rows: + if fields_only: + defn = _defn_or_refuse(session, table_key, st=st, defs_only=True, scope_applied=True) + fields_base = list(defn.get("fields") or []) + pids, rows_src = frozenset(), [] + elif with_rows: # ⭐ W33-T03 (D-214): `st` is a caller's LEND, threaded to the wall and nowhere else. Only # a READ route passes one — see `scoped_pool`'s own note and contract C5. pids, rows_src, fields_base, defn = scoped_pool(session, table_key, st=st) @@ -1544,9 +1555,11 @@ def _compute_ut_assembly(session: Session, table_key: str, storage_key: str = "" # which is the honest answer for a hand-typed one rather than a descope. today = _recurrence_today measures = ut_measures(table_key, session) - derived = aios_grid.cohort_cells(lists) # R9: this table's own lists - for _pid, _cells in _ut_measure_cells(table_key, fields, pids, today, measures, - getattr(session.runtime, 'measure_memo', {})).items(): + derived = aios_grid.cohort_cells(lists) if not fields_only else {} + materialized_fields = fields if needed is None else [f for f in fields if f.get("key") in needed] + for _pid, _cells in (_ut_measure_cells(table_key, materialized_fields, pids, today, measures, + getattr(session.runtime, 'measure_memo', {})) + if not fields_only and (needed is None or aios_grid.measure_fields_of(materialized_fields)) else {}).items(): derived.setdefault(_pid, {}).update(_cells) # ⭐⭐ D-590 (wave 46 hotfix) — **THE PER-TENANT SCHEMA OVERLAY, APPLIED TO THE GRID READ.** @@ -1583,7 +1596,8 @@ def _compute_ut_assembly(session: Session, table_key: str, storage_key: str = "" # here (`table_rows`) is layering strata in a fixed order. "shared_cells": shared_cells, "measures": measures, - "measure_sets": _ut_measure_sets(table_key, views, measures, pids, today), + "measure_sets": (_ut_measure_sets(table_key, views, measures, pids, today) + if not fields_only and needed is None else {}), "today": today, # ⚠ ALWAYS PRESENT, EMPTY WHEN THERE IS NOTHING TO SAY — a key a consumer has to test # for is a key a consumer forgets to test for, and this one carries a refusal. diff --git a/api/telnyx_whatsapp.py b/api/telnyx_whatsapp.py index ea3387201178c7786e9259a748d9e8e428db91e0..ff9892ed14e035622c4918fcc809b1abd79c1a75 100644 --- a/api/telnyx_whatsapp.py +++ b/api/telnyx_whatsapp.py @@ -107,6 +107,10 @@ def _send(to, message, *, purpose, tenant, username, transport, store_): "name": message["template"], "language": {"code": "en_US"}, "components": [{"type": "body", "parameters": [ {"type": "text", "text": p} for p in message["params"]]}]}} + if purpose == "auth_code" and message["template"] == auth_template(): + whatsapp_message["template"]["components"].append({ + "type": "button", "sub_type": "url", "index": 0, + "parameters": [{"type": "text", "text": message["params"][0]}]}) else: whatsapp_message = {"type": "text", "text": {"body": message["text"]}} payload = {"from": sender, "to": to, "whatsapp_message": whatsapp_message} @@ -134,6 +138,107 @@ def _send(to, message, *, purpose, tenant, username, transport, store_): return copy.deepcopy(entry) +TELNYX_FAILED = {"delivery_failed", "sending_failed", "expired"} +UNDELIVERED = "WhatsApp could not deliver this message." + + +def _first_code(errors): + if isinstance(errors, list) and errors and isinstance(errors[0], dict): + return errors[0].get("code") + return None + + +def _status_updates(event_type, payload): + """Read (providerId, outcome, telnyxCode, metaCode) rows from one status event.""" + if not isinstance(payload, dict): + return [] + if event_type == "message.finalized": + provider_id = payload.get("id") + if payload.get("type") != "WHATSAPP" or not isinstance(provider_id, str) or not provider_id: + return [] + recipients = payload.get("to") if isinstance(payload.get("to"), list) else [] + states = {row.get("status") for row in recipients + if isinstance(row, dict) and isinstance(row.get("status"), str)} + if states & TELNYX_FAILED: + code = _first_code(payload.get("errors")) + return [(provider_id, "failed", None if code in (None, "") else str(code), None)] + return [(provider_id, "delivered", None, None)] if "delivered" in states else [] + if event_type == "whatsapp.messages": + rows = [] + for row in payload.get("statuses") if isinstance(payload.get("statuses"), list) else []: + # A malformed row is skipped: a status event never answers 400. + if (not isinstance(row, dict) or not isinstance(row.get("id"), str) or not row["id"] + or not isinstance(row.get("status"), str)): + continue + if row.get("status") == "failed": + code = _first_code(row.get("errors")) + if isinstance(code, str) and code.isdigit(): + code = int(code) + meta = code if isinstance(code, int) and not isinstance(code, bool) else None + rows.append((row["id"], "failed", None, meta)) + elif row.get("status") in {"delivered", "read"}: + rows.append((row["id"], "delivered", None, None)) + return rows + return [] + + +def _apply_status(entry, outcome, telnyx_code, meta_code): + before = dict(entry) + if outcome == "failed": + # Failed is terminal: a later event adds a code but never clears one. + entry.update(status="failed", error=UNDELIVERED) + if telnyx_code is not None: + entry["errorCode"] = telnyx_code + if meta_code is not None: + entry["metaCode"] = meta_code + elif entry.get("status") != "failed": + entry["status"] = "delivered" + return entry != before + + +def record_status(event_type, payload, *, store_=None): + """Apply a signed Telnyx delivery event to outbox entries by providerId; return entries changed.""" + rows = _status_updates(event_type, payload) + if not rows: + return 0 + selected = store_ if store_ is not None else _store + current = (selected.get(OUTBOX_KEY) or {}).get("messages") or [] + + def changes(messages): + changed = 0 + for entry in messages: + if not isinstance(entry, dict) or not entry.get("providerId"): + continue + for provider_id, outcome, telnyx_code, meta_code in rows: + if entry["providerId"] == provider_id: + changed += _apply_status(entry, outcome, telnyx_code, meta_code) + return changed + + # An unknown id or a repeated event writes nothing at all. + if not changes(copy.deepcopy(current)): + return 0 + outcome = {"changed": 0} + + def apply(doc): + doc["messages"] = list(doc.get("messages") or []) + outcome["changed"] = changes(doc["messages"]) + return doc + + selected.update(OUTBOX_KEY, apply) + return outcome["changed"] + + +def delivery_status(provider_id, *, store_=None): + """Return the outbox status of the newest entry sent as provider_id, or None.""" + if not provider_id: + return None + messages = ((store_ if store_ is not None else _store).get(OUTBOX_KEY) or {}).get("messages") or [] + for entry in reversed(messages): + if isinstance(entry, dict) and entry.get("providerId") == provider_id: + return entry.get("status") + return None + + def send_template(to, template, params, *, purpose, tenant="", username="", transport=None, store_=None): _check_text(template) diff --git a/api/test_odoo_rank_refresh.py b/api/test_odoo_rank_refresh.py index 9e245254fc56c65f0a318bb8cdce61e80c88274f..036061467b27b633bc8c83178ef8d0fd7914943f 100644 --- a/api/test_odoo_rank_refresh.py +++ b/api/test_odoo_rank_refresh.py @@ -171,7 +171,8 @@ class RankRefreshTests(unittest.TestCase): events.append(('sleep', seconds)) cycle[0] += 1 sleeps.append(seconds) - def sync(manual, passes): + def sync(manual, passes, entities): + self.assertIsNone(entities) # Existing tenants retain the full mirror selection. events.append(('sync', passes)) self.assertFalse(manual) self.assertEqual(passes, len(reports) + 1) @@ -193,7 +194,8 @@ class RankRefreshTests(unittest.TestCase): # every pass would ask for a zero wait. clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60, strftime=lambda *_: 'synthetic-time', gmtime=lambda: ()) - ns = dict(RESYNC_STATE={}, _RESYNC_NOTICE=[None], + ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], + _configured_mirror_entities=lambda *_: None, _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False, tenant='synthetic', tenantSource='test', tenantSeconds=60, envSeconds=60, disagreement=None), @@ -499,7 +501,8 @@ class RankRefreshTests(unittest.TestCase): raise StopIteration('bounded synthetic resident loop') return False - def sync(manual_flag, passes): + def sync(manual_flag, passes, entities): + self.assertIsNone(entities) events.append(('sync', passes)) err = failures.pop(0) if failures else None if err is not None: @@ -514,6 +517,7 @@ class RankRefreshTests(unittest.TestCase): clock = SimpleNamespace(sleep=sleep, time=lambda: now, strftime=lambda *_: 'synthetic-time', gmtime=lambda: ()) ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None], + _configured_mirror_entities=lambda *_: None, _resolve_resync_cadence=lambda: dict( resolvedSeconds=cadence, manual=manual, tenant='synthetic', tenantSource='test', tenantSeconds=cadence, envSeconds=cadence, diff --git a/api/whatsapp_door.py b/api/whatsapp_door.py index 1ab8bc09c62ccca18a115b27e36809d35868d5ef..82a633ec0f009799aaeaf642ae8ef970aae78104 100644 --- a/api/whatsapp_door.py +++ b/api/whatsapp_door.py @@ -146,13 +146,23 @@ def accept(raw: bytes, signature: str, timestamp: str, *, now=None): raise WhatsAppDoorRefusal("Telnyx webhook signature verification failed") try: data = json.loads(raw)["data"] + # A delivery status updates the outbox only: no receipt claim, no window stamp, no turn. + telnyx_whatsapp.record_status(data["event_type"], data.get("payload")) if data["event_type"] != "message.received": return None payload = data["payload"] message_id = payload["id"] phone = whatsapp_identity.normalize_phone(payload["from"]["phone_number"]) - text = payload.get("text", payload.get("body", "")) - if not isinstance(message_id, str) or not message_id.strip() or not isinstance(text, str): + if not isinstance(message_id, str) or not message_id.strip(): + raise ValueError("Invalid message fields") + body = payload.get("body", "") + # Edits and media can carry decoy text; ignore them before claiming receipt/window. + if isinstance(body, dict) and body.get("type") != "text": + return None + text = payload.get("text", body) + if isinstance(body, dict) and not isinstance(text, str): + text = body["text"]["body"] + if not isinstance(text, str): raise ValueError("Invalid message fields") except (ValueError, TypeError, KeyError) as exc: raise ValueError("The WhatsApp webhook payload is invalid.") from exc diff --git a/api/whatsapp_identity.py b/api/whatsapp_identity.py index 1281cde424ae2f351e979029bec35096b7ed7a4e..3173cd9b8502e21ac6078af19d412f6868facce5 100644 --- a/api/whatsapp_identity.py +++ b/api/whatsapp_identity.py @@ -22,6 +22,7 @@ LOCK_S = 900 PHONE_TAKEN = "This number is already connected to another Loopable account." CODE_WRONG = "That code is not right. Check the latest WhatsApp message and try again." CODE_EXPIRED = "That code has expired. Send a new code." +CODE_UNDELIVERED = "The code could not be delivered to this number." class WhatsAppRefusal(ValueError): @@ -145,6 +146,16 @@ def start(tenant, username, phone, *, now=None, sender=None): p: row for p, row in doc.items() if p != phone or row.get("codeHash") != pending["codeHash"]}) raise WhatsAppRefusal("The code could not be sent. Try again.") + provider_id = delivery.get("providerId") if isinstance(delivery, dict) else None + if provider_id: + # status() reads the outbox by this id, so a later refusal can reach the caller. + def remember(doc): + row = doc.get(phone) + if isinstance(row, dict) and row.get("codeHash") == pending["codeHash"]: + row["providerId"] = provider_id + return doc + + _store.update(CODES_KEY, remember) return {"state": "pending", "phone": phone, "expiresAt": pending["expiresAt"]} @@ -201,4 +212,6 @@ def status(tenant, username, *, now=None): "message": _lock_sentence(pending, now)} if pending["expiresAt"] <= now: return {"state": "none"} + if telnyx_whatsapp.delivery_status(pending.get("providerId")) == "failed": + return {"state": "undelivered", "phone": phone, "message": CODE_UNDELIVERED} return {"state": "pending", "phone": phone, "expiresAt": pending["expiresAt"]} diff --git a/platform/aios_grid.py b/platform/aios_grid.py index 676222493582485eb06e41d5096d2ef56dddd9cc..11a78e55ca859c3f26b14f1ab587ec98d85ab069 100644 --- a/platform/aios_grid.py +++ b/platform/aios_grid.py @@ -1921,7 +1921,12 @@ def rows_from_pool(pool_rows, fields=None, overlays=None, derived=None): row[k] = v if field["type"] in {"text", "status", "date"} else ( _round2(v) if field["type"] in {"currency", "pct"} else _round(v)) for field in overlay_fields: - row[field["key"]] = saved.get(field["key"], "") + k = field["key"] + # The pool may carry the mirror's sync stamp. It wins over a stale overlay, + # including an explicit blank; never manufacture freshness from _created or now. + row[k] = r.get(k, saved.get(k, "")) if ( + k == "refreshed" and (field.get("automation") or {}).get("preset") + ) else saved.get(k, "") got = derived.get(pid) or {} for k in derived_keys: # '' not None: a customer in no cohort has an EMPTY cohort list, and `is empty` on a diff --git a/platform/core/audit.py b/platform/core/audit.py index f156863ddab4447a877e5706f1f9cef326c87c2b..38036bf43e8a3532f645ec1acb9f3322a6c3a625 100644 --- a/platform/core/audit.py +++ b/platform/core/audit.py @@ -5,6 +5,7 @@ import re import time from harness import runtime +from core import freeze_notice AUDIT_KINDS = frozenset({ @@ -142,6 +143,8 @@ def ordered(doc, tenant, limit=200) -> list[dict]: def record(kind, tenant, principal, detail=None) -> dict | None: """Refusals raise before I/O. Store failures lose the event, never the user's sign-in.""" item = event(kind, tenant, principal, detail) + if freeze_notice.enabled(): + return None tenant = item["tenant"] def _append(doc): diff --git a/platform/core/data_binding.py b/platform/core/data_binding.py index ad3cb149883173fd5259578c8806f2b5b7c5119f..e0f117a3dea904e6f1f1c06b7e94da846e8b9f2f 100644 --- a/platform/core/data_binding.py +++ b/platform/core/data_binding.py @@ -49,6 +49,7 @@ per D-160 the Space environment cannot be read from outside, so the container ha saw. One deploy then answers "does SPACE_ID exist here", instead of three. """ import os +from core import freeze_notice #: The deployment that OWNS production data. A frozenset rather than a string because the product #: has had two public addresses before and will again. @@ -284,6 +285,7 @@ def write_refusal(repo): def check_write(repo, operation='write'): """Raise `StoreWriteRefused` unless this deployment may write `repo`. The one enforcement door.""" + freeze_notice.check_write() reason = gate_write_refusal(repo) or write_refusal(repo) if reason: raise StoreWriteRefused(f'{operation} to {repo!r} refused: {reason}') diff --git a/platform/core/external_oauth.py b/platform/core/external_oauth.py index b65981e8a22b86e95a1679968ecdb75254eda821..d92197dc7deefff0a7f59cebbb166a3a7a2a2ff4 100644 --- a/platform/core/external_oauth.py +++ b/platform/core/external_oauth.py @@ -14,7 +14,7 @@ import secrets import time from urllib.parse import urlsplit, urlunsplit -from core import external_access +from core import external_access, perm_scope, perms, registry, shares, user_tables, users BUCKET = "external_oauth" @@ -400,7 +400,9 @@ def authorize(st, username: str, tenant: str, *, client_id, redirect_uri, state, raise ExternalOAuthError("oauth_client_invalid") if redirect_uri not in (client.get("redirect_uris") or []): raise ExternalOAuthError("oauth_redirect_uri_invalid") - access, databases = external_access.scope_of(client) + # R15/C5: approval delegates the live account authority. Saved client settings and + # old grants remain readable metadata, never a cap on a future Manage users change. + access, databases = external_access.ACCESS_READWRITE, external_access.ALL_DATABASES scopes = _normalise_scopes(scope, access) states = {key: row for key, row in book["states"].items() if isinstance(row, dict) and float(row.get("expires_at") or 0) >= issued_at} @@ -604,6 +606,78 @@ def _intersect_databases(left, right): return tuple(item for item in (left or ()) if item in allowed) +def _live_permission_user(username: str, tenant: str) -> dict: + """Read and validate the raw account before its public projection can discard bad perms.""" + records = users.registry() + record = records.get(username) if isinstance(records, dict) else None + if not isinstance(record, dict) or record.get("active", True) is not True: + raise ExternalOAuthAuthError(AUTH_ERROR) + version = record.get("perms_v", 0) + if type(version) is not int or version < 0: + raise ExternalOAuthAuthError(AUTH_ERROR) + block = record.get("perms", {}) + if not isinstance(block, dict): + raise ExternalOAuthAuthError(AUTH_ERROR) + for key, entry in block.items(): + if not isinstance(key, str) or not key or not isinstance(entry, dict): + raise ExternalOAuthAuthError(AUTH_ERROR) + if "access" in entry and type(entry["access"]) is not bool: + raise ExternalOAuthAuthError(AUTH_ERROR) + if "metrics" in entry and type(entry["metrics"]) is not bool: + raise ExternalOAuthAuthError(AUTH_ERROR) + tree = entry.get("filter") + if tree is not None and (not isinstance(tree, dict) + or not isinstance(tree.get("nodes"), list) + or tree.get("conj", "and") not in {"and", "or"}): + raise ExternalOAuthAuthError(AUTH_ERROR) + if "hiddenFields" in entry and (not isinstance(entry["hiddenFields"], list) + or any(not isinstance(field, str) for field in entry["hiddenFields"])): + raise ExternalOAuthAuthError(AUTH_ERROR) + modules = record.get("modules", "all") + if modules != "all" and (not isinstance(modules, (list, tuple)) or not modules + or any(not isinstance(key, str) or not key for key in modules)): + raise ExternalOAuthAuthError(AUTH_ERROR) + public = users._public(username, record) + if public.get("tenant") != tenant: + raise ExternalOAuthAuthError(AUTH_ERROR) + return public + + +def _live_permission_scope(user: dict, st) -> tuple[str, object]: + """C5: resolve this call's database and editor authority, without reading database rows. + + The canonical evaluators preserve migrated topic denies and the distinct ut_* share rule. + The coarse write capability also permits owned Sheet work; each writer still checks its + own object, database, row and field authority. Neither client nor grant snapshots enter. + """ + if perms.is_admin(user): + return external_access.ACCESS_READWRITE, external_access.ALL_DATABASES + definitions = st.get_projection(user_tables.STORE_KEY, drop=user_tables.DEF_ONLY_DROPS) + if definitions is None: + definitions = {} + if not isinstance(definitions, dict) or any( + not isinstance(key, str) or not isinstance(value, dict) + for key, value in definitions.items()): + raise ExternalOAuthAuthError(AUTH_ERROR) + grants = st.get(shares.SHARES_KEY, fresh=True) + if grants is None: + grants = {} + if not isinstance(grants, dict): + raise ExternalOAuthAuthError(AUTH_ERROR) + if "database" in grants and not isinstance(grants["database"], dict): + raise ExternalOAuthAuthError(AUTH_ERROR) + lent = user_tables.lend(st, **{user_tables.STORE_KEY: definitions, shares.SHARES_KEY: grants}) + topics = {row["key"] for row in registry.governable_modules().values() + if perms.tenant_has_module(st, row["key"])} + candidates = topics | {key for key in definitions if key.startswith(user_tables.KEY_PREFIX)} + databases = tuple(key for key in sorted(candidates) if perm_scope.may_read(user, key, st=lent)) + editable = any(key in topics or user_tables.may_edit_database( + key, user["username"], st=lent) for key in databases) + editable = editable or (perms.tenant_has_module(st, "sheets") + and perm_scope.nav_may_open(user, "sheets", st=lent)) + return (external_access.ACCESS_READWRITE if editable else external_access.ACCESS_READ), databases + + def verify_access(token: str, *, resource, runtime_resolver) -> VerifiedOAuth: try: _, tenant_key, grant_id, secret = _parse_token(token, {"lpo1"}) @@ -626,17 +700,11 @@ def verify_access(token: str, *, resource, runtime_resolver) -> VerifiedOAuth: or client.get("username") != row.get("username") or client.get("tenant") != tenant_key): raise ExternalOAuthAuthError(AUTH_ERROR) - try: - public = external_access.resolve_active_user(row["username"], tenant_key) - except external_access.ExternalAccessError as exc: - raise ExternalOAuthAuthError(AUTH_ERROR) from exc - grant_access, grant_databases = external_access.scope_of(row) - client_access, client_databases = external_access.scope_of(client) - access = (external_access.ACCESS_READWRITE - if grant_access == client_access == external_access.ACCESS_READWRITE - and "mcp:write" in (row.get("scopes") or ()) - else external_access.ACCESS_READ) - databases = _intersect_databases(grant_databases, client_databases) + public = _live_permission_user(row["username"], tenant_key) + access, databases = _live_permission_scope(public, st) + scopes = ("mcp:read",) + (("mcp:write",) if external_access.may_write(access) else ()) + if "offline_access" in (row.get("scopes") or ()): + scopes += ("offline_access",) if now - float(row.get("last_used_at") or 0) >= _STAMP_EVERY: def _stamp(value): latest = _book_value(value) @@ -652,7 +720,7 @@ def verify_access(token: str, *, resource, runtime_resolver) -> VerifiedOAuth: return latest _update(st, _stamp, auth=True) return VerifiedOAuth(tenant_key, grant_id, row["client_id"], public, st, access, - databases, tuple(row.get("scopes") or ()), audience) + databases, scopes, audience) except ExternalOAuthAuthError: raise except Exception as exc: diff --git a/platform/core/field_permissions.py b/platform/core/field_permissions.py index 07babf781e8925caa3c562f28f4c1f6d3694ad43..e4dc1e38e8fc21c8cd3de607fd64516a823a7b73 100644 --- a/platform/core/field_permissions.py +++ b/platform/core/field_permissions.py @@ -1157,7 +1157,8 @@ def reconcile_shared_permissions(shared_key, grant_topic, st=None, shared_docume # agree without turning ungoverned contract fields into read-only fields. updated[grant_mark] = True updated["permissions"] = derived - shared_overlay.put_field(bucket, key, updated, st=st) + if not getattr(st, "schema_only", False): + shared_overlay.put_field(bucket, key, updated, st=st) if isinstance(shared_document, dict): shared_document.setdefault("fields", {})[key] = updated rewritten += 1 @@ -1374,6 +1375,30 @@ def migrate_legacy_fields(table_key, st=None, known_users=None, grant_topic=None if not key or st is None: return {"promoted": 0, "normalized": 0, "reclassified": 0, "suppressed": 0, "purged": 0, "folded": 0} + if getattr(st, "schema_only", False): + # The same migration decisions and grant classifier, over detached definitions only. + # A pending copy-before-remove operation belongs to the full read door. + fold_legacy_shared_bucket(shared_key, st=st) + document = (workspace_document if isinstance(workspace_document, dict) + else st.get_projection(key, drop=("overlays",))) + shared = (shared_document if isinstance(shared_document, dict) + else shared_overlay.document(shared_key, st=st)) + tombstoned = _live_tombstones(document) + for owner, workspace in (document or {}).items(): + if owner == WORKSPACE_SHARED_MEMBER or not isinstance(workspace, dict): + continue + for field_key, original in (workspace.get("fields") or {}).items(): + if (str(field_key) not in tombstoned and _field_is_migratable(original) + and stored_permissions(original, fallback="collaborative", + known_users=known_users)["edit"] != "personal"): + raise shared_overlay.SchemaUnavailable( + 'field definitions need a full workspace refresh to finish migration') + reclassified = reconcile_shared_permissions( + shared_key, grant_key, st=st, shared_document=shared) + for dead in tombstoned: + (shared.get("fields") or {}).pop(dead, None) + return {"promoted": 0, "normalized": 0, "reclassified": reclassified, + "suppressed": 0, "purged": 0, "folded": 0} # ⭐⭐ W50-T23 — THE FOLD RUNS BEFORE THE RECONCILE, AND THE ORDER IS THE POINT. A definition # that arrives from the legacy bucket must be classified by the same pass that classifies # every other one; folding after the reconcile would leave the newcomer unreconciled until diff --git a/platform/core/freeze_notice.py b/platform/core/freeze_notice.py new file mode 100644 index 0000000000000000000000000000000000000000..afb5f00e42d7ecb983e18ce575f5b2c98fe7a54c --- /dev/null +++ b/platform/core/freeze_notice.py @@ -0,0 +1,49 @@ +"""Deployment maintenance state, independent of sessions and stored documents.""" +import os +from contextlib import contextmanager +from contextvars import ContextVar + +ENV_NAME = "AIOS_FREEZE_NOTICE" +DEFAULT_TEXT = "Data input is frozen for maintenance." +REFUSAL_TEXT = "Data input is frozen for maintenance. Try again shortly." +ERROR_CODE = "data_input_frozen" +STATUS_CODE = 423 +_REFUSAL = ContextVar("aios_freeze_refusal", default=None) + + +class DataInputFrozen(RuntimeError): + """A maintenance freeze refused a durable mutation before acceptance.""" + + def __init__(self): + refusal = _REFUSAL.get() + if refusal is not None: + refusal[0] = True + super().__init__(REFUSAL_TEXT) + + +@contextmanager +def refusal_scope(): + """Track a refusal through route wrappers and copied threadpool contexts.""" + refusal = [False] + token = _REFUSAL.set(refusal) + try: + yield refusal + finally: + _REFUSAL.reset(token) + + +def enabled(): + return bool(os.environ.get(ENV_NAME)) + + +def notice(): + """Return only the public on/off state and notice text.""" + value = str(os.environ.get(ENV_NAME) or "") + text = DEFAULT_TEXT if value.lower() in {"1", "on"} else value + return {"enabled": bool(value), "text": text} + + +def check_write(): + """Refuse before any store permission check, cache mutation or network call.""" + if enabled(): + raise DataInputFrozen() diff --git a/platform/core/grid_events.py b/platform/core/grid_events.py index 325b5d0a13ecf012a4c4f4da734efb02b38e676f..4053c3fe1d82250160bd271f32530837fcb2ee98 100644 --- a/platform/core/grid_events.py +++ b/platform/core/grid_events.py @@ -1181,7 +1181,7 @@ def _refuse(ctx, code, message, key='', *, event='field_upsert'): #: the same 24-event window still overwrites it, because that branch assigns the slot directly. #: Widening `EventResult` to a message LIST is the real fix and it is not this ticket's file. def _folder_refuse(ctx, kind, code, message, key=''): - """Refuse ONE folder event out loud: the machine record AND the rendered sentence.""" + """Refuse one event out loud: the machine record AND the rendered sentence.""" try: out = getattr(ctx, 'out', None) if out is not None: @@ -1489,6 +1489,12 @@ def handle_one(event, ctx): requested_name = name if not view_id or not name: return False + display = raw['config'].get('display') + if isinstance(display, dict) and display.get('mode') == 'timeseries': + return _folder_refuse( + ctx, kind, 'view_mode_retired', + 'Time-series Views are retired. Choose an available View display mode.', + key=view_id) if 'permissions' in raw: return _refuse( ctx, 'view_permissions_retired', @@ -3350,8 +3356,12 @@ def handle_one(event, ctx): if kind == 'overlay_patch': pid = event.get('pid') - if not isinstance(pid, int) or pid not in allowed_pids: + if not isinstance(pid, int) or isinstance(pid, bool): return False + if pid not in allowed_pids: + return _folder_refuse( + ctx, kind, 'row_outside_audience', + 'This edit was not applied because the record is outside your permitted audience.') _history_table = str(ctx.scope_key or 'customer') if _history_table.startswith('ut_') and ctx.table is not None: import core.user_tables as _ut_history diff --git a/platform/core/keychain.py b/platform/core/keychain.py index d5b038c9d0efbff249e11edfdafefbadcc6fc5b6..fdf9acfe6d6a35fae4679b00dc8b19a95df54ddb 100644 --- a/platform/core/keychain.py +++ b/platform/core/keychain.py @@ -253,10 +253,13 @@ def next_slot(rt, etype): return top + 1 -def add_entry(rt, label, etype, fields, username): +def add_entry(rt, label, etype, fields, username, *, scope=None, write_scope=None): """Encrypt + store. Returns the metadata row, or raises ValueError on shape problems and KeychainLocked when there is no key (a secret must never be stored in the clear). + A scoped setup writer supplies `scope` and the API's `write_scope` callback. Personal + ownership commits before the credential; callers omitting these keep their existing path. + ⭐ W54-T44 / C4 — the row is stamped with its `connection_id` and its `slot`, and a caller that supplies NO label gets the default the slot names: the tenant's first Odoo connection is `Odoo 1`, the second `Odoo 2`. An explicitly supplied label always wins, and the label stays @@ -268,6 +271,13 @@ def add_entry(rt, label, etype, fields, username): etype = str(etype or '').strip().lower() if etype not in ENTRY_TYPES: raise ValueError(f"type must be one of {', '.join(sorted(ENTRY_TYPES))}") + if scope is not None and scope not in ('business', 'personal'): + raise ValueError('invalid credential scope') + if scope == 'personal': + if etype in TENANT_WIDE_TYPES: + raise ValueError('this connector requires business scope') + if not username or not callable(write_scope): + raise ValueError('personal scope requires an owner and a scoped writer') if etype == 'amazon_sp' and not isinstance(fields, dict): fields = {} if not isinstance(fields, dict) or (not fields and etype != 'amazon_sp'): @@ -305,14 +315,33 @@ def add_entry(rt, label, etype, fields, username): if etype == 'amazon_sp': row[AMAZON_ROTATION_DEADLINE_FIELD] = deadline + # Setup links publish a personal entry only AFTER its existing scope writer has + # committed the ownership row. If either write fails, keep that reservation: + # deleting it could turn an uncertain credential commit into a business entry. + if scope == 'personal': + if not write_scope(rt, eid, scope, username): + raise ValueError('credential scope could not be saved') + scopes = rt.get(SCOPE_KEY) + saved = scopes.get(eid) if isinstance(scopes, dict) else None + if saved != {'scope': 'personal', 'owner': str(username)}: + raise ValueError('credential scope could not be saved') + def _up(cur): entries = cur.setdefault('entries', {}) + if eid in entries: + if entries[eid] == row: + return cur + raise ValueError('credential identity already exists') if len(entries) >= MAX_ENTRIES: raise ValueError(f'this keychain is at its {MAX_ENTRIES}-entry cap') entries[eid] = row return cur - rt.update(KEY, _up, flush='sync') + stored = rt.update(KEY, _up, flush='sync') + if scope is not None: + saved_entries = stored.get('entries') if isinstance(stored, dict) else None + if not isinstance(saved_entries, dict) or saved_entries.get(eid) != row: + raise RuntimeError('credential write was not confirmed') return {'id': eid, 'connection_id': eid, 'slot': slot, 'label': label, 'type': etype, 'preview': preview, 'created': row['created'], 'createdBy': row['createdBy']} @@ -451,14 +480,14 @@ def odoo_creds(rt, connection_id=None): def meta_creds(rt, connection_id=None): - """W31 (R2): the FIRST `meta_ads` entry's fields, else None — the Meta connector's resolver. + """The named or designated default `meta_ads` connection's fields, else None. ⭐⭐ W49-T13 / D-202 — IT HAS A PRODUCTION CALLER NOW, AND THAT WAS THE WHOLE DEFECT. `harness/meta_store.py::token` read `META_ADS_ACCESS_TOKEN` unconditionally, so this resolver existed, was correct, and was reached by nothing: a tenant's stored Meta credential was accepted, previewed and testable while every sync ran on the deployment's own token. The - loader now asks here FIRST and falls back to the environment only. Deleting this function - because "nothing calls it" would have been the wrong repair to the same reading. + loader now asks here FIRST; its environment fallback is restricted to Royal Imports. + Deleting this function because "nothing calls it" would have been the wrong repair. ⛔ IT IS DELIBERATELY THE SAME SHAPE AS `odoo_creds` AND SHARES ITS IMPLEMENTATION, because "which stored credential serves this connector?" is ONE question and this module was one @@ -466,11 +495,10 @@ def meta_creds(rt, connection_id=None): difference between the two connectors belongs in the CONNECTOR, not in the lookup. ⚠ AND IT FAILS CLOSED WITH NO ENVIRONMENT FALLBACK, which is where it deliberately DIFFERS - from `odoo_creds`'s caller. `harness/runtime.py::odoo_source` falls back to the process - environment for tenant #0 — a documented exception for the tenant whose `.env` this is. There - is no equivalent for Meta: `META_ADS_ACCESS_TOKEN` is the OWNER's measurement token (R8), and - handing it to a tenant whose admin has not stored one is precisely the leak the R3 rule - exists to prevent. A tenant with no entry gets None, and the connector says so. + from its caller: environment fallback is a Royal-only connector decision, never a keychain + lookup decision. `META_ADS_ACCESS_TOKEN` is the OWNER's measurement token (R8), and handing + it to another tenant is precisely the leak the R3 rule exists to prevent. A tenant with no + entry gets None here. ⭐ W54-T45 / R4 — `connection_id` NAMES WHICH Meta connection, and no id means the tenant's DESIGNATED DEFAULT (the lowest-`slot` `meta_ads` connection), not "whichever row sorts first". diff --git a/platform/core/route_system.py b/platform/core/route_system.py index 2f7e9194b4808b577979b915473cdfa089d7ceb5..26e40bb0d602d96d132cac22b974cca82d75cd3e 100644 --- a/platform/core/route_system.py +++ b/platform/core/route_system.py @@ -591,7 +591,13 @@ def reconcile_inbox_tasks(st, *, today=None): def inbox_tasks(st, assignee, *, today=None): - return [task for task in reconcile_inbox_tasks(st, today=today) + from core import freeze_notice + if freeze_notice.enabled(): + # Reconcile only the detached read; durable receipts remain unchanged. + tasks = _reconcile_inbox_state(copy.deepcopy(read(st)), _inbox_day(today, st=st)) + else: + tasks = reconcile_inbox_tasks(st, today=today) + return [task for task in tasks if str(task.get("assignee") or "").lower() == str(assignee or "").lower()] diff --git a/platform/core/setup_links.py b/platform/core/setup_links.py new file mode 100644 index 0000000000000000000000000000000000000000..63a0f48b43e6fb68037b98461c424b545dc3aa93 --- /dev/null +++ b/platform/core/setup_links.py @@ -0,0 +1,162 @@ +"""Single-use connector setup capabilities. Credential writes are supplied by the API. + +The forms bucket holds only a token digest and server-owned metadata. Store callbacks +may be replayed after a conflict, so claiming is idempotent for its own operation and +refuses every competing operation before any credential writer can run. +""" +import hashlib +import math +import re +import secrets +import threading +import time +from urllib.parse import urlsplit + +from core import keychain + +KIND = "connector_setup" +TTL_SECONDS = 24 * 60 * 60 +_TOKEN = re.compile(r"^[A-Za-z0-9_-]{32}$") +# Serialize setup metadata mutations within a process too. Durable store.update and +# its conflict replay enforce the claim across processes; no secret write holds this lock. +_TOKEN_LOCK = threading.Lock() + + +class InvalidSetupLink(ValueError): + pass + + +class InvalidSetupValues(ValueError): + pass + + +class SetupWriteFailed(RuntimeError): + pass + + +def digest(token): + if not isinstance(token, str) or not _TOKEN.fullmatch(token): + return "" + return "setup:" + hashlib.sha256(token.encode("ascii")).hexdigest() + + +def public_origin(value): + """Accept a configured HTTPS origin, never a caller-supplied host or relative URL.""" + value = str(value or "").strip().rstrip("/") + try: + parts = urlsplit(value) + port = parts.port + except ValueError: + raise InvalidSetupLink() from None + if (parts.scheme != "https" or not parts.hostname or parts.username is not None + or parts.password is not None or parts.path or parts.query or parts.fragment + or any(ch.isspace() or ord(ch) < 32 for ch in value) or "\\" in value + or port == 0): + raise InvalidSetupLink() + return value + + +def _target_ok(connector, scope): + return (isinstance(connector, str) and connector in keychain.ENTRY_TYPES + and scope in ("personal", "business") + and not (scope == "personal" and connector in keychain.TENANT_WIDE_TYPES)) + + +def read_record(index, token, tenant, *, now=None): + row = index.get(digest(token)) if isinstance(index, dict) and digest(token) else None + if not isinstance(row, dict): + return None + expiry = row.get("expiresAt") + now = time.time() if now is None else now + if (row.get("kind") != KIND or row.get("status") != "ready" + or row.get("tenant") != tenant or not row.get("owner") + or not _target_ok(row.get("connector"), row.get("scope")) + or type(expiry) not in (int, float) or not math.isfinite(expiry) + or now >= expiry): + return None + return {key: row[key] for key in + ("kind", "tenant", "owner", "connector", "scope", "expiresAt", "status")} + + +def mint(rt, *, tokens_key, tenant, owner, connector, scope, origin, now=None): + origin = public_origin(origin) + if not tenant or not owner or not _target_ok(connector, scope): + raise InvalidSetupLink() + now = time.time() if now is None else now + token = secrets.token_urlsafe(24) + hashed = digest(token) + row = {"kind": KIND, "tenant": tenant, "owner": owner, "connector": connector, + "scope": scope, "expiresAt": now + TTL_SECONDS, "status": "ready"} + + def _mint(cur): + cur = dict(cur or {}) + if hashed in cur and cur[hashed] != row: + raise InvalidSetupLink() + cur[hashed] = dict(row) + return cur + + with _TOKEN_LOCK: + stored = rt.update(tokens_key, _mint, flush="sync") + if not isinstance(stored, dict) or stored.get(hashed) != row: + raise InvalidSetupLink() + return {"url": f"{origin}/#/form/{token}", "expiresAt": row["expiresAt"], + "connector": connector, "scope": scope} + + +def clean_values(field_specs, values): + """Use the supplied connector schema; never echo a submitted value in an error.""" + if not isinstance(values, dict) or not field_specs: + raise InvalidSetupValues() + keys = {field["key"] for field in field_specs} + if set(values) - keys: + raise InvalidSetupValues() + clean = {} + for field in field_specs: + key = field["key"] + value = values.get(key, "") + if (not isinstance(value, str) or len(value) > keychain.MAX_FIELD_LEN + or (field.get("required", True) and not value.strip())): + raise InvalidSetupValues() + if value: + clean[key] = value + return clean + + +def submit(rt, token, record, values, *, tokens_key, field_specs, write_credentials): + clean = clean_values(field_specs, values) + hashed, claim_id = digest(token), secrets.token_hex(16) + + def _claim(cur): + cur = dict(cur or {}) + current = cur.get(hashed) + # HF conflict replay of THIS successful claim is harmless; another claimant refuses. + if (isinstance(current, dict) and current.get("status") == "used" + and current.get("claimId") == claim_id): + return cur + fresh = read_record(cur, token, record["tenant"]) + if fresh is None or fresh != record: + raise InvalidSetupLink() + cur[hashed] = {**fresh, "status": "used", "claimId": claim_id} + return cur + + try: + with _TOKEN_LOCK: + stored = rt.update(tokens_key, _claim, flush="sync") + claimed = stored.get(hashed) if isinstance(stored, dict) else None + if (not isinstance(claimed, dict) or claimed.get("claimId") != claim_id + or claimed.get("status") != "used"): + raise SetupWriteFailed() + except InvalidSetupLink: + clean.clear() + raise + except Exception: + clean.clear() + raise SetupWriteFailed() from None + # A failed or uncertain write must never revive this capability or claim success. + try: + write_credentials(record, clean) + except Exception: + raise SetupWriteFailed() from None + finally: + clean.clear() + return {"ok": True, "kind": KIND} diff --git a/platform/core/shared_overlay.py b/platform/core/shared_overlay.py index 521d050f7d1d6d1a0244aaba7b3bc50d81877253..8186bdfb11015e402d82c609d814d3a83d98113e 100644 --- a/platform/core/shared_overlay.py +++ b/platform/core/shared_overlay.py @@ -64,6 +64,10 @@ BUCKET_SUFFIX = '__shared' #: in the tenant, so an unbounded one is an unbounded cost for all of them. MAX_VALUE_CHARS = 32_768 + +class SchemaUnavailable(RuntimeError): + """Definitions cannot be answered without an unfinished full-workspace migration.""" + # ══════════════════════════ W41-T04 / CONTRACT C3 — ONE COLUMN-CREATION DOOR ══════════════════ # # ⭐⭐ WHAT THIS ALLOWLIST IS FOR, AND WHY IT IS *HERE* RATHER THAN AT EACH CALLER. Before it, @@ -141,6 +145,15 @@ def _read(table_key, st=None): """The whole stratum, always in its full two-key shape. Lenient like every other display read: an unreachable store degrades to "nothing shared yet", never to an exception on a page that would otherwise render.""" + if getattr(_st(st), 'schema_only', False): + reader = getattr(_st(st), 'get_entry', None) + if not callable(reader): + raise SchemaUnavailable('field definitions cannot be read without loading row cells') + fields = reader(bucket(table_key), 'fields') + if fields is not None and not isinstance(fields, dict): + raise SchemaUnavailable('the stored field definitions are unavailable') + # Asking this metadata document for cells must remain a loud programming error. + return store._Projected({'fields': dict(fields or {})}, ('cells',)) try: data = _st(st).get(bucket(table_key)) or {} except Exception: @@ -251,11 +264,15 @@ def fold_legacy_bucket(legacy_key, table_key, st=None): if not _may_hold_anything(legacy, st=st): return {'fields': 0, 'cells': 0} src = _read(legacy, st=st) - if not src['fields'] and not src['cells']: + if not src['fields'] and (getattr(_st(st), 'schema_only', False) or not src['cells']): return {'fields': 0, 'cells': 0} dst = _read(canon, st=st) add_fields = {key: defn for key, defn in src['fields'].items() if isinstance(defn, dict) and key not in dst['fields']} + if getattr(_st(st), 'schema_only', False): + if add_fields: + raise SchemaUnavailable('field definitions need a full workspace refresh to finish migration') + return {'fields': 0, 'cells': 0} add_cells = {} for pid, values in src['cells'].items(): if not isinstance(values, dict): @@ -299,13 +316,21 @@ def _value(value): ⛔ A dict or a list RAISES rather than being dropped. This is called by our own code, so a non-scalar is a programming error, and silently storing nothing would surface later as "the shared column is blank for everyone" with no failure anywhere near the cause. + + An oversized string refuses before any mutation. The shared-cell HTTP door already relays + ValueError as a sentence; cutting here would silently change text and could corrupt JSON. """ if isinstance(value, (dict, list, tuple, set)): raise ValueError(f'shared_overlay: a cell holds a scalar, not {type(value).__name__}') if value is None or isinstance(value, bool) or isinstance(value, (int, float)): return value text = str(value) - return text[:MAX_VALUE_CHARS] + if len(text) > MAX_VALUE_CHARS: + raise ValueError( + f"This cell value has {len(text):,} characters and would be truncated at the " + f"{MAX_VALUE_CHARS:,}-character cell limit. It was not saved. " + "Shorten the value or split it across cells and try again.") + return text # ----------------------------------------------------------------- the column DEFINITIONS @@ -553,7 +578,7 @@ def snapshot(table_key, pids, st=None): def put_cell(table_key, pid, field_key, value, st=None): - """Write ONE shared cell. Returns the value as stored (which may be truncated).""" + """Write ONE shared cell. Oversized values refuse before writing; accepted values are exact.""" return put_cells(table_key, pid, {field_key: value}, st=st).get(str(field_key or '')) diff --git a/platform/core/sheet_import.py b/platform/core/sheet_import.py index ec057f106cf47a08b0f589b77aee9b29ce004ce6..1eb55818e6bf0282994b4eec52c54f8551a4d7e4 100644 --- a/platform/core/sheet_import.py +++ b/platform/core/sheet_import.py @@ -987,15 +987,20 @@ def _address_in_document(ref): def _guard_worksheet_xml(stream): - """Stream one worksheet and reject impossible dimensions/cell counts before openpyxl. + """Stream one worksheet and bound authored cells plus merge allocation before openpyxl. ElementTree sees only bounded, central-directory-admitted bytes. It is used here as an admission parser, not as the workbook parser: no cell value, formula or style is interpreted. """ dimension_seen = False cells = 0 + merged_cells = 0 + ancestors = [] try: - for _event, element in ET.iterparse(stream, events=("end",)): + for event, element in ET.iterparse(stream, events=("start", "end")): + if event == "start": + ancestors.append(element) + continue tag = str(element.tag).rsplit("}", 1)[-1] if tag == "dimension": ref = str(element.get("ref") or "").replace("$", "").upper() @@ -1004,7 +1009,9 @@ def _guard_worksheet_xml(stream): or (len(parts) == 2 and rect_of(ref) is None)): _archive_refusal( "import_declared_dimension_invalid", - "A worksheet declares cells outside Excel's supported dimensions, so it cannot be imported.") + "A worksheet declares cells outside Excel's supported dimensions of " + "1,048,576 rows and 16,384 columns. Remove the out-of-range rows or columns " + "in Excel and save the workbook again.") dimension_seen = True elif tag == "c": cells += 1 @@ -1012,13 +1019,39 @@ def _guard_worksheet_xml(stream): if not _address_in_document(ref): _archive_refusal( "import_declared_dimension_invalid", - "A worksheet contains a cell outside Excel's supported dimensions, so it cannot be imported.") + "A worksheet contains a cell outside Excel's supported dimensions of " + "1,048,576 rows and 16,384 columns. Remove the out-of-range rows or columns " + "in Excel and save the workbook again.") if cells > sheets._MAX_CELLS_PER_SHEET: _archive_refusal( "sheet_too_many_cells", "A worksheet declares more than " + str(sheets._MAX_CELLS_PER_SHEET) - + " populated cells, so it cannot be imported.") + + " populated cells, so it cannot be imported. Delete unused cells or " + "split the worksheet across tabs and try again.") + elif tag == "mergeCell": + ref = str(element.get("ref") or "").replace("$", "").upper() + parts = ref.split(":") + if len(parts) not in (1, 2) or not all(_address_in_document(part) for part in parts): + _archive_refusal( + "import_declared_dimension_invalid", + "A merged range is outside Excel's supported dimensions of " + "1,048,576 rows and 16,384 columns. Unmerge that range in Excel and try again.") + rect = rect_of(ref) + # Normal-mode openpyxl allocates every merged placeholder. A sparse dimension + # alone allocates nothing and must remain legal, including Excel's last cell. + merged_cells += ((rect["col1"] - rect["col0"] + 1) + * (rect["row1"] - rect["row0"] + 1) - 1) + if cells + merged_cells > sheets._MAX_CELLS_PER_SHEET: + _archive_refusal( + "import_merged_cells_too_large", + "Opening this worksheet's merged ranges would exceed the " + + str(sheets._MAX_CELLS_PER_SHEET) + + "-cell import limit. Unmerge large ranges in Excel or split the worksheet " + "across tabs and try again.") + ancestors.pop() element.clear() + if ancestors: + ancestors[-1].remove(element) except ImportRefusal: raise except ET.ParseError as exc: @@ -1040,32 +1073,42 @@ def guard_import_archive(path): raise ImportRefusal( "import_too_large", "That file is larger than the " + str(MAX_IMPORT_BYTES // (1024 * 1024)) - + " MB import limit, so it cannot be read.") + + " MB import limit, so it cannot be read. Split it into smaller workbooks and try again.") try: with zipfile.ZipFile(path) as archive: members = archive.infolist() if len(members) > MAX_ARCHIVE_MEMBERS: _archive_refusal("import_zip_bloat", - "That workbook archive holds too many parts to import safely.") + f"That workbook archive exceeds the {MAX_ARCHIVE_MEMBERS:,}-part " + "import limit. Split it into smaller workbooks and try again.") names = [entry.filename for entry in members] if len(set(names)) != len(names): _archive_refusal("import_zip_bloat", - "That workbook archive repeats a part name, so it cannot be imported safely.") + "That workbook archive repeats a part name, so it cannot be imported " + "safely. Save it again as .xlsx and try once more.") total = sum(max(0, int(entry.file_size)) for entry in members) if total > MAX_ARCHIVE_UNCOMPRESSED_BYTES: _archive_refusal("import_zip_bloat", - "That workbook expands beyond the safe import limit.") + "That workbook expands beyond the " + + str(MAX_ARCHIVE_UNCOMPRESSED_BYTES // (1024 * 1024)) + + " MB uncompressed import limit. Split it into smaller workbooks and try again.") for entry in members: if entry.flag_bits & 0x1: _archive_refusal("import_encrypted", - "Password protected workbooks cannot be imported.") + "Password protected workbooks cannot be imported. Remove the " + "password in Excel and save the workbook again.") if entry.file_size > MAX_ARCHIVE_MEMBER_BYTES: _archive_refusal("import_zip_bloat", - "One workbook part expands beyond the safe import limit.") + "One workbook part expands beyond the " + + str(MAX_ARCHIVE_MEMBER_BYTES // (1024 * 1024)) + + " MB uncompressed part limit. Split it into smaller workbooks and try again.") ratio = entry.file_size / max(1, entry.compress_size) if entry.file_size and ratio > MAX_ARCHIVE_COMPRESSION_RATIO: _archive_refusal("import_zip_bloat", - "That workbook archive is compressed too densely to import safely.") + "That workbook archive exceeds the " + + str(MAX_ARCHIVE_COMPRESSION_RATIO) + + " to 1 compression limit. Remove unused repeated content " + "or split it into smaller workbooks and try again.") worksheets = [entry for entry in members if re.fullmatch(r"xl/worksheets/sheet[^/]*\.xml", entry.filename)] for entry in worksheets: diff --git a/platform/core/sheets.py b/platform/core/sheets.py index e91f611590db88d94b895ba7dd713bdaf1a9b2c6..d5d8e00cbbcfb848b4ff3d2997c16c87545c650c 100644 --- a/platform/core/sheets.py +++ b/platform/core/sheets.py @@ -261,7 +261,10 @@ SHEET_CHART_STACKING = ("clustered", "stacked", "percent") #: The order is the order a cleaned tab reads in: identity, then A2's geometry, then C2's strata. SHEET_TAB_KEYS = ("id", "name", "cols", "rows", "cells", "usedExtent", "colWidths", "freeze", "rules", "rowGroups", "hiddenRows", "hiddenCols", "charts", "imported", "rowHeight", "rowHeights", - "visibility", "view", "print", "excel") + "visibility", "view", "print", "excel", "kind", "pivot") + +SHEET_TAB_KINDS = ("grid", "pivot") +PIVOT_AGGREGATES = ("sum", "count", "avg", "min", "max") #: The keys above that `_clean_sheet` writes UNCONDITIONALLY. Every other member of #: `SHEET_TAB_KEYS` is written only when it holds something, for `_clean_style`'s reason: a tab @@ -1502,6 +1505,53 @@ def _clean_row_groups(raw, rows): return out or None +def clean_pivot(raw, *, draft=False): + """C6's authored pivot definition, validated without changing its JSON representation. + + Field and database access are checked by the read door, never by workbook storage. A saved + pivot is a definition, not a cached aggregate, and additive metadata remains intact. + `draft=True` (workbook storage) admits a tab with no database or value yet, which is what + Insert > PivotTable creates; the read door keeps refusing it. + """ + if not isinstance(raw, dict): + raise SheetRefusal("sheet_pivot_invalid", "A pivot must be an object.") + database = raw.get("database") + if not isinstance(database, str) or (not database.strip() and not draft): + raise SheetRefusal("sheet_pivot_invalid", "A pivot must name a database.") + for axis in ("rows", "columns"): + fields = raw.get(axis, []) + if not isinstance(fields, list) or any(not isinstance(field, str) or not field.strip() + for field in fields): + raise SheetRefusal("sheet_pivot_invalid", "Pivot rows and columns must list field names.") + values = raw.get("values") + if not isinstance(values, list) or (not values and not draft): + raise SheetRefusal("sheet_pivot_invalid", "A pivot must have at least one value.") + for value in values: + if not isinstance(value, dict) or not isinstance(value.get("field"), str) \ + or not value["field"].strip(): + raise SheetRefusal("sheet_pivot_invalid", "Each pivot value must name a field.") + if value.get("agg") not in PIVOT_AGGREGATES: + raise SheetRefusal("sheet_pivot_aggregate_unknown", + "Choose sum, count, avg, min or max for the pivot value.") + + def validate_filters(nodes): + if not isinstance(nodes, list): + raise SheetRefusal("sheet_pivot_invalid", "Pivot filters must be a list.") + for node in nodes: + if not isinstance(node, dict): + raise SheetRefusal("sheet_pivot_invalid", "Each pivot filter must be an object.") + if "children" in node: + if node.get("conj", "and") not in ("and", "or"): + raise SheetRefusal("sheet_pivot_invalid", "A pivot filter group must use and or or.") + validate_filters(node["children"]) + elif not isinstance(node.get("colId"), str) or not node["colId"].strip() \ + or not isinstance(node.get("op"), str) or not node["op"].strip(): + raise SheetRefusal("sheet_pivot_invalid", "Each pivot filter must name a field and condition.") + + validate_filters(raw.get("filters", [])) + return copy.deepcopy(_json_safe(raw, "pivot")) + + def _clean_sheet(raw, index=0, *, lossless=False): """Normalize a tab's known strata and retain additive JSON metadata without interpretation. @@ -1532,6 +1582,10 @@ def _clean_sheet(raw, index=0, *, lossless=False): visibility = raw.get("visibility") if visibility is not None and visibility not in ("visible", "hidden", "veryHidden"): raise SheetRefusal("sheet_visibility_unknown", "A sheet must be visible, hidden or very hidden.") + kind = raw.get("kind", "grid") + if kind not in SHEET_TAB_KINDS: + raise SheetRefusal("sheet_tab_kind_unknown", "Choose grid or pivot for the sheet tab.") + pivot = clean_pivot(raw.get("pivot"), draft=True) if kind == "pivot" or "pivot" in raw else None #: ⚠ THE ORDER OF THIS DICT IS THE ORDER THE CLEANERS RUN IN, and it is load bearing: two of #: them REFUSE by name (`_clean_rules` on an unknown op, `_clean_imported` on a record that is @@ -1552,6 +1606,9 @@ def _clean_sheet(raw, index=0, *, lossless=False): "view": _clean_view(raw.get("view")), "print": _clean_print(raw.get("print")), "excel": excel, + # Absence remains the legacy grid default; an explicitly authored kind round-trips. + "kind": kind if "kind" in raw else None, + "pivot": pivot, } #: ⛔ THE WALK IS THROUGH `SHEET_TAB_KEYS`, NOT THROUGH `optional`, AND THAT IS THE WHOLE @@ -1839,7 +1896,7 @@ def put_workbook(st, workbook_id, raw, expect_rev=None, *, actor="", actor_name= if expect_rev is not None and int(expect_rev) < int(prior.get("structureRev") or 0): raise SheetStaleRefusal( "sheet_structure_stale", - "Rows or columns changed since you opened this workbook. This save was not applied. Reload the workbook and try again.", + "Rows or columns changed since you opened this workbook. This save was not applied. Try again using the refreshed workbook.", rev_current=int(prior.get("rev") or 0)) if expect_rev is not None and int(prior.get("rev") or 0) != int(expect_rev): raise SheetStaleRefusal( @@ -2167,12 +2224,12 @@ def patch_cells(st, workbook_id, tab_ref, raw_cells, expect_rev=None, *, actor=" if revision < int(prior.get("structureRev") or 0): raise SheetStaleRefusal( "sheet_structure_stale", - "Rows or columns changed since you opened this workbook. This save was not applied. Reload the workbook and try again.", + "Rows or columns changed since you opened this workbook. This save was not applied. Try again using the refreshed workbook.", rev_current=current) if revision < int(tab.get("writesFrom") or 0): raise SheetStaleRefusal( "sheet_revision_stale", - "This tab's retained change history was truncated after your revision. Reload the tab and try again.", + "This tab's retained change history was truncated after your revision. This save was not applied. Try again using the refreshed tab.", rev_current=current) for write in writes: if int(write["rev"]) > revision and write["by"] != actor: @@ -2421,7 +2478,7 @@ def narrow_workbook(st, workbook_id): state.setdefault("access", "private") return state, state - # R28 / W62-T25: explicit sharing changes the fact used by the index's visibility filter. + # Preserve the existing explicit-share marker and index facts; R31 admission uses roles. book = _persist(st, workbook_key(wid), _apply) _index_upsert(st, book) @@ -2477,21 +2534,20 @@ def workbook_role(st, book, viewer, *, is_admin=False, rec=None): def may_read_workbook(book, role): - """R28: a role admits a reader; unmarked or creatorless documents remain legacy-visible. + """R31 / C7: only an owner, grantee or administrator may read a workbook. - R28 / W62-T25: creation and explicit sharing stamp access. The index records its presence; - workbook reads still use the document, and PUT must preserve marker absence too. - Legacy visibility grants no ownership or editing role. + The existing role resolver includes administrators as owners. Legacy access markers and + missing creators confer no read access; document reads and index projections use this rule. """ - return bool(role) or "access" not in book or not book.get("created_by") + return role in ("owner", "view", "edit") def project_shares(st, rows, viewer=None, *, is_admin=False, role_on_workbook=None, grant_records=None): """Join `shares` onto a list of index rows. Returns NEW rows; the stored ones are untouched. - W62-T25: a named viewer filters rows using document facts and receives their role. - The route supplies its existing role helper; core callers use workbook_role directly. + R31 / C7: a named viewer filters rows with the workbook read predicate and receives their role. + The route uses the bulk role join; callers may also supply a workbook role helper. `list_workbooks` itself stays a bare pointer list on purpose: `iter_workbooks` walks it to answer W49-T10's dependents question, and paying a grant-registry read per workbook there would put a permission lookup inside a scan that asks no permission @@ -2503,16 +2559,13 @@ def project_shares(st, rows, viewer=None, *, is_admin=False, role_on_workbook=No documents was pulled in full — all its tabs, all its cells — so that three fields could be consulted, and the list got slower with every cell anybody typed. - ⭐ THE ROW ALREADY ANSWERS TWO OF THE THREE. `_index_row` rebuilds `id` and `created_by` off + ⭐ THE ROW ALREADY ANSWERS THE ROLE QUESTION. `_index_row` rebuilds `id` and `created_by` off the document on every save, and `workbook_role` reads nothing else; so does the route's - injected helper. The third is `may_read_workbook`'s legacy-visibility clause, `"access" not in - book`, and it is consulted ONLY when the viewer has no role at all AND the row names a - creator - the rows that are otherwise about to be REFUSED. W62-T25 records that third fact - as `has_access`; only rows written before that fact existed still read their document. - - R28 / W62-T25: admission uses the document facts rebuilt on save and explicit sharing. - `_index_drop` removes a deleted document's row. Older rows still check document existence - when taking the fallback; opening any workbook always reads its document for real. + injected helper. R31 removes the legacy-visibility exception, so older rows lacking + `has_access` need no workbook-document fallback either. + + `_index_drop` removes a deleted document's row. Opening any workbook always reads its + document for real; the index remains a pointer list. """ out = [] bulk = grant_records @@ -2537,18 +2590,9 @@ def project_shares(st, rows, viewer=None, *, is_admin=False, role_on_workbook=No light = {"id": row["id"], "created_by": row.get("created_by") or ""} role = (role_on_workbook(light) if role_on_workbook is not None else workbook_role(st, light, viewer, is_admin=is_admin, rec=rec)) - if not role and light["created_by"]: - if "has_access" in row: - if row["has_access"]: - continue - else: - # R28 / W62-T25: old rows lack this fact, so retain their document check. - # R28: this fallback set is expected to be empty after every workbook - # has been saved once; guessing would change R28's legacy admission. - book = _read(st, workbook_key(row["id"])) - if not book or not book.get("id") or not may_read_workbook(book, role): - continue - joined["role"] = role or "view" + if not may_read_workbook(light, role): + continue + joined["role"] = role joined["shares"] = shares_from_record(rec) out.append(joined) return out @@ -2843,7 +2887,8 @@ def validate_folder_tree(folders, item_folders): if len(rows_in) > _MAX_FOLDERS: raise SheetFolderRefusal( "too_many_folders", - "That rail holds more folders than one workspace accepts.", + f"That rail holds {len(rows_in)} folders and a workspace accepts at most {_MAX_FOLDERS}. " + "Delete unused folders or combine their contents into fewer folders and try again.", reason="too_many") # ---- pass 1: shape. Ids, duplicates, and a `parent` resolved against the FINISHED set. @@ -3127,9 +3172,9 @@ def save_folders(st, folders, item_folders, starred=None, expect_rev=None, *, inside `_apply` for the same reason. `store.update` discards the callback's work when it raises, so a refused save still writes nothing. - ⚠ `expect_rev=None` OPTS OUT, exactly as `put_workbook`'s does. A client that has never read - the rail cannot send a revision, and refusing would make the first folder ever created - impossible. + ⚠ Trusted core callers may omit `expect_rev`. The HTTP wholesale door requires the integer + revision from `read_index`, including revision zero for the first folder. Placements-only + deltas use `merge_placements` instead of replacing the rail. """ tree, placed, repairs = validate_folder_tree(folders, item_folders) writable = None if writable_ids is None else {str(wid) for wid in writable_ids} diff --git a/platform/core/store.py b/platform/core/store.py index 7a3f0532404c04a2b0ae1028b50237ddc792ff83..c7382bf801c17f360912564bcc96d339e89e1fd1 100644 --- a/platform/core/store.py +++ b/platform/core/store.py @@ -380,6 +380,34 @@ class Store: self._base[name] = sha # D-305 — set with the cache, never apart from it return _project(data, drop) + def get_entry(self, name, entry): + """One detached dictionary entry, or None, with the existing document cache/lock. + + Cold HF reads retain the whole downloaded document in the existing cache. Only the + selected entry is serialized; sibling rows/cells never enter the returned copy. + """ + def selected(data): + value = data.get(str(entry)) if isinstance(data, dict) else None + return json.loads(json.dumps(value)) if isinstance(value, dict) else None + + with self._lock: + if name in self._cache: + return selected(self._cache[name]) + try: + data, sha = self._download(name) + except Exception as exc: + try: + import harness.telemetry as telemetry + telemetry.error(f'store:get_entry:{name}', exc, + fallback='cached' if name in self._cache else 'empty') + except Exception: + pass + with self._lock: + return selected(self._cache.get(name)) + with self._lock: + self._cache[name], self._base[name] = data, sha + return selected(data) + def _key_lock(self, name): """The write lock for ONE key (W31-T13). Created on demand; never removed.""" n = str(name) @@ -1183,6 +1211,11 @@ def get_projection(name, drop=()): return _d().get_projection(name, drop=drop) +def get_entry(name, entry): + """One dictionary entry through the default tenant's existing store handle.""" + return _d().get_entry(name, entry) + + def revision(name): """The bucket's change token — wave 29, C6. See `Store.revision` for why it copies nothing. diff --git a/platform/core/store_backend.py b/platform/core/store_backend.py index fc7d10cc634c1499aaeb57c60b5b8089c3def7f9..13e8624faf00a308236ecc7584ffed58a56cd1f7 100644 --- a/platform/core/store_backend.py +++ b/platform/core/store_backend.py @@ -113,6 +113,11 @@ def get_projection(name_, drop=()): return active().get_projection(name_, drop=drop) +def get_entry(name_, entry): + """One dictionary entry through the existing active-backend selector.""" + return active().get_entry(name_, entry) + + def revision(name_): """The bucket's change token (wave 29, C6) — `{'rev', 'updated_at', 'token'}`. diff --git a/platform/core/store_pg.py b/platform/core/store_pg.py index bef62ce965d2c3d202ce7e863f8e17531f58d3d3..589fc828a1929f9b3a48e171be51c6119c772b89 100644 --- a/platform/core/store_pg.py +++ b/platform/core/store_pg.py @@ -49,6 +49,7 @@ from pathlib import Path from urllib.parse import urlparse import core.data_binding as data_binding +from core import freeze_notice from core.user_tables import enforce_byte_budget as _BYTE_BUDGET_ENFORCER _URL_ENV = 'DATABASE_URL' @@ -254,6 +255,7 @@ def check_write(tenant_slug=None, operation='write'): a session-scoped fallback and tell the user their change was saved. A new type would have had to re-earn both properties, and would have failed the second one silently. """ + freeze_notice.check_write() reason = gate_write_refusal(tenant_slug) or write_refusal(tenant_slug) if reason: raise data_binding.StoreWriteRefused( @@ -550,6 +552,11 @@ def _get_projections(names, drop=(), tenant_slug=None, timeout_s=None): } +def get_entry(name, entry, tenant_slug=None): + """Dictionary entry parity with the HF store through the existing PG primitive.""" + return _get_entry(name, entry, tenant_slug=tenant_slug) + + def _get_entry(name, entry, tenant_slug=None): """ONE top-level entry of document `name` (`value -> entry`), or None when either is absent. @@ -650,21 +657,15 @@ class StoreDocumentTooLarge(RuntimeError): def _payload(name, data, con=None, tenant_slug=None): """Serialize `data` for `name` through the byte budget and the ceiling. Raises when refused.""" - payload = json.dumps(data) if str(name) == 'user_tables' and isinstance(data, dict): from core import user_tables as _ut - # Under both budgets in TOTAL, no table can be over either, so the measurement runs only - # when it can find something (or a stamp may need clearing). - if len(payload) > min(_ut.MAX_TABLE_BYTES, _ut.MAX_DOCUMENT_BYTES) or any( - isinstance(t, dict) and t.get(_ut.OVERSIZE_FIELD) and t.get('rows') - for t in data.values()): - report = _ut.enforce_byte_budget(data) - if report['evicted'] or report['cleared']: - payload = json.dumps(data) - if report['evicted']: - print('[store_pg] user_tables byte budget: stored no rows for ' - + ', '.join(f'{k} ({v / 1048576:.1f} MB)' - for k, v in sorted(report['evicted'].items()))) + # Read-through copies must never persist, even below the size budget. + report = _ut.enforce_byte_budget(data) + if report['evicted']: + print('[store_pg] user_tables byte budget: stored no rows for ' + + ', '.join(f'{k} ({v / 1048576:.1f} MB)' + for k, v in sorted(report['evicted'].items()))) + payload = json.dumps(data) if len(payload) > DOCUMENT_CEILING_BYTES: before = None if con is not None: diff --git a/platform/core/table_store.py b/platform/core/table_store.py index 07341d26bb517f6e1fb8af91aad9613b4a5e5d8f..9847c4c0a562d8123058a280a49cf5d88bffb32c 100644 --- a/platform/core/table_store.py +++ b/platform/core/table_store.py @@ -774,7 +774,7 @@ class TableStore: # then splice a bounded copy into that payload only. Keeping it out of `fields` # prevents an old request id surviving forever and overriding a later rename. corrections = {} - if consume_corrections and ws.get('fieldCorrections'): + if consume_corrections and not getattr(self._st, 'schema_only', False) and ws.get('fieldCorrections'): def _take(current): current_ws = current.get(username) or {} pending = current_ws.get('fieldCorrections') or {} @@ -824,7 +824,8 @@ class TableStore: out = { 'views': dict(ws.get('views') or {}), 'fields': fields, - 'overlays': dict(ws.get('overlays') or {}), + 'overlays': ({} if getattr(self._st, 'schema_only', False) + else dict(ws.get('overlays') or {})), # wave-8 I11 (C4): folders over the saved views / cohorts sidebars. A FOURTH # stratum rather than a key on each item — see aios_grid.clean_folders for why # (a cohort lives in another store, and filing is an organising act, not part of @@ -1402,6 +1403,9 @@ class TableStore: permission_plans = self._personal_permission_plans(source) if not plans and not permission_plans and not has_blank_legacy: return {'migrated': 0, 'status': 'clean'} + if getattr(self._st, 'schema_only', False): + from core.shared_overlay import SchemaUnavailable + raise SchemaUnavailable('saved fields and views need a full workspace refresh to finish migration') import core.shares as shares diff --git a/platform/core/user_tables.py b/platform/core/user_tables.py index 54e2dbb4284ee8728997678b1a87e47b28b3678b..5ccb6437e11ebec6f4904e5a41803894f0a010d1 100644 --- a/platform/core/user_tables.py +++ b/platform/core/user_tables.py @@ -327,6 +327,20 @@ def capped_table_count(tables): #: for whole tables. Every table that fails one is REPORTED, per table, on the status door. _READ_THROUGH = set() +# C8 / R33: these reserved connector databases always serve the mirror. A cold +# definition write must retire their legacy rows even before the API registry loads. +_MIRROR_ONLY_KEYS = frozenset(('ut_amzn_orders', 'ut_amzn_order_items')) + + +def _mirror_only_definition(key, table): + return (key in _READ_THROUGH or table.get('readThrough') is True + or (key in _MIRROR_ONLY_KEYS and ( + _connector_fed(key, table) + or (table.get('source') == AUTOMATION_SOURCE + and table.get('recordMode') == AUTOMATION_RECORD_MODE + and any((field.get('automation') or {}).get('flowId') == 'amazon_relational' + for field in table.get('fields') or [] if isinstance(field, dict)))))) + def register_read_through(*table_keys): """Declare that these databases are SERVED from the mirror and store no rows here.""" @@ -378,14 +392,13 @@ def strip_materialised(st=None): the permission wall and the nav entry with it. """ out = {'dropped': {}, 'before': 0, 'after': 0} - if not _READ_THROUGH: - return out s = _st(st) try: cur = s.get(STORE_KEY) or {} except Exception: # noqa: BLE001 return out - fat = {k: len((cur.get(k) or {}).get('rows') or {}) for k in _READ_THROUGH} + fat = {k: len(t.get('rows') or {}) for k, t in cur.items() + if isinstance(t, dict) and _mirror_only_definition(k, t)} fat = {k: n for k, n in fat.items() if n} if not fat: return out # the common case: nothing to do, and nothing measured @@ -394,14 +407,14 @@ def strip_materialised(st=None): out['before'] = len(_json.dumps(cur, default=str)) def _strip(doc): - for key in list(_READ_THROUGH): - t = (doc or {}).get(key) - if isinstance(t, dict) and t.get('rows'): + for key, t in (doc or {}).items(): + if isinstance(t, dict) and _mirror_only_definition(key, t) and t.get('rows'): out['dropped'][key] = len(t['rows']) t['rows'] = {} # ⛔ THE STAMP IS THE POINT — see `materialises`. Without it the next process to # boot reads an empty `rows` and calls that the answer. t['readThrough'] = True + t.pop(OVERSIZE_FIELD, None) return doc s.update(STORE_KEY, _strip, flush='sync') @@ -431,9 +444,9 @@ def _connector_fed(key, table): def enforce_byte_budget(doc, now=None): - """Evict connector-fed rows over the byte budget from a `user_tables` document, in place. + """Remove read-through copies, then evict connector-fed rows over the byte budget in place. - Idempotent, and called by `core.store_pg` on every write of this document. Returns + Idempotent; `core.store_pg` calls this from its payload-budget check. Returns `{'evicted': {key: bytes}, 'cleared': [key, ...]}`. """ out = {'evicted': {}, 'cleared': []} @@ -444,6 +457,20 @@ def enforce_byte_budget(doc, now=None): for key, table in doc.items(): if not isinstance(table, dict): continue + # A read-through definition cannot retain old rows below the byte budget. + # The flag and removal share the caller's atomic definition write; the + # source population remains in the mirror and has no row ceiling. + if _mirror_only_definition(key, table): + rows = table.get('rows') or {} + if rows: + out['evicted'][key] = rows_bytes(rows) + table['rows'] = {} + if table.get('readThrough') is not True: + out['cleared'].append(key) + table['readThrough'] = True + if table.pop(OVERSIZE_FIELD, None) is not None and key not in out['cleared']: + out['cleared'].append(key) + continue rows = table.get('rows') if not rows: continue @@ -1585,9 +1612,17 @@ def all_defs(st=None): this function, so an empty answer here stops being a blank nav and becomes a 404 on every database in the tenant. """ + # Schema-only handles cannot use the historical whole-reader fallback described above: + # their caller must distinguish an unavailable definition snapshot from an empty one. + reader = _st(st) try: - return dict(_st(st).get_projection(STORE_KEY, drop=DEF_ONLY_DROPS) or {}) + return dict(reader.get_projection(STORE_KEY, drop=DEF_ONLY_DROPS) or {}) except Exception: + # A schema-only request promises not to materialize rows, including on an outage. + # Its existing route maps this failure to a named unavailable response. Ordinary + # full readers retain the correct-but-slow fallback below. + if getattr(reader, 'schema_only', False): + raise # ⚠ `AttributeError` (a store too old to project) is not special-cased any more — it was # only ever ONE of the ways this can fail, and singling it out is what made the other ways # silent. `all_tables` is correct for all of them. @@ -4758,8 +4793,9 @@ def may_edit_database(table_key, viewer, is_admin=False, st=None): #: LAZILY (an admin, or a table's creator, is admitted before that leg runs and never pays for #: it) and memoised for the pass. #: -#: ⚠ THE ALLOW-LIST IS THE SAFETY ARGUMENT, not the read-only-ness. Any other key passes through -#: to the real runtime uncached, `fresh=True` always bypasses, and every WRITE verb reaches the +#: ⚠ THE ALLOW-LIST CONTROLS AUTOMATIC REUSE. Any other UNSUPPLIED key passes through +#: to the real runtime uncached. Explicit snapshots are caller-captured read-only documents for +#: this pass only; `fresh=True` always bypasses, and every WRITE verb reaches the #: runtime through `__getattr__` — so this can serve a stale value for the two named keys and #: dynamically named `__shared` overlay buckets, for #: exactly one pass, to code proven not to mutate them (`all_tables` shallow-copies the top level @@ -4769,6 +4805,9 @@ def may_edit_database(table_key, viewer, is_admin=False, st=None): #: `fresh=True` and write verbs still reach the runtime, never the snapshot. _LENDABLE = (STORE_KEY, 'object_shares') +# A caller may also supply a captured read-only document explicitly. That grants no automatic +# reuse to its bucket: unsupplied keys outside the allow-list still read the real runtime. + class _Lent: """A read-only `st` that serves already-read documents and passes everything else through.""" @@ -4777,7 +4816,8 @@ class _Lent: self._runtime, self._docs = runtime, dict(docs or {}) def get(self, name, fresh=False): - if fresh or (name not in _LENDABLE and not str(name).endswith('__shared')): + if fresh or (name not in self._docs and name not in _LENDABLE + and not str(name).endswith('__shared')): return self._runtime.get(name, fresh) if fresh else self._runtime.get(name) if name not in self._docs: self._docs[name] = self._runtime.get(name) # lazily, once, for this pass @@ -4797,7 +4837,7 @@ class _Lent: A re-projection would cost a second copy to convert the first case and change nothing about the second. """ - if name not in _LENDABLE and not str(name).endswith('__shared'): + if name not in self._docs and name not in _LENDABLE and not str(name).endswith('__shared'): return self._runtime.get_projection(name, drop=drop) if name not in self._docs: self._docs[name] = self._runtime.get_projection(name, drop=drop) @@ -4808,7 +4848,7 @@ class _Lent: def lend(st=None, **docs): - """Lend `_LENDABLE` keys and `__shared` suffix buckets; defer other keys to `st`. + """Lend explicit snapshots plus `_LENDABLE` and `__shared` buckets; defer other keys to `st`. `lend(rt, user_tables=doc)` — keyword names are STORE KEYS. Call it once per read pass and hand the result to every predicate in that pass; see the note above for what makes it safe. diff --git a/platform/core/users.py b/platform/core/users.py index 1e91edee1eb2d4ed0c91f396c095aeb5f823c5cb..8c76408324ca8edf1d43c74b46617cc70b9938e8 100644 --- a/platform/core/users.py +++ b/platform/core/users.py @@ -21,6 +21,7 @@ import time from urllib.parse import quote import core.store as store +from core import freeze_notice BU_LABELS = {5: 'Fisch', 6: 'Royal'} _ITER = 200_000 @@ -327,6 +328,7 @@ def set_mfa(username, secret, recovery_codes, *, require_unenrolled=False, expec enrolment check lets two workers both issue a first set of codes; an epoch check prevents regeneration racing an admin reset from silently reinstalling the lost factor. """ + freeze_notice.check_write() username = (username or '').strip().lower() # The replay closure captures only hashes, never printable recovery codes: the HF store # journals closures for conflict replay, and that journal must not retain clear codes. @@ -380,6 +382,14 @@ def clear_mfa(username): def consume_recovery_code(username, code) -> bool: username = (username or '').strip().lower() wanted = _recovery_key(code) + if freeze_notice.enabled(): + rec = (registry() or {}).get(username) or {} + matched = False + for stored in rec.get('recovery_codes') or []: + matched |= hmac.compare_digest(stored, wanted) + if matched: + freeze_notice.check_write() + return False consumed = [False] def _set(reg): @@ -483,6 +493,8 @@ def login_lockout_remaining(identifier, now=None): def note_login_failure(identifier, now=None): """Atomically count one failed password/MFA proof and return lock seconds remaining.""" + if freeze_notice.enabled(): + return login_lockout_remaining(identifier, now=now) now = time.time() if now is None else float(now) key = _login_lockout_key(identifier) @@ -552,6 +564,8 @@ def note_login_failure(identifier, now=None): def clear_login_failures(identifier): """Clear the account's shared guessing budget after a complete successful proof.""" + if freeze_notice.enabled(): + return None key = _login_lockout_key(identifier) def _clear(doc): @@ -747,7 +761,8 @@ def create_user(username, pw, name, role='user', bus='all', modules='all', the carry-and-bump lives here rather than in each caller: an overwrite is at least as session-invalidating as a password change, and it usually IS one. """ - username = (username or '').strip().lower() + original_username = (username or '').strip() + username = original_username.lower() if not username: raise ValueError('username and password are required') # Refuse before the update so even an empty password names its broken rules and writes nothing. @@ -769,6 +784,8 @@ def create_user(username, pw, name, role='user', bus='all', modules='all', # their own plane. None = leave as it was; True/False = set it deliberately. platform_admin=(prior.get('platform_admin') is True if platform_admin is None else platform_admin is True)) + # Display spelling is separate from the key used by auth, grants and workspaces. + rec['username_original'] = prior.get('username_original') or original_username if prior: rec['epoch'] = int(prior.get('epoch') or 0) + 1 reg[username] = rec @@ -782,6 +799,7 @@ def set_password(username, pw, enforce=True): `enforce=False` is only for a caller that already validated (or a future migration) to rotate without re-reading. The admin password route is that one caller; all others enforce here. """ + freeze_notice.check_write() username = (username or '').strip().lower() if enforce: try: @@ -915,6 +933,8 @@ _STAMPS_LOCK = __import__('threading').Lock() def _stamp(username, fields): """Merge `fields` into ONE account record, on a background thread, with a fresh read.""" + if freeze_notice.enabled(): + return None username = (username or '').strip().lower() if not username or not fields: return None @@ -926,6 +946,8 @@ def _stamp(username, fields): return reg def _work(): + if freeze_notice.enabled(): + return try: store.update('users', _set) # sync: fresh strict read + blocking upload except Exception: diff --git a/platform/harness/amazon_store.py b/platform/harness/amazon_store.py index 63562ab46cc9a24b953cad966b7c55948bb4aa16..7238f4ef1d1d0344978d7423cd6b2161fc3a5fc9 100644 --- a/platform/harness/amazon_store.py +++ b/platform/harness/amazon_store.py @@ -2712,6 +2712,55 @@ def _refused_reports(con): if (state := _read_state(con, key))}) +def plain_refusal_reason(reason, *, status="", table="", operation=""): + """Project a current or legacy refusal to reason/action copy; never echo vendor prose.""" + text = str(reason or "") + lower = text.lower() + status = str(status or "").upper() + if "within two years" in lower or "within the past two years" in lower: + return "Amazon refused this date range. Choose a start date within the past two years and retry sync." + if "reportperiod" in lower or "reporting period" in lower: + return "Amazon needs a reporting period for this report. Check the date range and retry sync." + if status == "CANCELLED" or "cancelled" in lower: + return "Amazon cancelled this report. Retry sync to request it again." + if status == "FATAL" or "fatal" in lower or "could not prepare" in lower: + return ("Amazon could not prepare this report. " + "Check its date range and seller eligibility, then retry sync.") + if (status == "429" or re.search(r"\b(?:http(?: status)?|status(?: code)?|answered)\s*[:=]?\s*429\b", lower) + or "throttl" in lower or "limiting requests" in lower): + return "Amazon is limiting requests. Sync resumes after Amazon's waiting period." + if "read-only" in lower or "operationrefused" in lower or "cannot request that report" in lower: + return "This connector cannot request that report. Ask an administrator to review the Amazon connection." + if (status in ("NOT_OFFERED", "403") + or re.search(r"\b(?:http(?: status)?|status(?: code)?|answered)\s*[:=]?\s*403\b|\(403\)", lower) + or "not allowed" in lower + or "required report role" in lower or "needs the" in lower + or "not granted" in lower or "does not offer" in lower): + cx = _client() + role = (cx.REPORT_TYPES.get(table) or {}).get("role") + role = role or " / ".join((cx.OPERATIONS.get(operation) or {}).get("roles", ())) + held = bool(role) and all(r.strip() in cx.ROLES_TAKEN + for r in str(role).replace(" / ", " + ").split(" + ") if r.strip()) + missing_access = bool(re.search(r"missing.*role|not granted|has not granted", lower)) + if not missing_access and (held or "already holds" in lower or "does not offer" in lower): + role_copy = f" despite the {role} role" if role else "" + return (f"Amazon does not offer this report to this seller account{role_copy}. " + "Check seller eligibility in Seller Central.") + role_copy = f"grant the {role} role" if role else "check the required Amazon roles" + return ("Amazon has not granted access to this report. " + f"Ask an administrator to {role_copy} and reconnect Amazon.") + return ("Amazon could not load this report. Retry sync; if it fails again, " + "ask an administrator to review the Amazon connection.") + + +def is_refusal_reason(reason): + """Recognize refusal facts in legacy status rows without first-load metadata.""" + return bool(re.search( + r"REFUSED:|\b(?:FATAL|CANCELLED|NOT_OFFERED)\b|Amazon (?:refused|cancelled|could not (?:prepare|load)|" + r"has not granted access|does not offer|is limiting requests|needs a reporting period)|This connector cannot request", + str(reason or ""), re.I)) + + def refused_reports(states): """Project already-read mirror markers without opening a writer connection.""" rows = [] @@ -2726,13 +2775,14 @@ def refused_reports(states): detail = {} except (ValueError, TypeError): detail = {} - reason = detail.get("reason") or state.get("detail") or "Amazon did not offer this report. Check seller eligibility and required roles." + reason = str(detail.get("reason") or state.get("detail") or "") legacy_status = re.search(r"\b(CANCELLED|FATAL)\b", reason) status = (detail.get("amazonStatus") or (legacy_status.group(1) if legacy_status else - "NOT_OFFERED" if "403" in reason or "not allowed" in reason.lower() else "REFUSED")) + "NOT_OFFERED" if re.search(r"\bHTTP\s*403\b|not allowed", reason, re.I) else "REFUSED")) spec = _client().REPORT_TYPES.get(table) or {} rows.append({"table": table, "label": spec.get("label") or "Amazon " + table.replace("_", " "), - "reason": reason[:500].replace("\u2014", "; ").replace("\u2013", " to ")[:500], + "reason": plain_refusal_reason(reason, status=status, table=table), + "diagnostic": str(detail.get("diagnostic") or reason)[:500], "amazonStatus": str(status)}) return rows @@ -2851,7 +2901,8 @@ def _sync_problem(exc, table, operation, tenant, rt, report, log): message = (f"{table}: Amazon report is {exc.status}; its saved report ID will be polled on the next pass. " "A report still pending after 24 hours is requested again.") log(message) - return {"state": "pending", "reason": message, "amazonStatus": exc.status} + return {"state": "pending", "reason": "Amazon is preparing this report. Sync will check it again automatically.", + "diagnostic": message, "amazonStatus": exc.status} if isinstance(exc, AmazonThrottled): retry = exc.retry_after state = "throttled" @@ -2863,7 +2914,7 @@ def _sync_problem(exc, table, operation, tenant, rt, report, log): elif isinstance(exc, AmazonOperationRefused): state = "refused" fix = "use an operation in the approved read-only scope; check the connector operation mapping" - elif "403" in str(exc): + elif re.search(r"\b(?:HTTP(?: status)?|status(?: code)?|answered)\s*[:=]?\s*403\b|\(403\)", str(exc), re.I): state = "refused" cx = _client() role = (cx.REPORT_TYPES.get(table) or {}).get("role") @@ -2873,6 +2924,7 @@ def _sync_problem(exc, table, operation, tenant, rt, report, log): # the sentence says which case applies instead of sending an admin to re-grant a held role. held = all(r.strip() in cx.ROLES_TAKEN for r in str(role or "").replace(" / ", " + ").split(" + ") if r.strip()) + held = held and not re.search(r"missing.*role|not granted|has not granted", str(exc), re.I) fix = (f"needs the {role} role; this application already holds it, so Amazon does not " "offer this report to this seller account" if role and held else f"needs the {role} role; grant it and reauthorize the application before retrying") @@ -2883,9 +2935,11 @@ def _sync_problem(exc, table, operation, tenant, rt, report, log): message = message.replace("\u2014", "; ").replace("\u2013", " to ") report["problems"].append(message) log(message) - return {"state": state, "reason": message, - "amazonStatus": str(getattr(exc, "status", "NOT_OFFERED" if "not allowed" in str(exc).lower() - or "403" in str(exc) else "REFUSED"))} + status = str(getattr(exc, "status", "NOT_OFFERED" if "not allowed" in str(exc).lower() + or re.search(r"\b(?:HTTP|answered)\s*403\b|\(403\)", str(exc), re.I) else "REFUSED")) + return {"state": state, "reason": plain_refusal_reason(message, status=status, table=table, + operation=operation), + "diagnostic": message, "amazonStatus": status} @contextmanager @@ -4000,7 +4054,8 @@ def sync(tenant_key="royal-imports", tables=None, since=None, log=print, rt=None _record_state(con, entity_key(refused), problem["state"], 0, problem["reason"]) if problem["state"] == "refused": _record_state(con, entity_key("refused_" + refused), "refused", detail=json.dumps({ - "reason": problem["reason"][:500], "amazonStatus": problem["amazonStatus"]})) + "reason": problem["reason"][:500], "amazonStatus": problem["amazonStatus"], + "diagnostic": problem.get("diagnostic", "")[:500]})) _record_state(con, entity_key("sync_tick_" + refused), "attempted" if problem["state"] == "pending" else "done", 0) except Exception: # noqa: BLE001 @@ -4386,6 +4441,205 @@ def snapshot_selftest(log=print, check=None): return len(failures) +def snapshot_sync_selftest(log=print, check=None): + """S70-T34: every current-state source lands once while history spans several windows. + + Exercise the real outer sync with an in-memory mirror. The report fixture returns the + same document rows for every requested window, so repeated pulls cannot hide behind + disjoint fixture SKUs. Saved-mirror restore and checkpoint integrity are separate tests. + """ + from collections import Counter + from copy import deepcopy + from types import SimpleNamespace + from unittest.mock import patch + import duckdb + cx = _client() + module = sys.modules[__name__] + failures = [] + + def ck(name, probe): + try: + ok = bool(probe()) + except Exception as exc: + ok = False + log(f" {name}: {type(exc).__name__}: {exc}") + if check: + check("[snapshot sync] " + name, ok) + else: + log(f" {'ok' if ok else 'FAIL'} {name}") + if not ok: + failures.append(name) + + class Clock(datetime): + instant = datetime(2026, 9, 25, 12, tzinfo=timezone.utc) + + @classmethod + def now(cls, tz=None): + return cls.instant.astimezone(tz) + + # Enumerate the vendor declarations independently of the store's snapshot helper. + snapshots = tuple(table for table, spec in cx.REPORT_TYPES.items() if spec.get("snapshot")) + selected = ("orders", "order_items", "transactions", *snapshots) + ck("snapshot census includes every declared current-state source", lambda: + bool(snapshots) and set(_snapshot_tables()) == set(snapshots)) + rt = SimpleNamespace(key="qa-snapshot-sync", get=lambda key, **_kwargs: ( + {"databases": {amazon_schema.DATABASE_PREFIX + t: t in selected for t in TABLES}} + if key == ENABLEMENT_KEY else None)) + start = Clock.instant - timedelta(days=65) + calls, generation = [], [0] + report_tables = {spec["report"]: table for table, spec in cx.REPORT_TYPES.items() + if table in snapshots and spec.get("report")} + order_reports = {cx.REPORT_TYPES["orders"][key] for key in ("report", "report_incremental")} + api_tables = {cx.REPORT_TYPES[table]["api"]: table for table in snapshots + if cx.REPORT_TYPES[table].get("api")} + orders = ("amazon-order-id\tsku\tquantity\tpurchase-date\torder-status\n" + f"O1\tS1\t1\t{_iso(start + timedelta(days=1))}\tShipped\n" + f"O1\tS2\t2\t{_iso(start + timedelta(days=1))}\tShipped\n" + f"O2\tS3\t3\t{_iso(start + timedelta(days=61))}\tShipped\n") + transactions = [{"transactionId": f"T{i}", "postedDate": _iso(start + timedelta(days=day)), + "totalAmount": {"currencyAmount": 10 * i, "currencyCode": "USD"}, + "breakdowns": [{"breakdownType": "Sales", "breakdownAmount": { + "currencyAmount": 10 * i, "currencyCode": "USD"}}], + "items": [{"contexts": [{"contextType": "ProductContext", "sku": f"S{j}"}]} + for j in (1, 2)]} + for i, day in enumerate((1, 30, 35), 1)] + + def size(table): + return (986 if table == "restock_recommendations" else 2) - generation[0] + + def expected_rows(table): + return size(table) + len(cx.REPORT_TYPES[table].get("json_extra_roots", ())) + + def document(_fields, report_type, **kwargs): + table = "orders" if report_type in order_reports else report_tables[report_type] + calls.append((table, kwargs)) + if table == "orders": + body = orders + else: + spec = cx.REPORT_TYPES[table] + records = [{"sku": f"S{i}", "asin": f"B{i + 1:09d}", "quantity": generation[0] + 2} + for i in range(size(table))] + if spec.get("json_root"): + data = {spec["json_root"]: records} + data.update({root: [{"status": "NORMAL"}] for root in spec.get("json_extra_roots", ())}) + body = json.dumps(data) + elif table == "inventory": + day = (Clock.instant - timedelta(days=1)).strftime("%m/%d/%Y") + body = ("date\tmsku\tfnsku\tasin\tdisposition\tlocation\tending-warehouse-balance\n" + + "".join(f"{day}\tS{i}\tF{i}\tB{i + 1:09d}\tSELLABLE\tUS\t2\n" + for i in range(size(table)))) + else: + header = "seller-sku\tasin1\tquantity\n" if table == "listings" else "sku\tasin\tquantity\n" + body = header + "".join(f"{r['sku']}\t{r['asin']}\t{r['quantity']}\n" for r in records) + return body, {"reportDocumentId": f"snapshot-sync-{len(calls)}", "createdTime": _iso(Clock.instant)} + + def records(_fields, operation, **kwargs): + table = api_tables[operation] + calls.append((table, kwargs)) + return [{"asin": f"B{i + 1:09d}", "sku": f"S{i}"} for i in range(size(table))] + + def finances(_fields, posted_after, posted_before): + calls.append(("transactions", {"start": posted_after, "end": posted_before})) + # Return the same full-history payload on every call, including boundary-overlap rows. + # The loader must partition and replace it without multiplying amounts or item rows. + return deepcopy(transactions) + + def snapshot_counts(requests): + return Counter(table for table, _kwargs in requests if table in snapshots) + + def history_counts(): + return (con.execute("SELECT COUNT(*), COUNT(DISTINCT amazon_order_id) FROM amzn_orders").fetchone(), + con.execute("SELECT COUNT(*), SUM(quantity) FROM amzn_order_items").fetchone(), + con.execute("SELECT COUNT(*), COUNT(DISTINCT transaction_id), SUM(total_amount) " + "FROM amzn_transactions").fetchone()) + + con = duckdb.connect(":memory:") + con.execute("CREATE TABLE _sync_state (entity VARCHAR PRIMARY KEY, phase VARCHAR, " + "last_id BIGINT, cursor_wd VARCHAR, rows BIGINT, updated_at VARCHAR)") + try: + with patch.object(module, "_connect_tenant_mirror", side_effect=lambda *_a: (con.cursor(), None)), \ + patch.object(module, "credential", return_value={"fixture": True}), \ + patch.object(module, "restore_mirror", return_value={"problems": []}), \ + patch.object(module, "persist_mirror"), \ + patch.object(module, "retention_check", return_value={"earliestTriggerAt": None}), \ + patch.object(module, "_sync_event"), \ + patch.object(module, "DEFAULT_BACKFILL_DAYS", 65), \ + patch.object(module, "datetime", Clock), \ + patch.object(cx, "report_document", side_effect=document), \ + patch.object(cx, "group_a_records", side_effect=records), \ + patch.object(cx, "transactions", side_effect=finances), \ + patch.object(cx, "awd_inventory", return_value=[]), \ + patch.object(cx, "access_token", side_effect=AssertionError("offline snapshot sync fixture")), \ + patch.object(cx.urllib.request, "urlopen", side_effect=AssertionError("offline snapshot sync fixture")): + first = sync(rt.key, tables=selected, rt=rt, first_load=True, + report_budget=len(snapshots) + 2, log=lambda *_a: None) + ck("first load requests every snapshot once alongside multiwindow history", lambda: + not first["problems"] and first["state"] == "partial" and first["remaining"] == 4 + and snapshot_counts(calls) == Counter(dict.fromkeys(snapshots, 1))) + offset = len(calls) + resumed = sync(rt.key, tables=selected, rt=rt, first_load=True, + report_budget=10, log=lambda *_a: None) + ck("history continuation never requests an already landed snapshot", lambda: + not resumed["problems"] and resumed["state"] == "done" and resumed["remaining"] == 0 + and not snapshot_counts(calls[offset:]) + and Counter(table for table, _kwargs in calls if table in ("orders", "transactions")) + == {"orders": 3, "transactions": 3}) + ck("repeated history documents retain full counts without multiplying totals", lambda: + history_counts() == ((2, 2), (3, 6), (6, 3, 60))) + for table in snapshots: + ck(table + " first load stores one document's rows", lambda: + con.execute(f"SELECT COUNT(*) FROM {table_name(table)}").fetchone()[0] == expected_rows(table)) + + # Advance beyond every real refresh floor; do not weaken vendor cadence in the fixture. + Clock.instant += timedelta(seconds=max(cx.REPORT_TYPES[t].get("min_interval_s", 0) + for t in snapshots) + 86400) + generation[0] = 1 + offset = len(calls) + resynced = sync(rt.key, tables=selected, since=start, rt=rt, log=lambda *_a: None) + resync_calls = calls[offset:] + ck("explicit multiwindow resync requests every snapshot exactly once", lambda: + not resynced["problems"] and snapshot_counts(resync_calls) == Counter(dict.fromkeys(snapshots, 1)) + and Counter(table for table, _kwargs in resync_calls if table in ("orders", "transactions")) + == {"orders": 3, "transactions": 3}) + ck("snapshot transports use no backfill window except the declared ledger day", lambda: + all((kwargs.get("start") == _iso(Clock.instant.replace(hour=0) - timedelta(days=1)) + and kwargs.get("end") == _iso(Clock.instant.replace(hour=0) - timedelta(seconds=1))) + if cx.REPORT_TYPES[table].get("dated_snapshot") else + kwargs.get("start") is None and kwargs.get("end") is None + for table, kwargs in resync_calls if table in snapshots)) + for table in snapshots: + date = (Clock.instant - timedelta(days=1) if table == "inventory" else Clock.instant).date().isoformat() + ck(table + " a new snapshot replaces older dates and removed rows", lambda: + con.execute(f"SELECT COUNT(*), COUNT(DISTINCT _snapshot_date), MAX(_snapshot_date) " + f"FROM {table_name(table)}").fetchone() == (expected_rows(table), 1, date)) + ck("resync history totals still match the full fixture history", lambda: + history_counts() == ((2, 2), (3, 6), (6, 3, 60))) + # A watermark replay preserves the mirror, so this catches plain append behavior + # that an explicit since (which clears history first) would otherwise conceal. + # Retain older rows outside the replay, so clearing all history cannot pass either. + older = _iso(start - timedelta(days=1)) + for table in ("orders", "order_items"): + con.execute(f"INSERT INTO {table_name(table)} SELECT * REPLACE " + "('kept-order' AS amazon_order_id, 'kept-sku' AS sku, 4 AS quantity, " + "? AS purchase_date, 'kept-history' AS _report_document_id) " + f"FROM {table_name(table)} WHERE amazon_order_id='O2'", [older]) + con.execute("INSERT INTO amzn_transactions SELECT * REPLACE " + "('kept-transaction' AS transaction_id, ? AS posted_date, 7 AS total_amount, " + "1 AS transaction_item_count, 7 AS transaction_breakdown_sales_amount, " + "'kept-history' AS _report_document_id) " + "FROM amzn_transactions WHERE transaction_id='T1' AND item_position=0", [older]) + for table in ("orders", "order_items", "transactions"): + record_watermark(con, table, start) + replay = sync(rt.key, tables=("orders", "order_items", "transactions"), rt=rt, log=lambda *_a: None) + ck("incremental multiwindow replay removes duplication without clearing history", lambda: + not replay["problems"] and all(replay["tables"][t]["incremental"] + for t in ("orders", "order_items", "transactions")) + and history_counts() == ((3, 3), (4, 10), (7, 4, 67))) + finally: + con.close() + return len(failures) + + def mirror_selftest(log=print, check=None): """S67-T32 (D-3031 + D-3481): the saved snapshot is one copy, whole, and restorable. @@ -4870,7 +5124,7 @@ def margin_selftest(log=print, check=None): got = _sync_problem(cx.AmazonError("vendor answered 403"), table, cx.REPORT_TYPES[table].get("api", "report_document"), "fixture", None, report, lambda *_: None) - return f"needs the {role} role" in got["reason"] + return role in got["reason"] with patch.dict(cx.REPORT_TYPES[table], {"role": "wrong role"}): red = not probe_role() ck(f"margin {table} 403 names its required role", red and probe_role()) diff --git a/platform/harness/datastore.py b/platform/harness/datastore.py index 8c11ed4d1920b55bc65635fea3df8d8273b89d9f..bbe6037e7f9da8727e43fe6148178c3ded911422 100644 --- a/platform/harness/datastore.py +++ b/platform/harness/datastore.py @@ -52,9 +52,8 @@ DB_PATH = Path(os.environ.get("AIOS_DUCKDB_PATH") or (_STORE_DIR / "royal.duckdb # # ⛔ WHY A PROBE AND NOT AN IMPORT. The pause flag lives in the tenant's store bucket, which is # `harness.runtime`'s to read — and `runtime` already imports THIS module (`_ds.DB_PATH`), so an -# import back would be a cycle. The harness installs the probe at its own import instead. Nothing -# else changes: with no probe installed (every gate, every ops script, `sync_runner`) the answer -# is False and this module behaves exactly as it did. +# import back at module initialization would be a cycle. The harness installs the pause probe +# at its own import. The frozen-config check below imports the sibling lazily at call time. # # ⚠ IT FAILS TOWARDS "NOT PAUSED", deliberately and consistently with the shipped decision one # layer up (`routes_keychain.odoo_paused`: "an unreachable flags bucket must never freeze a live @@ -63,6 +62,8 @@ DB_PATH = Path(os.environ.get("AIOS_DUCKDB_PATH") or (_STORE_DIR / "royal.duckdb # nobody notices for a week. Two copies of that policy would be worse than one; this is the same # one. _paused_probe = None +# Same tenant bucket as the API's `odoo_relational.CONFIG_KEY`; no upward API import is allowed. +_ODOO_CONFIG_KEY = "odoo_connector_config" def set_paused_probe(fn): @@ -73,7 +74,22 @@ def set_paused_probe(fn): def source_paused(): - """True when this store's tenant has its Odoo connector paused. Never raises.""" + """True when this mirror's tenant has paused or disconnected Odoo. Never raises.""" + try: + # Resolve afresh from the current mirror path, using the runtime's one tenant resolver. + # Credential deletion changes the resolved pause id; the durable freeze does not depend + # on that id and must still stop sync after deletion, path switches and process restarts. + from harness import runtime + key = runtime.tenant_for_store_path(DB_PATH) + if key: + rt = runtime.get_runtime(key) + if str(getattr(rt, "key", "") or "") == key: + cfg = rt.get(_ODOO_CONFIG_KEY, fresh=True) + if isinstance(cfg, dict) and cfg.get("frozen"): + return True + except Exception: + pass + # The lazy import also installs the ordinary pause probe for standalone sync workers. try: return bool(_paused_probe and _paused_probe()) except Exception: @@ -238,7 +254,7 @@ ENTITIES = { # carry a real barcode and 1,997 carry none. It is a `store=True` char column on # `product.product`, so the mirror can hold it. "fields": ["default_code", "name", "categ_id", "type", "standard_price", "active", - "barcode", "write_date"], + "barcode", "uom_id", "write_date"], # ⛔ WITHOUT THIS THE COLUMN ALTERS IN AND STAYS NULL FOREVER — the same trap # `account_move.invoice_origin` and the `res_partner` address block are both commented # for. WE moved the schema; no product's `write_date` moved, so an incremental @@ -254,7 +270,7 @@ ENTITIES = { # DOES NOT COME FROM HERE: it comes live from `modules/products.py::catalogue()`. This # column exists so the mirror is not permanently blind to a field the source has, which is # D-165's shape; widening `read_products` is a separate ticket with its own fence. - "backfill_fields": ["barcode"], # added 2026-08-24 (W41-T26) + "backfill_fields": ["barcode", "uom_id"], }, "account_move": { "model": "account.move", "archivable": False, @@ -339,8 +355,9 @@ ENTITIES = { # ⛔ `optional` — see `ready()`. A NEW entity must never gate the readiness of surfaces # that do not read it, or adding one takes the product down until it has synced. "model": "stock.move", "archivable": False, "optional": True, - "fields": ["product_id", "quantity_done", "date", "state", + "fields": ["name", "product_id", "product_uom", "quantity_done", "date", "state", "location_id", "location_dest_id", "picking_id", "write_date"], + "backfill_fields": ["name", "product_uom"], }, # ⭐ THE USAGE LOOKUP, WITHOUT WHICH THE ROW ABOVE CANNOT BE READ. `location_id` syncs as # `location_id` + `location_id_name` (the m2o convention) — a NAME, never a usage — so the @@ -353,7 +370,26 @@ ENTITIES = { # which; that choice lives in `model/metrics/stock.yml`, not here. "stock_location": { "model": "stock.location", "archivable": True, "optional": True, - "fields": ["name", "complete_name", "usage", "active", "write_date"], + "fields": ["name", "complete_name", "usage", "active", "parent_path", "write_date"], + "backfill_fields": ["parent_path"], + }, + "stock_valuation_layer": { + "model": "stock.valuation.layer", "archivable": False, "optional": True, + "fields": ["product_id", "quantity", "value", "create_date", "stock_move_id", "write_date"], + }, + "stock_quant": { + "model": "stock.quant", "archivable": False, "optional": True, + "fields": ["product_id", "location_id", "quantity", "reserved_quantity", "write_date"], + }, + "purchase_order": { + "model": "purchase.order", "archivable": False, "optional": True, + "fields": ["name", "partner_id", "date_approve", "date_order", "state", + "amount_untaxed", "amount_total", "currency_id", "write_date"], + }, + "purchase_order_line": { + "model": "purchase.order.line", "archivable": False, "optional": True, + "fields": ["order_id", "product_id", "product_uom", "product_uom_qty", "product_qty", + "qty_received", "price_unit", "price_subtotal", "date_planned", "write_date"], }, } @@ -939,14 +975,18 @@ NUMERIC_FIELDS = ("amount_untaxed", "amount_untaxed_signed", "amount_residual_si "price_subtotal", "margin", "purchase_price", "product_uom_qty", "standard_price", "debit", "credit", "balance", "amount", # W37-T13 — the stock-movement quantity. See the note above. - "quantity_done") + "quantity_done", "quantity", "value", "reserved_quantity", + "amount_total", "product_qty", "qty_received", "price_unit") + +# Odoo also names this many2one without the usual `_id` suffix. +MANY2ONE_FIELDS = frozenset({"product_uom"}) def _cols(spec): cols = ["id BIGINT PRIMARY KEY"] for f in spec["fields"]: base = f.removesuffix("_id") - if f.endswith("_id"): + if f.endswith("_id") or f in MANY2ONE_FIELDS: cols += [f"{base}_id BIGINT", f"{base}_name VARCHAR"] elif f in BOOL_FIELDS: cols.append(f"{f} BOOLEAN") @@ -981,7 +1021,7 @@ def _ensure_columns(con, key, spec): def _field_cols(f): """The store column(s) one spec field materialises into (m2o fields become _id + _name).""" - if f.endswith("_id"): + if f.endswith("_id") or f in MANY2ONE_FIELDS: base = f.removesuffix("_id") return [f"{base}_id", f"{base}_name"] return [f] @@ -1285,7 +1325,7 @@ def sync_all(entities=None, max_batches=200, log=print): _paused_notice("sync", log) return {} out = {} - for key in (entities or list(ENTITIES)): + for key in (list(ENTITIES) if entities is None else entities): t0 = time.time() phase, pulled = sync_entity(key, max_batches=max_batches, log=log) out[key] = {"phase": phase, "pulled": pulled, "secs": round(time.time() - t0, 1)} @@ -1312,7 +1352,7 @@ def reconcile_deletes(entities=None, log=print): con = connect() try: out = {} - for key in (entities or list(ENTITIES)): + for key in (list(ENTITIES) if entities is None else entities): spec = ENTITIES[key] # HOTFIX 2026-09-29: DETECT FIRST, then walk. The cursor sync has already pulled every # created row, so the mirror can only hold MORE rows than the source when something diff --git a/platform/harness/filter_sql.py b/platform/harness/filter_sql.py index 30e554655042da23a5105708efd122a15b1b6905..2356a4d30ba7f096d9ae9db088effa01cc0ce7d3 100644 --- a/platform/harness/filter_sql.py +++ b/platform/harness/filter_sql.py @@ -105,6 +105,14 @@ COHORT_OPS = frozenset({'anyOf', 'allOf', 'noneOf'}) COHORT_OP_ALIASES = {'eq': 'anyOf', 'neq': 'noneOf'} #: Mirrors types.ts MAX_COHORT_IDS. MAX_COHORT_IDS = 20 +#: A view names one id; its supplied membership uses the same SQL as a cohort. +VIEW_FIELD = '__view__' +MAX_VIEW_ID = 64 + + +def parse_view_id(value): + """Mirrors types.ts viewRuleId: one trimmed id, never comma-split.""" + return ('' if value is None else str(value)).strip()[:MAX_VIEW_ID] def parse_cohort_ids(value): @@ -278,6 +286,8 @@ def is_rule_active(rule, columns): if not isinstance(rule, dict): return False col = rule.get('colId') + if col == VIEW_FIELD: + return bool(parse_view_id(rule.get('value'))) if col == COHORT_FIELD: # a cohort leaf has no column to look up # Driven by the SET it names, so a value of "," is INACTIVE rather than active-and- # unanswerable. Mirrors the cohort branch at the top of types.ts isRuleActive. @@ -385,6 +395,9 @@ def _compile_leaf(rule, columns, state): op = rule.get('op') value = _as_str(rule.get('value')) + if col_id == VIEW_FIELD: + return _compile_view(rule, state) + # --- owner item 5: a COHORT leaf, before any column lookup ------------------------------- # `__cohort__` is not a column. Handled first for the same reason evalNode handles it first: # falling through to the column path would make it INACTIVE, and inactive means every row. @@ -578,6 +591,23 @@ def _compile_multi_value(op, v, t, rule, used): return used((f"(NOT {union})" if op == 'hasNoneOf' else union), vals) +def _compile_view(rule, state): + """Compile supplied view membership through the cohort compiler, without resolving views. + + A synthetic key preserves singular view ids containing commas and keeps empty, unresolved + and NULL-id semantics in one implementation. Missing membership never becomes a complement. + """ + sets = state.get('cohort_sets') + if sets is None: + raise ValueError('a view condition needs cohort_sets (the supplied view membership sets)') + if rule.get('op') not in ('inView', 'anyOf', 'eq', 'notInView', 'noneOf', 'neq'): + raise ValueError('unsupported view membership operator') + key = '__view_members__' + op = 'noneOf' if rule.get('op') in ('notInView', 'noneOf', 'neq') else 'anyOf' + return _compile_cohort({'colId': COHORT_FIELD, 'op': op, 'value': key}, + {**state, 'cohort_sets': {key: sets.get(parse_view_id(rule.get('value')))}}) + + def _compile_cohort(rule, state): """Port of evalNode's cohort branch: membership in a SET of hand-curated sets of row ids. @@ -672,8 +702,8 @@ def compile_filter_tree(nodes, conj='and', columns=None, member_ids=None, id_sql `today` is the TENANT'S today, required by any relative date condition (`is within …`, `is before [one month ago]`) and by nothing else — an `exact` date needs none, which is what - keeps every pre-anchor caller working unchanged. `cohort_sets` is `{cohortId: [pid, ...]}` - for cohort-membership leaves. Both REFUSE when needed and missing; neither defaults. + keeps every pre-anchor caller working unchanged. `cohort_sets` supplies `{id: [pid, ...]}` + for cohort and view membership leaves. Both REFUSE when needed and missing; neither defaults. """ state = {'aggregate': False, 'used': set(), 'today': today, 'cohort_sets': cohort_sets, 'id_sql': id_sql} diff --git a/platform/harness/meta_store.py b/platform/harness/meta_store.py index 8f8d0e7a2251347f7f351688c59d4965cd810fbc..67298575024bb99e71f5dab8a70e4576e96bfa85 100644 --- a/platform/harness/meta_store.py +++ b/platform/harness/meta_store.py @@ -214,9 +214,8 @@ def _env_token(): return "" -def token(rt=None): - """The Meta token for `rt`: **the tenant's stored keychain credential first, the environment - only as a fallback**; "" when neither answers. +def token(rt=None, *, tenant_key=None): + """The tenant's stored Meta credential first; environment fallback only for Royal Imports. ⭐⭐ W49-T13 / D-202 — THE PRECEDENCE IS THE WHOLE DEFECT, NOT THE CALL. This module read `META_ADS_ACCESS_TOKEN` unconditionally, so a tenant whose administrator had stored a Meta @@ -225,28 +224,26 @@ def token(rt=None): that merely proves `keychain.meta_creds` is CALLED cannot see that; the assertion has to drive both sources at once and name which one won. - ⛔ IT FAILS CLOSED ON THE TENANT QUESTION. With no `rt` there is no tenant to read a keychain - for, so this answers from the environment ALONE rather than resolving a default runtime. - Resolving one would read tenant #0's stored credential on behalf of whatever tenant is - actually syncing, which is the precise elevation `core/keychain.meta_creds` refuses an env - fallback for in the first place. - - ⚠ THE ENV FALLBACK SURVIVES BECAUSE D-202 ASKS FOR IT (*"with the env variable as fallback - only"*) and because removing it would take tenant #0's live Meta sync offline. It is the - weaker half of the pair: a tenant with no stored entry still reaches the deployment token, and - that residual is booked rather than silently kept. + With no runtime, only an explicitly supplied `royal-imports` tenant key authorizes the + standalone tenant #0 fallback. Missing or unresolved non-Royal tenants get "". When both a + runtime and requested tenant key are supplied, they must agree before any credential is read. ⚠ THE FIELD NAME IS RESOLVED, NOT ASSUMED. A Meta credential is conventionally stored under `access_token`, but `token` and `api_key` are both accepted by the connector that stores it, so an entry using either is honoured here. An entry of the right TYPE carrying none of the - three yields "" from the keychain and the environment answers — an unreadable credential must - not be quieter than a missing one, so `sync` reports the empty token in as many words. + three yields no stored token; only Royal may then use the deployment token. `sync` reports a + missing credential without disclosing any field. ⛔ NO CREDENTIAL IS PRINTED, LOGGED OR RAISED FROM HERE. The return value is the only place the secret exists, and every failure path returns "" rather than echoing what it read. """ + requested = str(tenant_key or "").strip().lower() + runtime_key = "" if rt is not None: try: + runtime_key = str(getattr(rt, "key", "") or "").strip().lower() + if tenant_key is not None and runtime_key != requested: + return "" import core.keychain as _keychain fields = _keychain.meta_creds(rt) or {} for name in _META_TOKEN_FIELDS: @@ -254,11 +251,15 @@ def token(rt=None): if stored: return stored except Exception: # noqa: BLE001 - # A locked or unreadable keychain costs the STORED credential and nothing else. It - # must not take the sync down, and it must not carry the exception outward: the - # message could quote a field this function exists to keep out of a log. + # The exception may quote a credential field; it must never leave this resolver. pass - return _env_token() + royal = runtime_key == "royal-imports" if rt is not None else requested == "royal-imports" + if royal: + try: + return _env_token() + except Exception: # noqa: BLE001 + pass + return "" def _get(path, tok, **params): @@ -409,7 +410,13 @@ def _ensure(con, table, fields, rows=None): insert. The alternative is a mirror that is permanently unwritable for one bad guess. """ want = {f: _coltype(f, rows) for f in fields} - have = {r[1]: str(r[2]).upper() for r in con.execute(f"PRAGMA table_info('{table}')").fetchall()} + # DuckDB raises CatalogException for table_info on a new tenant's absent table. + # Check existence first so the first pull reaches the create path below. + exists = con.execute( + "SELECT 1 FROM information_schema.tables WHERE table_catalog = current_database() " + "AND table_schema = 'main' AND table_name = ?", [table]).fetchone() + have = ({r[1]: str(r[2]).upper() for r in + con.execute(f"PRAGMA table_info('{table}')").fetchall()} if exists else {}) if have: clash = [f for f, ty in want.items() if f in have and have[f] != ty and not (have[f].startswith("VARCHAR") and ty == "VARCHAR")] @@ -464,9 +471,9 @@ def sync(tenant_key="royal-imports", tok=None, log=print, insights=True, insight """ # ⭐ W49-T13 / D-202 — THE TENANT IS PASSED, which is what makes "the tenant's stored # credential first" true of this loader rather than of a helper nobody calls with one. A - # runtime that cannot be resolved (an unregistered slug, an unavailable store) leaves `rt` - # None and `token()` answers from the environment exactly as it did before this ticket. - tok = tok or token(_runtime_for(tenant_key)) + # runtime that cannot be resolved retains the requested tenant identity: only the standalone + # Royal caller may use the deployment token; an unresolved non-Royal tenant fails closed. + tok = tok or token(_runtime_for(tenant_key), tenant_key=tenant_key) # ⛔ THE WINDOW IS A PARAMETER, NOT AN ENVIRONMENT READ AT CALL TIME — and the difference cost a # live deploy. `INSIGHTS_DAYS` binds at IMPORT (module scope), so `main._pull_meta`'s # `os.environ.setdefault("META_INSIGHTS_DAYS", "7")` executed AFTER this module was already @@ -479,8 +486,9 @@ def sync(tenant_key="royal-imports", tok=None, log=print, insights=True, insight report = {"tenant": tenant_key, "tables": {}, "problems": [], "accounts": []} if not tok: report["problems"].append( - "META_ADS_ACCESS_TOKEN is not set in the environment or in platform/.env, so nothing " - "was pulled. This is a missing CREDENTIAL, not a missing capability.") + "No Meta Ads credential could be resolved for this tenant, so nothing was pulled. " + "Add the tenant's credential under Settings > Keychains. The deployment token is " + "available only to Royal Imports.") return report path = datastore.path_for(tenant_key) diff --git a/platform/modules/product_data.py b/platform/modules/product_data.py index e746c77fe0330391a648370b5c2f9a52c79311e0..dc5f4db6a1e0632f5831e886a80f3769d22ba781 100644 --- a/platform/modules/product_data.py +++ b/platform/modules/product_data.py @@ -188,6 +188,7 @@ def image_field_definition(st=None): and deliberately claims the field for the Royal Imports Leadership account, as the product owner requested. """ + schema_only = getattr(st, 'schema_only', False) default = { 'key': IMAGE_FIELD_KEY, 'label': 'Image', @@ -210,6 +211,8 @@ def image_field_definition(st=None): # Old deployments may have a stored Image definition without the # creator/custom markers. Repair the metadata without disturbing # existing permissions, grants, or cells. + if schema_only: + return merged if merged != existing: shared_overlay.put_field(TABLE_KEY, IMAGE_FIELD_KEY, merged, st=st) try: @@ -221,6 +224,10 @@ def image_field_definition(st=None): pass return merged + # The built-in definition is deterministic. Schema discovery reads its detached + # metadata; seeding its stored definition and grants remains at the full read door. + if schema_only: + return dict(default) stored = shared_overlay.put_field(TABLE_KEY, IMAGE_FIELD_KEY, default, st=st) try: import core.shares as shares @@ -330,9 +337,13 @@ def table_ops(st=None): def shared_fields(st=None): """Return and lazily migrate tenant-wide product field definitions.""" from core import field_permissions - field_permissions.migrate_legacy_fields(TABLE_KEY, st=st, grant_topic='product_data') - image_field_definition(st=st) - return shared_overlay.fields(TABLE_KEY, st=st if st is not None else TABLE_OPS.st) + document = shared_overlay.document(TABLE_KEY, st=st) if getattr(st, 'schema_only', False) else None + field_permissions.migrate_legacy_fields(TABLE_KEY, st=st, grant_topic='product_data', + shared_document=document) + if not getattr(st, 'schema_only', False): + image_field_definition(st=st) + return shared_overlay.fields(TABLE_KEY, st=st if st is not None else TABLE_OPS.st, + document=document) def shared_cells(pids, st=None): diff --git a/web/public/AIOS_DEPLOY_RECEIPT b/web/public/AIOS_DEPLOY_RECEIPT index 78592ac836eb8e2a505c3111cb6c8ad0fb1151d5..89e9883fe4fee285dd95ce116bbb247a5ad4ffb4 100644 --- a/web/public/AIOS_DEPLOY_RECEIPT +++ b/web/public/AIOS_DEPLOY_RECEIPT @@ -1 +1 @@ -f9c22c30ffdf9fb74c6f7ebf0b8fdba8cd17d7ca2692568176dcf6e4337d41f8 +6ec69a903ff975a3a93d72b03ad3ca14ee11d2c218c18f7a3e0f9f5cc05ec1fa diff --git a/web/public/favicon.svg b/web/public/favicon.svg index 9a8288bac88e71235e39803425a652f7c548d677..ffee24511fe064fe9c3fc8b49ffd2c58c837986e 100644 --- a/web/public/favicon.svg +++ b/web/public/favicon.svg @@ -1,8 +1,14 @@ - - + + + + diff --git a/web/src/automation/AutomationBuilder.tsx b/web/src/automation/AutomationBuilder.tsx index 514e1e50100b32499eae3ae8f35789373db3dc2c..691f36f0f3afcb537efab1ee35d83da0012386da 100644 --- a/web/src/automation/AutomationBuilder.tsx +++ b/web/src/automation/AutomationBuilder.tsx @@ -32,6 +32,8 @@ // --------------------------------------------------------------------------- import type { ReactNode } from "react"; import { Fragment, useEffect, useRef, useState } from "react"; +import { useEscapeClose } from "../ui/useEscapeClose"; +import { useOverlayLayer } from "../customer-grid/OverlaySurface"; // ⭐ WAVE 30 · ITEM 7 (R3 + R4) — connector identity, from the ONE module that owns it. Not a // second copy of two SVGs: the directory, this menu and the trigger picker all answer "which @@ -431,6 +433,29 @@ function tableLabel(tables: UserTable[], key: string): string { return tables.find((t) => t.key === key)?.label || key; } +/** Mounted only while confirming; its claim sits above the builder's menus. */ +function ChangeTriggerDialog({ label, onCancel, onConfirm }: { + label: string; + onCancel: () => void; + onConfirm: () => void; +}) { + const panelRef = useRef(null); + useOverlayLayer({ + panelRef, + onDismiss: onCancel, + dismissOnOutside: false, + trapFocus: true, + }); + return
+

Change the trigger?

+

Anything configured for {label} is dropped.

+
+ + +
+
; +} + export default function AutomationBuilder({ automation, agentContract, @@ -492,25 +517,32 @@ export default function AutomationBuilder({ * open-ness for the `menu` variant precisely because the caller owns the anchor. */ const addWrap = useRef(null); + const actionMenuRef = useRef(null); + const actionAddRef = useRef(null); + const closeTriggerPicker = () => { + setPicking(false); + addWrap.current?.querySelector(".autox-add")?.focus(); + }; + useEscapeClose(picking, closeTriggerPicker, { container: () => addWrap.current }); + useEscapeClose(adding, () => { + setAdding(false); + actionAddRef.current?.focus(); + }, { container: () => actionMenuRef.current }); - // Escape and an outside click both close it — the `field` variant's rule, applied to the twin - // that had neither. Without this the empty state's menu was a one-way door: every row picks a - // trigger, so changing your mind meant picking one you did not want. + // Ordinary menus still dismiss outside; Escape belongs to the shared arbiter. useEffect(() => { - if (!picking) return; - const onKey = (e: KeyboardEvent) => { - if (e.key === "Escape") setPicking(false); - }; + if (!picking && !adding) return; const onDown = (e: MouseEvent) => { - if (!addWrap.current?.contains(e.target as Node)) setPicking(false); + const target = e.target as Node; + if (picking && !addWrap.current?.contains(target)) setPicking(false); + if (adding && !actionMenuRef.current?.contains(target) + && !actionAddRef.current?.contains(target)) setAdding(false); }; - window.addEventListener("keydown", onKey); window.addEventListener("mousedown", onDown); return () => { - window.removeEventListener("keydown", onKey); window.removeEventListener("mousedown", onDown); }; - }, [picking]); + }, [picking, adding]); const trigger = automation.trigger || null; const triggerKey = trigger?.key || (automation.schedule?.enabled ? "schedule" : "manual"); @@ -1429,7 +1461,7 @@ export default function AutomationBuilder({ value={triggerKey} disabled={busy} onPick={onPickTrigger} - onDismiss={() => setPicking(false)} + onDismiss={closeTriggerPicker} /> ) : null}
@@ -1505,6 +1537,7 @@ export default function AutomationBuilder({ - - - + setConfirmKey("")} + onConfirm={() => { + onPickTrigger(confirmKey); + setConfirmKey(""); + }} /> ) : null} {/* ⛔ THE PICKER USED TO RENDER HERE, and here is outside both columns. `.auto-work` is a @@ -2147,7 +2165,7 @@ function TriggerProps({ id="autox-thook" className="auto-input is-mono" readOnly - value={`/api/v1/automations/hook/${trigger.token}`} + value={`/api/v1/automations/${encodeURIComponent(automation.id)}/hook/${trigger.token}`} />

Minted once. It survives every other change to this trigger.

@@ -2328,6 +2346,14 @@ function NewDatabase({ const [name, setName] = useState(""); const [busy, setBusy] = useState(false); const [err, setErr] = useState(""); + const addButtonRef = useRef(null); + const cancel = () => { + if (busy) return; + setOpen(false); + setErr(""); + window.requestAnimationFrame(() => addButtonRef.current?.focus()); + }; + useEscapeClose(open, cancel); const create = () => { const label = name.trim(); @@ -2358,6 +2384,7 @@ function NewDatabase({ @@ -3674,10 +3695,8 @@ export function ActionProps({ ); })}

- Leave these unticked to keep every post. Ticked, only the kinds you name are - kept — and a group cannot keep more than the {" "} - {Number((cfg as { maxPosts?: number }).maxPosts || 10) || 10} posts this - enrichment captures. + Leave these unticked to keep every post. Each selected type can keep up to {" "} + {Number((cfg as { maxPosts?: number }).maxPosts || 10) || 10} posts.

) : null} diff --git a/web/src/connectors/ConnectTutorial.tsx b/web/src/connectors/ConnectTutorial.tsx new file mode 100644 index 0000000000000000000000000000000000000000..23667c3f6849101d2e34a20c553ec98332e1c32c --- /dev/null +++ b/web/src/connectors/ConnectTutorial.tsx @@ -0,0 +1,326 @@ +// S70-T09: new-connection tutorials. The frame is the existing AI wizard; metadata comes +// from the connector detail. Secrets stay in this mounted draft until Done or dismissal. +import { useEffect, useRef, useState } from "react"; +import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, WORKSPACE_STALE_EVENT, checkTenant } from "../apiContract"; +import { ConnectionWizard, GuideSteps } from "../settings/ExternalAccessPane"; +import { clearConnectorDetailCache, parseConnectSpec } from "./ConnectorModal"; +import type { ConnectorConnectSpec } from "./ConnectorModal"; +import type { ConnectorRow } from "./ConnectorsPage"; + +export interface TutorialGuide { + need: string; + steps: string[]; + failureAction: string; + testNote: string; +} + +const screenText = (value: unknown) => typeof value === "string" + ? value.trim().replace(/[\u2013\u2014]/g, ", ") : ""; +const object = (value: unknown): Record => value !== null + && typeof value === "object" && !Array.isArray(value) ? value as Record : {}; + +export function parseTutorialGuide(value: unknown): TutorialGuide | null { + const row = object(value); + const need = screenText(row.need); + const steps = Array.isArray(row.steps) ? row.steps.map(screenText).filter(Boolean) : []; + const failureAction = screenText(row.failureAction); + const testNote = screenText(row.testNote); + return need && steps.length && failureAction && testNote ? { need, steps, failureAction, testNote } : null; +} + +type SetupSpec = ConnectorConnectSpec & { + hint: string; + fieldHints: Record; + tutorial: TutorialGuide; + initialDatabases: { key: string; label: string; selected: boolean }[]; +}; +type SetupRead = { phase: "loading" } | { phase: "error"; message: string } + | { phase: "held" } | { phase: "ready"; spec: SetupSpec }; + +// Never display raw provider/exception text: a failed probe can echo a submitted secret. +async function call(path: string, init?: RequestInit): Promise<{ + ok: boolean; status: number; body: Record; stopped?: boolean; +}> { + const response = await fetch(`${API_V1}${path}`, { credentials: CREDENTIALS, ...init }); + if (checkTenant(response)) return { ok: false, status: 409, body: {}, stopped: true }; + if (response.status === 401) { + window.dispatchEvent(new CustomEvent(UNAUTHORIZED_EVENT)); + return { ok: false, status: 401, body: {}, stopped: true }; + } + return { ok: response.ok, status: response.status, body: object(await response.json().catch(() => null)) }; +} + +function useStepFocus(step: number) { + const heading = useRef(null); + const previous = useRef(step); + useEffect(() => { + if (previous.current !== step) heading.current?.focus(); + previous.current = step; + }, [step]); + return heading; +} + +export default function ConnectTutorial(props: { + row: ConnectorRow; + onDone: () => void; + onOpenSettings: () => void; +}) { + return props.row.key === "webhooks" ? : ; +} + +function CredentialTutorial({ row, onDone, onOpenSettings }: { + row: ConnectorRow; + onDone: () => void; + onOpenSettings: () => void; +}) { + const [read, setRead] = useState({ phase: "loading" }); + const [readVersion, setReadVersion] = useState(0); + const [step, setStep] = useState(0); + const [label, setLabel] = useState(row.label); + const [scope, setScope] = useState(""); + const [values, setValues] = useState>({}); + const [selectedDatabases, setSelectedDatabases] = useState([]); + const [storedId, setStoredId] = useState(null); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(""); + const [uncertainSave, setUncertainSave] = useState(false); + const inFlight = useRef(false); + const mounted = useRef(false); + const heading = useStepFocus(step); + + useEffect(() => { + mounted.current = true; + return () => { mounted.current = false; }; + }, []); + + useEffect(() => { + const controller = new AbortController(); + setRead({ phase: "loading" }); + void (async () => { + try { + const response = await call(`/connectors/${encodeURIComponent(row.key)}/detail` + + "?databases_offset=0&databases_limit=1&fields=summary", { signal: controller.signal }); + if (controller.signal.aborted || response.stopped) return; + const keychain = object(response.body.keychain); + if (!response.ok || keychain.canConnect !== true) { + setRead({ phase: "error", message: response.status === 403 || keychain.canConnect === false + ? "Ask your workspace administrator for permission to connect this source." + : "Setup could not be loaded. Try again." }); + return; + } + // An existing connection belongs in Settings. Never reconnect it to stage a setup state. + if (Array.isArray(keychain.entries) && keychain.entries.length > 0) { + setRead({ phase: "held" }); + return; + } + const rawSpec = object(keychain.connect); + const spec = parseConnectSpec(rawSpec); + const tutorial = parseTutorialGuide(rawSpec.tutorial); + if (!spec || !tutorial) { + setRead({ phase: "error", message: "Setup instructions are unavailable. Open connection settings." }); + return; + } + const fieldHints: Record = {}; + for (const value of Array.isArray(rawSpec.fields) ? rawSpec.fields : []) { + const field = object(value); + if (typeof field.key === "string") fieldHints[field.key] = screenText(field.hint); + } + setValues(Object.fromEntries(spec.fields.map((field) => [field.key, field.default ?? ""]))); + setScope(spec.scopes[0]); + const initialDatabases = (Array.isArray(rawSpec.initialDatabases) ? rawSpec.initialDatabases : []) + .map(object).filter((item) => typeof item.key === "string" && typeof item.label === "string") + .map((item) => ({ key: String(item.key), label: screenText(item.label), selected: item.selected === true })); + setSelectedDatabases(initialDatabases.filter((item) => item.selected).map((item) => item.key)); + setRead({ phase: "ready", spec: { ...spec, hint: screenText(rawSpec.hint), fieldHints, tutorial, initialDatabases } }); + } catch { + if (!controller.signal.aborted) setRead({ phase: "error", message: "Cannot reach the server. Try again." }); + } + })(); + return () => controller.abort(); + }, [row.key, readVersion]); + + if (read.phase === "loading") return
+ +
; + if (read.phase === "error") return
+

{read.message}

+ + +
; + if (read.phase === "held") return
+

A key is already stored.

+ +
; + + const { spec } = read; + const complete = Boolean(label.trim()) && spec.scopes.includes(scope) + && spec.fields.every((field) => Boolean(values[field.key]?.trim())); + const hasSelection = spec.initialDatabases.length > 0; + const testStep = hasSelection ? 4 : 3; + const doneStep = testStep + 1; + const titles = ["What you need", "Get credentials", "Paste credentials", + ...(hasSelection ? ["Choose databases"] : []), "Test connection", "Done"]; + + const test = async () => { + if (inFlight.current || !complete || uncertainSave) return; + inFlight.current = true; + setBusy(true); + setError(""); + let entryId = storedId; + let saving = entryId === null; + try { + if (saving) { + const result = await call("/admin/keychain", { + method: "POST", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ label: label.trim(), type: spec.type, scope, + ...(hasSelection ? { initialDatabases: selectedDatabases } : {}), + fields: Object.fromEntries(spec.fields.map((field) => [field.key, values[field.key].trim()])) }), + }); + if (!mounted.current || result.stopped) return; + if (!result.ok) { + const code = object(result.body.error).code; + setError(result.status === 403 + ? "Ask your workspace administrator for permission to add this connection." + : code === "keychain_locked" + ? "Ask your workspace administrator to unlock the keychain, then try again." + : result.status >= 500 + ? "Open connection settings to check whether the key was saved before trying again." + : `The key was not saved. ${spec.tutorial.failureAction}`); + if (result.status >= 500) setUncertainSave(true); + return; + } + const id = object(result.body.entry).id; + if (typeof id !== "string" || !id) { + setUncertainSave(true); + setError("Open connection settings to find the saved key. Do not store it again."); + return; + } + entryId = id; + setStoredId(id); + clearConnectorDetailCache(); + window.dispatchEvent(new CustomEvent(WORKSPACE_STALE_EVENT)); + saving = false; + } + // Only this explicit button reaches the existing canonical test door. A retry uses the + // returned ID, never another create. Draft fields remain available after a failed Test. + const result = await call(`/admin/keychain/${encodeURIComponent(entryId!)}/test`, { method: "POST" }); + if (!mounted.current || result.stopped) return; + if (!result.ok || result.body.ok !== true) { + setError(result.status === 403 + ? "Ask your workspace administrator for permission to test this connection." + : result.status === 404 + ? "The saved key is unavailable. Open connection settings and select the current key." + : `Test failed. ${spec.tutorial.failureAction}`); + return; + } + clearConnectorDetailCache(); + setValues({}); + setStep(doneStep); + } catch { + if (!mounted.current) return; + if (saving) { + setUncertainSave(true); + setError("Cannot reach the server. Open connection settings to check whether the key was saved before trying again."); + } else { + setError("Cannot reach the server. Check your connection, then test the saved key again."); + } + } finally { + inFlight.current = false; + if (mounted.current) setBusy(false); + } + }; + + return
+ + {step > 0 && step < doneStep ? : null} + {step < testStep ? : null} + {step === testStep ? : null} + {step === doneStep ? : null} + }> + {step === 0 ?

{spec.tutorial.need}

: null} + {step === 1 ? <> + ({ text }))} /> + {spec.hint ?

{spec.hint}

: null} + : null} + {step === 2 ?
+
+ + setLabel(event.target.value)} /> +
+ {spec.scopes.length > 1 ?
+ + +
: null} + {spec.fields.map((field) =>
+ + setValues((current) => ({ ...current, [field.key]: event.target.value }))} /> + {spec.fieldHints[field.key] ?

{spec.fieldHints[field.key]}

: null} +
)} + {storedId || uncertainSave ? : null} +
: null} + {hasSelection && step === 3 ?
+ {spec.initialDatabases.map((database) => )} +
: null} + {step === testStep ? <> +

{screenText(label)}

+

{spec.tutorial.testNote}

+ {error ?
+

{error}

+ {storedId || uncertainSave ? : null} +
: null} + : null} + {step === doneStep ?

{spec.type === "stripe" || spec.type === "shopify" + ? "Key stored" : spec.type === "odoo" ? "Server reachable" : "Connected"}

: null} +
+
; +} + +function WebhookTutorial({ row, onDone }: { row: ConnectorRow; onDone: () => void }) { + const [step, setStep] = useState(0); + const heading = useStepFocus(step); + const guide = row.tutorial; + if (!guide) return
+

Webhook instructions are unavailable.

+ Automations +
; + const titles = ["What you need", "Set up webhook", "Check the run"]; + return
+ + {step > 0 ? : null} + {step < 2 ? + : } + }> + {step === 0 ?

{guide.need}

: null} + {step === 1 ? <> + ({ text }))} /> + Automations + : null} + {step === 2 ? <> +

{guide.testNote}

+

{guide.failureAction}

+ Automations + : null} +
+
; +} diff --git a/web/src/connectors/ConnectorModal.tsx b/web/src/connectors/ConnectorModal.tsx index bd1090083c7851cccab804f03e8483d7f29c0173..156f2e2bcfed5127db20b719beed6590c5575e6c 100644 --- a/web/src/connectors/ConnectorModal.tsx +++ b/web/src/connectors/ConnectorModal.tsx @@ -44,7 +44,7 @@ // later renders in the neutral tone instead of dropping the panel. // --------------------------------------------------------------------------- -import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react"; +import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react"; import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT } from "../apiContract"; import { BodyPortal, useOverlayLayer } from "../customer-grid/OverlaySurface"; import { ConfirmDialog } from "../ui/ConfirmDialog"; @@ -655,39 +655,14 @@ export function parseConnectorDetail(body: unknown, selectedKey: string): Connec }; } -/** - * The Access row's sentence, derived from C4's `keychain` half. - * - * ⛔ IT COMES FROM THE DETAIL PAYLOAD, NOT FROM THE CARD. The done-when says the Account, Access - * and Source rows are read from `GET /api/v1/connectors/{key}/detail`, and C4's `overview` has no - * `access` key to add one to. Reaching back into the directory row for `scope` would make one of - * three rows come from a different read, which is how two views of one connection drift apart. - * - * ⚠ BUSINESS WINS over personal for the same reason `_entry_types` says so on the server: a reader - * holding both is connected business-wide, and the stronger fact is the true one. - * - * ⛔⛔ IT TAKES THE STATUS TOO, AND THAT ARGUMENT IS THE WHOLE CORRECTNESS OF THIS ROW. Read from - * the entries alone, a connection with no KEYCHAIN entry reads as *"nobody has connected this - * yet"* — and most connected cards have no keychain entry at all: Odoo is connected through - * `env_odoo_available` on tenant #0, an OAuth provider is connected through a per-user slot, - * Webhooks and the Scraper hold no credential a keychain lists. So the modal would have said - * "nobody has connected this" beside a status pill reading `connected`, over a card whose own - * `scopeNote` says "Business-wide". That is the confidently-wrong shape this directory's server - * header was written about, and no source scan or type check can see it. - */ -export function accessSentence(keychain: ConnectorKeychainHalf, status: string): string { - const scopes = keychain.entries.map((entry) => entry.scope); - if (scopes.includes("business")) return "Business-wide, for everyone in this workspace"; - if (scopes.includes("personal")) return "Your own personal connection"; - if (keychain.entries.length) return "Connected, and this workspace did not record the scope"; - if (status === "connected") { - // True of every case that reaches here, and it names the cause rather than guessing at one: - // the connection is real, and its credential is not a keychain entry this reader can see. - return "Connected. The credential behind it is not one the keychain holds for this account."; - } - return keychain.canConnect - ? "Nobody has connected this yet. You can add a key from the Keychain tab." - : "Nobody has connected this yet, and this account cannot add a key for it."; +/** R22: access is a status from the detail payload, including connections without keychain rows. */ +export function accessSentence( + keychain: ConnectorKeychainHalf, status: string, connectorKey?: string, +): string { + if (status === "connected") return "Connected"; + if (status === "reconnect") return connectorKey === "amazon_sp" ? "Needs a test" : "Reconnect"; + if (status === "expired") return "Expired"; + return keychain.entries.length ? "Stored" : "Not connected"; } /** The brand mark, or the two-letter fallback tile the directory already uses (wave 30, R4). */ @@ -745,14 +720,14 @@ export function ConnectorOverviewPanel({
Account
{overview.account || "No account name was recorded for this connection."}
Access
-
{accessSentence(keychain, overview.status)}
+
{accessSentence(keychain, overview.status, overview.key)}
Source
{overview.source || "This connector did not name its source system."}

{overview.creates.length ? `Creates: ${overview.creates.join(", ")}` - : "Creates: this connector has not declared any databases in this workspace yet."} + : "Creates: no databases are available to you from this connection."}

{/* ⭐⭐ W49-T19 · R31 / C5 — THE PAID DOOR, AND IT NAMES WHAT IT DOES. The button opens the dialog; the dialog states the price before anything is spent. The @@ -968,6 +943,7 @@ export default function ConnectorModal({ }) { const connectorKey = row.key; const panelRef = useRef(null); + const closeRef = useRef(null); const [tab, setTab] = useState(initialTab); const [load, setLoad] = useState(() => { const cached = connectorDetailCache.get(connectorKey); @@ -1094,6 +1070,11 @@ export default function ConnectorModal({ restoreFocus: true, }); + // The overlay captures the opener first. Focus enters before paint, even while detail is pending. + useLayoutEffect(() => { + closeRef.current?.focus({ preventScroll: true }); + }, [connectorKey]); + useEffect(() => { let dead = false; const cached = reads === 0 ? connectorDetailCache.get(connectorKey) : undefined; @@ -1272,9 +1253,9 @@ export default function ConnectorModal({ // connected this" about a connection nobody had asked the server about yet. if (load.phase === "loading") { return ( -

- Reading this connector. -

+
+ +
); } if (load.phase === "error") { @@ -1327,9 +1308,9 @@ export default function ConnectorModal({ : undefined; if (deferredGraph === null) { return ( -

- Reading the relationships for this connection. -

+
+ +
); } if (deferredGraph && "error" in deferredGraph) { @@ -1432,13 +1413,13 @@ export default function ConnectorModal({ ref={panelRef} role="dialog" aria-modal="true" - aria-label={`${row.label} connector`} + aria-labelledby="conn-modal-title" data-overlay-kind="connector-detail" tabIndex={-1} >
- {title} + {title} {/* The server's own word, sentence-cased by CSS and never by `toUpperCase`. */} {/* ⚠ ONLY WHEN THERE IS A WORD. A modal opened from Settings > Keychains carries no card to read a state off, so until `/detail` lands there is nothing to say and an @@ -1473,6 +1454,7 @@ export default function ConnectorModal({ ) : null} +
+
+ {row.key === "whatsapp" ? + : row.key === "slack" ? + : row.key === "webhooks" || row.manage?.startsWith("keychain") ? ( + + ) + : row.kind === "mcp" && isClientId(client) ? ( + + ) : row.key === "loopable-api" ? ( + + ) : row.operation === AMAZON_REVIEWS_ACTION || row.action === AMAZON_REVIEWS_ACTION ? ( + + ) : row.manage === "automation" ? ( +
+ Automations + +
+ ) : null} +
+ + + + ); +} + /* * ⛔ `selectConnectorGraph` STOOD HERE AND IS DELETED BY W49-T12 / R26, together with the downward * expand it served. It was the page's second guard over a graph selected by `selectedConnectionKey` @@ -501,7 +568,7 @@ export function matches(row: ConnectorRow, query: string): boolean { ); } -/** R13: the workspace's connections lead each tier. A card counts once the workspace HOLDS its + /** R13: the workspace's connections lead each tier. A card counts once the workspace HOLDS its * connection, including one the server asks to be reconnected or renewed (Amazon's "reconnect" and * "expired"), because that source is still this workspace's. Stable sort keeps catalogue order * within each group; never mutate the input. */ @@ -533,10 +600,10 @@ export function connectedFirst(rows: readonly ConnectorRow[]): ConnectorRow[] { */ export function scopeNote(state: string, scope: string | undefined): string { if (state !== "connected") return ""; - if (scope === "personal") return "Your personal connection"; + if (scope === "personal") return "Personal"; // ⚠ NO EM DASH. Standing rule 2 bans U+2014 and U+2013 in any string that reaches a screen, and // the rule says REWRITE rather than merely delete the character, so the clause became a comma. - if (scope === "business") return "Business-wide, for everyone in this workspace"; + if (scope === "business") return "Connected"; return ""; } @@ -552,12 +619,16 @@ export const AMAZON_REVIEWS_ACTION = "amazon_reviews"; * Never infer readiness from a provider key or promote an unconfigured/catalogue row. */ export function isOperationalConnector(row: ConnectorRow): boolean { + if (row.section === "contact" || row.kind === "api") return true; // Recipe creation is a local, $0 database action and must stay available before a shared // scraper credential exists. The missing credential is validated only at explicit Run now. if (row.key === "scraper") return true; - if (row.state === "connected" || row.state === "reconnect") return true; + if (["connected", "reconnect", "expired"].includes(row.state)) return true; + // A frozen (disconnected) Odoo keeps its card so it can be connected again. + if (row.state === "disconnected") return true; if (row.state !== "available" || row.needs?.trim()) return false; - if (row.action) return row.action === AMAZON_REVIEWS_ACTION; + const operation = row.operation || (row.action === AMAZON_REVIEWS_ACTION ? row.action : ""); + if (operation) return operation === AMAZON_REVIEWS_ACTION; return true; } @@ -595,7 +666,29 @@ export default function ConnectorsPage({ onKeychain: () => void; }) { const [load, setLoad] = useState({ phase: "loading" }); + const [directoryError, setDirectoryError] = useState(""); const [query, setQuery] = useState(""); + const [directoryVersion, setDirectoryVersion] = useState(0); + const refreshDirectory = useCallback(() => setDirectoryVersion((value) => value + 1), []); + const [setupKey, setSetupKey] = useState(""); + const contactRef = useRef(null); + const [contactJump, setContactJump] = useState(0); + useEffect(() => { + const followHash = () => { + const params = new URLSearchParams(window.location.hash.split("?", 2)[1] ?? ""); + if (params.get("section") === "contact") { + setQuery(""); setCategory(ALL_CATEGORIES); setContactJump((value) => value + 1); + } + if (params.has("slackLink") || params.has("slack") || params.has("slackError") + || params.get("channel") === "slack") setSetupKey("slack"); + }; + followHash(); + window.addEventListener("hashchange", followHash); + return () => window.removeEventListener("hashchange", followHash); + }, []); + useEffect(() => { + if (contactJump && load.phase === "ready") contactRef.current?.scrollIntoView({ block: "start" }); + }, [contactJump, load.phase]); /** * ⭐⭐ W49-T12 · R26 — THE CARD OPENS A CENTERED MODAL, and this is the whole of the page's * share of it: which connector is open, by key. @@ -673,17 +766,20 @@ export default function ConnectorsPage({ const res = await fetch(`${API_V1}/connectors/directory`, { credentials: CREDENTIALS, }); + if (dead) return; + if (checkTenant(res)) return; + if (res.status === 401) window.dispatchEvent(new CustomEvent(UNAUTHORIZED_EVENT)); const body = (await res.json().catch(() => null)) as | { error?: { message?: string } } | null; if (dead) return; if (!res.ok) { - setLoad({ - phase: "error", - message: body?.error?.message || `The server answered ${res.status}.`, - }); + const message = body?.error?.message || `The server answered ${res.status}.`; + setDirectoryError(message); + setLoad((current) => current.phase === "ready" ? current : { phase: "error", message }); return; } + setDirectoryError(""); setLoad({ phase: "ready", rows: parseDirectory(body).filter(isOperationalConnector), @@ -691,13 +787,18 @@ export default function ConnectorsPage({ chips: parseCategories(body), }); } catch { - if (!dead) setLoad({ phase: "error", message: "Cannot reach the server." }); + if (!dead) { + setDirectoryError("Cannot reach the server."); + setLoad((current) => current.phase === "ready" ? current : { + phase: "error", message: "Cannot reach the server.", + }); + } } })(); return () => { dead = true; }; - }, []); + }, [directoryVersion]); /** * ⭐ W49-T19 — IS THERE ANYTHING FOR THE ADMIN ANSWER TO BE ABOUT. The directory is 130-odd @@ -706,7 +807,7 @@ export default function ConnectorsPage({ */ const hasPullRow = load.phase === "ready" && - load.rows.some((row) => row.action === AMAZON_REVIEWS_ACTION); + load.rows.some((row) => row.operation === AMAZON_REVIEWS_ACTION || row.action === AMAZON_REVIEWS_ACTION); /** * ⭐⭐ S67-T20 / C4 — THE ODOO CARD SAYS WHEN ITS DATA IS OLD. Staging's sync was dead for three @@ -716,7 +817,7 @@ export default function ConnectorsPage({ */ const hasOdooCard = load.phase === "ready" && - load.rows.some((row) => row.key === "odoo" && row.state === "connected"); + load.rows.some((row) => row.key === "odoo" && row.state === "connected" && !!row.manage); const [odooWarnings, setOdooWarnings] = useState([]); useEffect(() => { if (!hasOdooCard) return; @@ -760,7 +861,7 @@ export default function ConnectorsPage({ // ⭐ W49-T18 — TWO FILTERS, ONE GRID, AND THEY COMPOSE. The chip narrows the taxonomy and the // search box narrows the text; a reader who picks "Finance" and then types keeps both, which is // what every directory of this shape does and what the rail would be annoying without. - const rows = allRows.filter((r) => inCategory(r, category) && matches(r, query)); + const rows = allRows.filter((r) => (r.section === "contact" || inCategory(r, category)) && matches(r, query)); /** * ⭐⭐ W52-T01 · RULING R1 / CONTRACT C1 — THE TWO TIERS, PARTITIONED FROM THE SAME FILTERED * LIST. `spawns_tables === true` sorts first (Odoo, Amazon, Meta Ads at baseline); everything @@ -768,14 +869,15 @@ export default function ConnectorsPage({ * groups still honour the category chip and the search box, because they are sliced from `rows` * rather than from `allRows`. */ - const databaseRows = connectedFirst(rows.filter((r) => r.spawns_tables === true)); - const otherRows = connectedFirst(rows.filter((r) => r.spawns_tables !== true)); + const contactRows = rows.filter((r) => r.section === "contact"); + const toolRows = connectedFirst(rows.filter((r) => r.section !== "contact")); // ⚠ Resolved against `allRows`, not the filtered list: typing in the search box while a modal is // open must not close the modal out from under the reader. const openRow = allRows.find((row) => row.key === openKey); // ⚠ Same rule, and here it protects a PAID run: filtering the grid while a pull is in flight // must not unmount the dialog that is spending the wallet. const pullRow = allRows.find((row) => row.key === pullKey); + const setupRow = allRows.find((row) => row.key === setupKey); /** * ⭐⭐ W49-T19 — THE CARD'S DOOR, AND BOTH CONDITIONS ARE LOAD-BEARING. The row must carry the * action (there IS something behind this card) and the server must have said this reader may @@ -784,12 +886,32 @@ export default function ConnectorsPage({ * which is what the other unavailable doors on this page already do. */ const pullHandler = (row: ConnectorRow) => - canPull && row.action === AMAZON_REVIEWS_ACTION + canPull && (row.operation === AMAZON_REVIEWS_ACTION || row.action === AMAZON_REVIEWS_ACTION) ? () => setPullKey(row.key) : undefined; + function openConnector(row: ConnectorRow) { + if (row.action === "connect" && row.manage === "oauth" && row.startUrl) { + window.location.assign(row.startUrl); + return; + } + if (row.manage?.startsWith("keychain") && row.action !== "settings" + && !["connected", "reconnect", "expired"].includes(row.state)) { + setSetupKey(row.key); + return; + } + if (row.section === "contact" || row.kind === "api" || row.operation + || row.action === AMAZON_REVIEWS_ACTION || row.manage === "automation") { + setSetupKey(row.key); + return; + } + // The same server-scoped keychain tab creates personal member credentials. + setOpenTab(row.action === "connect" && row.manage?.startsWith("keychain") ? "keychain" : "overview"); + setOpenKey(row.key); + } + return ( -
+

Connectors

{load.message}

) : null} + {load.phase === "ready" && directoryError ?

{directoryError}

: null} {/* ⭐⭐ W31-T44 — THE LIST SAYS WHEN IT IS SHORT (owner item 6b). @@ -857,18 +980,19 @@ export default function ConnectorsPage({ they map is different, so a connector's card behaves exactly as it did before this ticket wherever it lands. */} - {databaseRows.length > 0 ? ( -
-

- Databases + {contactRows.length > 0 ? ( +
+

+ Contact

+

ways to reach loopable directly

- {databaseRows.map((row) => ( + {contactRows.map((row) => ( setOpenKey(row.key)} + onOpen={() => openConnector(row)} onPullReviews={pullHandler(row)} syncWarnings={row.key === "odoo" ? odooWarnings : undefined} /> @@ -876,18 +1000,18 @@ export default function ConnectorsPage({
) : null} - {otherRows.length > 0 ? ( -
-

- Other sources + {toolRows.length > 0 ? ( +
+

+ Tools your loopable agent can use

- {otherRows.map((row) => ( + {toolRows.map((row) => ( setOpenKey(row.key)} + onOpen={() => openConnector(row)} onPullReviews={pullHandler(row)} syncWarnings={row.key === "odoo" ? odooWarnings : undefined} /> @@ -895,6 +1019,13 @@ export default function ConnectorsPage({
) : null} + {setupRow ? ( + { setSetupKey(""); refreshDirectory(); }} + onPullReviews={pullHandler(setupRow)} pulling={pullKey !== ""} + onOpenApi={() => setSetupKey("loopable-api")} + onOpenDetails={() => { setSetupKey(""); setOpenTab("overview"); setOpenKey(setupRow.key); }} /> + ) : null} {openRow ? ( ) : null} @@ -1009,21 +1141,43 @@ export function OdooSyncStatus({ onOpenDatabases }: { onOpenDatabases?: () => vo void getOdooFreshness(controller.signal).then((next) => { if (!controller.signal.aborted) setFreshness(next); }); - const healthRead = fetch(`${API_V1}/connectors/odoo/detail`, { - credentials: CREDENTIALS, signal: controller.signal, - }).then(async (response) => { - const body: unknown = await response.json(); - if (!response.ok || !body || typeof body !== "object") throw new Error("Connector status unavailable."); - const databases = Array.isArray((body as Record).databases) - ? (body as Record).databases as unknown[] : []; - return databases.flatMap((item) => { - if (!item || typeof item !== "object") return []; - const row = item as Record; - const health = parseDatabaseSyncHealth(row.syncHealth); - if (!str(row.key) || !health) return []; - return [{ key: str(row.key), label: str(row.label) || str(row.key), health }]; - }); - }); + const healthRead = (async () => { + const healthByKey = new Map(); + const keys = new Set(); + let offset: number | null = 0; + let total: number | null = null; + while (offset !== null) { + const response = await fetch(`${API_V1}/connectors/odoo/detail` + + `?databases_offset=${offset}&databases_limit=50&fields=summary`, { + credentials: CREDENTIALS, signal: controller.signal, + }); + const body: unknown = await response.json(); + if (controller.signal.aborted || !response.ok || !body || typeof body !== "object") { + throw new Error("Connector status unavailable."); + } + const payload = body as Record; + const page = parseDatabasesPage(payload.databasesPage); + if (!page || page.offset !== offset || page.limit !== 50 || + !Array.isArray(payload.databases) || payload.databases.length > 50 || + (total !== null && page.total !== total) || + (page.next !== null && page.next >= page.total)) { + throw new Error("Connector status unavailable."); + } + total = page.total; + for (const item of payload.databases) { + if (!item || typeof item !== "object") continue; + const row = item as Record; + const key = str(row.key); + if (!key || keys.has(key)) continue; + keys.add(key); + const health = parseDatabaseSyncHealth(row.syncHealth); + if (health) healthByKey.set(key, { key, label: str(row.label) || key, health }); + } + offset = page.next; + } + if (keys.size !== total) throw new Error("Connector status unavailable."); + return [...healthByKey.values()]; + })(); void Promise.all([getOdooStatus(controller.signal), getOdooSchema(controller.signal), healthRead]) .then(([nextStatus, nextSchema, nextHealth]) => { setStatus(nextStatus); setSchema(nextSchema); setDatabaseHealth(nextHealth); @@ -1171,7 +1325,7 @@ function SettingsGearIcon() { fill="none" stroke="currentColor" strokeWidth={1.3} - style={{ verticalAlign: "-0.13em", marginRight: 6 }} + className="conn-settings-icon" > @@ -1180,7 +1334,17 @@ function SettingsGearIcon() { } function ConnectorCardIdentity({ row }: { row: ConnectorRow }) { - const mark = brandForConnector(markKey(row.key), 22); + const client = row.clientId || row.key; + const mark = row.kind === "mcp" && isClientId(client) ? + : row.key === "loopable-api" ? + : row.key === "whatsapp" || row.key === "imessage" ? ( + + ) : brandForConnector(markKey(row.key), 22); return ( <> {/* ⭐ WAVE 30 · R4 — THE LOGO, OR THE LETTERS. Two mutually exclusive marks, and they take @@ -1198,11 +1362,11 @@ function ConnectorCardIdentity({ row }: { row: ConnectorRow }) { )} {row.label} - + {row.action !== "soon" ? {/* The server's own word, sentence-cased by CSS never by `toUpperCase`. R6 bans caps in app chrome, and a chip is chrome. */} - {row.state} - + {row.state === "available" ? "Not connected" : row.state} + : null} ); } @@ -1224,6 +1388,7 @@ function ConnectorCardHead({ row }: { row: ConnectorRow }) { ); } +/** C2: one reachable button. All secondary work belongs behind that door. */ export function ConnectorCard({ row, onKeychain, @@ -1233,252 +1398,35 @@ export function ConnectorCard({ }: { row: ConnectorRow; onKeychain: () => void; - /** - * ⭐⭐ S67-T20 / C4 — THIS CONNECTOR'S SYNC WARNINGS, already worded by `freshnessWarnings` - * ("Odoo data is stale: last synced ...", the last run's failure). Read-only text, never a - * control: the card keeps its one Settings door. Empty or absent renders nothing, which is how - * a fresh sync clears it. - */ - syncWarnings?: string[]; - /** - * ⭐⭐ W49-T12 · R26 — OPEN THE CONNECTOR MODAL. Optional for the same reason `onSelectGraph` - * was: the static proof sheets (`_conn_shot.tsx` and friends) paint this card without a door, - * and making the prop required would red `tsc` on files this ticket does not own. - * - * ⛔ AND THAT IS EXACTLY WHY `verify_ui`'s `connector_modal_faults` EXISTS. An optional prop the - * shipping page forgets to pass is a modal with no way in, compiling green - * ([[ui-invisible-to-assertions]], and W38-T19's Metrics toggle one wave over). The type system - * cannot enforce it, so the gate does. - */ onOpen?: () => void; - /** - * ⭐⭐ W49-T19 — OPEN THIS ROW'S OWN RUN DIALOG. Supplied by the page ONLY when the row carries - * the action AND the server said this reader may run it, so the card's whole test is "did - * somebody hand me this". A card never decides on its own that it may spend money. - * - * ⛔ AND IT IS WHY THIS CARD IS NOT MODAL-OPENABLE. A catalogue key has no `/detail` composer, - * so `ConnectorModal` would open on a 404; `openable` below withholds the modal door from any - * row carrying an action, which is what keeps this one interactive card honest while the other - * 117 stay faded. - */ onPullReviews?: () => void; - /* - * ⛔⛔ `onOpenDatabases` STOOD HERE AND IS DELETED BY W53-T28. It was W49-T14's door into the - * modal's Databases tab, and the card reached it through `OdooManage` -> `OdooSyncStatus`. The - * card no longer renders either: `OdooSyncStatus` and the "Manage fields in the Databases tab" - * link it owns are mounted inside `ConnectorModal`'s Sync cadence tab, where the jump is the - * modal's own `setTab("databases")`. R26's one-door rule is untouched; only the door's side of - * the wall changed. ⚠ Do not re-add it as a convenience: an optional prop on this card is one - * `onClick` away from being a second action, which is the thing this ticket removed. - */ + syncWarnings?: string[]; }) { - /** - * ⭐⭐ THE CARD HAS NO DOOR, AND THAT IS THE CONDITION EVERY GUARD BELOW WANTS — not the - * literal word `planned`, which is what they all used to read. - * - * ⛔⛔ TWO DIFFERENT ROWS RENDER THIS WAY AND THEY ARE NOT THE SAME CLAIM. A `planned` row is - * on the server's `PLANNED` roadmap and says in `needs` what it waits on; a `catalogue` row is - * a provider we can see in a vendor's directory and is waiting on nothing. W49-T18 gave the - * catalogue rows `planned` BECAUSE this fade keyed on that word, which put 117 listings into - * our own to-do list to buy a visual. Reading "has no door" instead is what let the state word - * go back to being true, and it is why the cards did not move a pixel when it did. - */ - const doorless = row.state === "planned" || row.state === CATALOGUE_STATE; - const connected = row.state === "connected"; - /** - * ⛔⛔ A ROW WITH A DIRECT ACTION IS NOT MODAL-OPENABLE, AND THIS IS THE LINE THAT SAYS SO. - * Making `md_amazon` neither planned nor catalogue is what puts a real control on its card; it - * must not also hand the card to `ConnectorModal`, whose Overview reads - * `/api/v1/connectors/{key}/detail` and would 404 on a catalogue key. So the action REPLACES - * the modal door rather than adding to it. - */ - const hasAction = !!row.action; - /** - * ⭐⭐ W52-T03 — SAME COMPUTATION AS R26's `openable`, KEPT UNDER ITS OWN NAME BECAUSE ITS JOB - * CHANGED: it used to decide whether the whole card took a click; it now decides whether the - * card carries the full-width Settings button at all. A `doorless` (planned/catalogue) row and - * an `hasAction` row (the one catalogue card with a direct action, e.g. Amazon reviews) still - * get NO button here — `ConnectorModal` reads `/detail`, which 404s for a catalogue key, so a - * Settings button on either would be the exact affordance-that-cannot-work R8 forbids. - */ - const openable = !!onOpen && !doorless && !hasAction; + const action = row.action === "soon" ? "soon" + : row.action === "settings" || ["connected", "reconnect", "expired"].includes(row.state) + ? "settings" : "connect"; + const label = action === "soon" ? "Soon" : action === "settings" ? "Settings" : "Connect"; + const disabled = action === "soon" || row.actionDisabled === true; + // Personal connections reach the same server-scoped keychain tab through onOpen. + // Keep the shell callback for legacy/static consumers that do not mount a detail modal. + const open = onOpen ?? onPullReviews ?? onKeychain; return ( -
- {/* ⭐⭐ W52-T03 — THE HEAD IS PLAIN, INFORMATIONAL MARKUP ONLY. Owner instruction - 23, verbatim: "instead we need to just put a long 'setting' button at the bottom - part of the card ... instead of" the whole card opening on click. The reachable - door is the full-width Settings button below, not the head. */} - -

{row.desc}

- {/* ⭐⭐ W49-T18 · R27 / C4 — WHERE THIS CARD IS FILED, AND HOW MUCH IS BEHIND IT. - Two measured facts and no sentence: the category is the one it is filed under, - and the endpoint count is the catalogue's own number for that provider. With - more than a hundred cards on this page, an explanatory line per card would be - the paragraph DESIGN.md §4 forbids. - ⛔ NOTHING HERE SAYS WHICH SIDE THE ROW CAME FROM. `provider` is stripped from - the payload before it leaves the server (C4), and R27's design rule is that the - distinction never renders: no badge, no tooltip, no different tone. */} - {row.category || row.endpoints ? ( -

- {row.category ? ( - {row.category} - ) : null} - {row.endpoints ? ( - - {row.endpoints} endpoint{row.endpoints === 1 ? "" : "s"} - - ) : null} -

- ) : null} - {/* ⭐⭐ WAVE 30 · R4 — THE DRILL, AND IT IS COLLAPSED FOR THE SAME REASON THE - TRIGGER MENU'S IS. The owner's words are a sequence, not a layout: *"Only when - I click 'Scraper' … would I see the option to choose either Instagram OR - TikTok."* So the platforms are behind a disclosure, not listed beside the - card's own sentence — and it is a real `
`, which opens on Enter and - Space with no keydown handler and carries no open/closed state to get wrong. - ⚠ These rows are INFORMATIONAL. A platform has no credential of its own, so it - offers no Connect button — the card's own action configures both, and a second - door here would be one that has nothing to open. */} - {row.platforms && row.platforms.length ? ( -
- - {row.platforms.length} platform{row.platforms.length === 1 ? "" : "s"} - - {row.platforms.map((p) => ( -
- - - {p.label} - {p.desc ? {p.desc} : null} - -
- ))} -
- ) : null} - {connected && row.connectedAs ? ( -

Connected as {row.connectedAs}

- ) : null} - {connected ? : null} - {/* ⭐ WAVE 32 · R4 — whose connection this is, said on the card where the fact - matters. Empty for everything else, so nothing new appears on the cards a - workspace has not connected. */} - {scopeNote(row.state, row.scope) ? ( -

{scopeNote(row.state, row.scope)}

- ) : null} - {/* ⚠ `needs` IS SHOWN WHATEVER THE STATE, and that is a correction: gating it on - "not connected" hid the one line that explains an `unconfigured` row, which is - the state it exists for ("the owner registers the client"). Only a DOORLESS - card suppresses it, because "not built yet" beside a card already marked planned - is the same sentence twice, and a catalogue listing has no `needs` to show. */} - {!doorless && row.needs ? ( -

{row.needs}

- ) : null} - {/* R13's honesty line for a token connector: the key is stored, the data arrives - later. Shown ON the card where the decision is, never in a paragraph. */} - {!doorless && row.note ?

{row.note}

: null} - {/* ── the ACTION. `manage` names WHERE it lives; this decides how to open it. - Every door here already existed — an OAuth start URL, the keychain dialog, - the automation surface — which is exactly why this page can be chrome: it - mints no new credential path (C11). An empty `manage` (a non-admin) renders - NO control rather than one that would 403. */} - {/* ⭐⭐ W49-T19 — THE CATALOGUE'S ONE DIRECT ACTION, AND IT IS THE DOOR THIS - TICKET EXISTS FOR. `manage` names a CREDENTIAL destination and this is not one: - the Monid key is already stored, and what the operator is choosing here is to - RUN something that spends it. So it reads off `action`, and it opens the run - dialog in place rather than a modal, because a catalogue key has no detail - route behind it (`openable` above). - ⛔ IT RENDERS ONLY WHEN THE PAGE HANDED DOWN A HANDLER, which happens only for - an admin whose session the server said may run it. A non-admin sees this card - exactly as they see every other catalogue card: readable, and with no control - on it. That is the same choice `manage: ""` already makes for a non-admin on - every keychain card, applied to a paid action. - ⚠ It wears `.conn-card-go`, the quiet text action every other card on this page - uses, and not a filled button: this is not the page's primary call to action, - and a second button family for one card would be a second visual vocabulary. */} - {onPullReviews ? ( - - ) : null} - {!doorless && row.manage === "oauth" && row.startUrl ? ( - - {row.state === "reconnect" ? "Reconnect" : "Connect"} - - ) : null} - {/* ⭐⭐ WAVE 32 items 6/11 (C2, R9/R10/R11) PUT ODOO'S SYNC PANEL ON THIS CARD, AND - W53-T28 (instructions 22/23) TAKES IT OFF AGAIN. W52-T03 deleted the plain - "Manage keys"/"Add keys" button from every other keychain card and spared Odoo's - because the modal could not yet configure grids, cadence and disconnect; W52-T04 - then built the Sync cadence tab that does all three, so the exception expired and - this branch goes with it. - ⛔ THE CARD IS NOT LOSING A CAPABILITY, IT IS LOSING A SECOND COPY OF ONE. The - panel behind this branch and the panel behind the modal's Sync cadence tab were - the same `OdooConfigPanel` over the same route, open at the same time, each with - its own state — two doors onto one setting, which is the failure R26 named on - this very page. Grids, cadence, server database and disconnect/reconnect are in - the Sync cadence tab; the key is in the Keychain tab; both are behind the one - Settings button below. ⛔ And the words "Manage keys"/"Add keys" stay retired - from every card, under any label. */} - {/* ⭐⭐ WAVE 33 · D-204 — THE MEMBER'S OWN DOOR. Same room, different promise: this - card connects an account that is YOURS, which is why the word is not "Manage - keys". It appears only for connectors whose credential may be personal — the - server decides that from `TENANT_WIDE_TYPES`, so a member never sees it on Odoo - or Meta Ads, where storing a personal key is refused. - ⚠ `connected` on the row is per-reader (`_entry_types` reads THIS user's - entries), so a member whose colleague connected Stripe business-wide still reads - the card as connected and is offered nothing to fix — which is correct: there is - nothing for them to do. */} - {!doorless && row.manage === "keychain_personal" ? ( - - ) : null} - {!doorless && row.manage === "automation" ? ( - - Open automations - - ) : null} - {/* ⭐⭐ W52-T03 — THE SETTINGS DOOR, owner instruction 23. A real ` - ) : null} -
+
+ + {syncWarnings?.length ? ( + Stale + ) : null} + +
); } diff --git a/web/src/connectors/DatabasesTab.tsx b/web/src/connectors/DatabasesTab.tsx index 2d724850fb53671624f2d5ad8957b9c5f0b39139..863b50c1ed8616a1ee81938d18b10e6f8d6b09a9 100644 --- a/web/src/connectors/DatabasesTab.tsx +++ b/web/src/connectors/DatabasesTab.tsx @@ -67,7 +67,7 @@ import type { ConnectorDatabaseField, ConnectorDetail, } from "./ConnectorModal"; -import { parseDatabaseExclusions, parseDatabaseFields } from "./ConnectorModal"; +import { parseDatabaseExclusions, parseDatabaseFields, parseDatabasesPage } from "./ConnectorModal"; import type { ConnectorRow } from "./ConnectorsPage"; import { watchMaterialization } from "./materializationJobs"; import type { MaterializationJob } from "./materializationJobs"; @@ -87,8 +87,9 @@ interface AmazonTableSync { nextDue: string | null; firstLoad: { remaining: number; etaSeconds: number; cause: string; needsAttention: boolean; - refused: AmazonRefusedReport[]; + refused?: AmazonRefusedReport[]; } | null; + refused?: boolean; stuck: boolean | null; stuckCause: string; unknownCause: string; @@ -292,9 +293,10 @@ function readableLag(seconds: number): string { return `${Math.floor(seconds / 86400).toLocaleString()}d`; } -export function DatabaseSyncSummary({ health, error = "" }: { +export function DatabaseSyncSummary({ health, error = "", errorCovered = false }: { health?: DatabaseSyncHealth; error?: string; + errorCovered?: boolean; }) { if (!health) return error ?

{error}

: null; const stateLabel: Record = { @@ -313,7 +315,7 @@ export function DatabaseSyncSummary({ health, error = "" }: { ? `Materialized ${readableSyncStamp(health.materializedTimestamp)}` : "Materialized timestamp unavailable"} {health.lagSeconds === null ? "Lag unavailable" : `Lag ${readableLag(health.lagSeconds)}`} - {health.error ? + {health.error && !errorCovered ? {health.error.code}: {health.error.message} : null}

; @@ -350,6 +352,7 @@ export function parseAmazonSyncStates(value: unknown): Record 0 && etaSeconds !== null && cause) ? { remaining: remaining ?? 0, etaSeconds: etaSeconds ?? 0, cause, @@ -361,6 +364,43 @@ export function parseAmazonSyncStates(value: unknown): Record> { + const states: Record = {}; + let offset: number | null = 0; + let total: number | null = null; + while (offset !== null) { + if (signal.aborted) throw new Error("Sync status read cancelled"); + const response = await fetch( + `${API_V1}/connectors/amazon_sp/detail` + + `?databases_offset=${offset}&databases_limit=${AMAZON_SYNC_PAGE_SIZE}&fields=summary`, + { credentials: CREDENTIALS, signal }, + ); + const raw: unknown = await response.json(); + if (signal.aborted) throw new Error("Sync status read cancelled"); + const next = response.ok ? parseAmazonSyncStates(raw) : null; + const page = parseDatabasesPage(syncObject(raw).databasesPage); + const databases = syncObject(raw).databases; + if (!next || !page || page.offset !== offset || page.limit !== AMAZON_SYNC_PAGE_SIZE || + !Array.isArray(databases) || databases.length > AMAZON_SYNC_PAGE_SIZE || + (total !== null && page.total !== total) || + (page.next !== null && page.next >= page.total)) { + throw new Error("Unreadable sync status"); + } + total = page.total; + for (const [key, state] of Object.entries(next)) { + if (!Object.prototype.hasOwnProperty.call(states, key)) states[key] = state; + } + offset = page.next; + } + if (Object.keys(states).length !== total) throw new Error("Incomplete sync status"); + return states; +} + function useAmazonSyncStates(enabled: boolean, detail: ConnectorDetail, syncPhase?: string) { const [states, setStates] = useState>({}); const [error, setError] = useState(""); @@ -374,13 +414,8 @@ function useAmazonSyncStates(enabled: boolean, detail: ConnectorDetail, syncPhas if (reading || controller.signal.aborted) return; reading = true; try { - const response = await fetch(`${API_V1}/connectors/amazon_sp/detail`, { - credentials: CREDENTIALS, signal: controller.signal, - }); - const raw: unknown = await response.json(); + const next = await readAmazonSyncStates(controller.signal); if (controller.signal.aborted) return; - const next = response.ok ? parseAmazonSyncStates(raw) : null; - if (!next) throw new Error("Unreadable sync status"); setStates(next); setError(""); } catch { @@ -417,8 +452,33 @@ function firstLoadEstimate(seconds: number): string { return rest ? `${plural(hours, "hour")} ${plural(rest, "minute")}` : plural(hours, "hour"); } -function amazonRefusalReason(reason: string): string { - return reason.replace(/^[A-Za-z_]\w*: [A-Za-z_]\w* REFUSED: [A-Za-z_]\w*: /, ""); +function amazonRefusalReason(reason: string, status = ""): string { + const text = reason.trim(); + const lower = text.toLowerCase(); + if (/within (?:the past )?two years/.test(lower)) { + return "Amazon refused this date range. Choose a start date within the past two years and retry sync."; + } + if (/reportperiod|reporting period/.test(lower)) { + return "Amazon needs a reporting period for this report. Check the date range and retry sync."; + } + if (status === "CANCELLED" || lower.includes("cancelled")) { + return "Amazon cancelled this report. Retry sync to request it again."; + } + if (status === "FATAL" || lower.includes("fatal")) { + return "Amazon could not prepare this report. Check its date range and seller eligibility, then retry sync."; + } + // Old containers and persisted snapshots can still deliver the producer's diagnostic sentence. + if (!/REFUSED:|FATAL|\b\w*(?:Error|Exception)\b|GET_[A-Z_]+|[{}]|SQLSTATE|SELECT\s|report[ _-]?id\b|\bHTTP\s*(?:403|429)\b/i.test(text)) return text; + if (/required role|missing.*role|not granted|needs the/.test(lower) && !/already holds/.test(lower)) { + return "Amazon has not granted access to this report. Ask an administrator to check the required Amazon roles and reconnect Amazon."; + } + if (/\bHTTP\s*403\b|not allowed|already holds/.test(lower)) { + return "Amazon does not offer this report to this seller account. Check seller eligibility in Seller Central."; + } + if (/\bHTTP\s*429\b|throttl/.test(lower)) { + return "Amazon is limiting requests. Sync resumes after Amazon's waiting period."; + } + return "Amazon could not load this report. Retry sync; if it fails again, ask an administrator to review the Amazon connection."; } export function AmazonFirstLoadNotice({ states, databaseNames }: { @@ -427,11 +487,11 @@ export function AmazonFirstLoadNotice({ states, databaseNames }: { // First-load facts belong to the connection, even though each row carries the same payload. const load = Object.values(states).find((state) => state?.firstLoad)?.firstLoad; if (!load || (load.remaining <= 0 && !load.needsAttention)) return null; - const refusedNames = [...new Set(load.refused.map((item) => + const refusedNames = [...new Set((load.refused ?? []).map((item) => databaseNames[`ut_amzn_${item.table}`] ?? item.label))]; return
{load.remaining > 0 ? - First load for connection: {load.remaining.toLocaleString()} reports remaining. + First load for connection: {plural(load.remaining, "report")} remaining. {" "}Estimated minimum time: {firstLoadEstimate(load.etaSeconds)}. {" "}{load.cause} : null} @@ -446,19 +506,21 @@ export function AmazonFirstLoadNotice({ states, databaseNames }: { export function AmazonSyncChip({ state, error, databaseKey }: { state?: AmazonTableSync | null; error?: string; databaseKey?: string; }) { - const report = state?.firstLoad?.refused.find((item) => `ut_amzn_${item.table}` === databaseKey); - const refusalReason = report ? amazonRefusalReason(report.reason) : ""; + const report = state?.firstLoad?.refused?.find((item) => `ut_amzn_${item.table}` === databaseKey); + const refusalReason = report ? amazonRefusalReason(report.reason, report.amazonStatus) : ""; const unknownCause = state?.unknownCause ?? ""; const cause = amazonRefusalReason(unknownCause.trim()).trim(); const reason = refusalReason.trim(); + const refused = Boolean(report || state?.refused || /REFUSED:|\b(?:FATAL|CANCELLED|NOT_OFFERED)\b|Amazon (?:refused|cancelled|could not (?:prepare|load)|has not granted access|does not offer|is limiting requests|needs a reporting period)|This connector cannot request/i.test(unknownCause)); const repeatsRefusal = Boolean(report && cause && reason && (cause.startsWith(reason) || reason.startsWith(cause))); const phase = state?.inProgress ? "syncing" : state?.stuck ? "failed" - : report ? "refused" : state?.deferred ? "deferred" + : refused ? "refused" : state?.deferred ? "deferred" : state?.lastSuccess || (state?.rowsLoaded ?? 0) > 0 ? "ready" : "unknown"; return
{state?.rowsLoaded === 0 && state.lastSuccess - ? "Loaded. Amazon returned no rows for this report." + ? refused ? "No rows retained from the last successful sync." + : "Loaded. Amazon returned no rows for this report." : state?.rowsLoaded == null || (state.rowsLoaded === 0 && !state.lastSuccess) ? "Not loaded yet" : `${state.rowsLoaded.toLocaleString()} rows loaded`} {report ? Amazon refused this report: {refusalReason} : null} @@ -467,7 +529,7 @@ export function AmazonSyncChip({ state, error, databaseKey }: { {state?.deferred ? Deferred: {state.deferred.reason} : null} {state?.nextDue ? Next due {readableSyncStamp(state.nextDue)} : null} {state?.stuckCause ? {state.stuckCause}. Retry sync to refresh progress. : null} - {unknownCause && !repeatsRefusal ? {unknownCause} : null} + {unknownCause && !repeatsRefusal ? {cause} : null} {!state ? {error || "Sync status is unknown in this container. Reload to check again."} : null}
; } @@ -2007,8 +2069,7 @@ const DatabasesTab = forwardRef {header}

- This connection has not declared any databases in this workspace. Connect it from the - Keychain tab to read its schema. + No databases are available to you from this connection.

); @@ -2138,7 +2199,8 @@ const DatabasesTab = forwardRef : null} + error={databaseSync.error} + errorCovered={showsAmazonSync && amazonSync.states[database.key]?.refused === true} />
- + {spec.scopes.length > 1 ? ( ) : (

- {spec.tenantWide - ? "Business-wide. This workspace's databases are read through this connection, so it applies to everyone here." - : scope === "personal" - ? "Personal, so only you. An administrator can add one for the whole workspace." - : "Business-wide, for everyone in this workspace."} + {scopeLabel(scope)}

)}
@@ -519,9 +512,11 @@ export default function KeychainTab({ const body = (await res.json().catch(() => null)) as | { ok?: boolean; message?: string; error?: { message?: string } } | null; - const said = res.ok - ? `${body?.ok ? "Working" : "Refused"}: ${body?.message || "the connector said nothing"}` - : body?.error?.message || `The server answered ${res.status}.`; + const said = row.key === "amazon_sp" + ? (res.ok && body?.ok === true ? "Connected" : "Needs a test") + : res.ok + ? `${body?.ok ? "Working" : "Refused"}: ${body?.message || "the connector said nothing"}` + : body?.error?.message || `The server answered ${res.status}.`; setTested((current) => ({ ...current, [entry.id]: said })); } catch { setTested((current) => ({ ...current, [entry.id]: "Cannot reach the server." })); diff --git a/web/src/connectors/MonidRunDialog.tsx b/web/src/connectors/MonidRunDialog.tsx index 961326dfdb1453dcb13fa14572dac1c9ef2d2d7d..036914524041033e8054a5e7456776b41ed96055 100644 --- a/web/src/connectors/MonidRunDialog.tsx +++ b/web/src/connectors/MonidRunDialog.tsx @@ -141,7 +141,7 @@ type Phase = | { kind: "refused"; message: string }; /** - * THE DIALOG. Portalled, focus-trapped, dismissed by Escape or a click outside, and it hands + * THE DIALOG. Portalled, focus-trapped, dismissed deliberately or by Escape, and it hands * focus back to the control that opened it. */ export default function MonidRunDialog({ @@ -173,7 +173,7 @@ export default function MonidRunDialog({ useOverlayLayer({ panelRef, onDismiss: onClose, - dismissOnOutside: true, + dismissOnOutside: false, initialFocus: "[data-overlay-autofocus]", // ⚠ EXPLICIT. The hook's own default is `false`. trapFocus: true, diff --git a/web/src/connectors/connectors.css b/web/src/connectors/connectors.css new file mode 100644 index 0000000000000000000000000000000000000000..c65603f1b4e308abdfa6cd67d6e10907db628804 --- /dev/null +++ b/web/src/connectors/connectors.css @@ -0,0 +1,308 @@ +/* S70-T07, DESIGN.md sections 2 to 5 and C1. All dimensions derive from house tokens. */ +.conn-directory .conn-title, +.conn-directory .conn-tier-heading { + font-weight: 600; +} + +.conn-directory .conn-tier-heading { + margin-bottom: calc(var(--lp-r-sm) * 3); +} + +.conn-directory .conn-tier-subtitle { + margin: calc(var(--lp-r-sm) * -2) 0 calc(var(--lp-r-sm) * 4); + color: var(--lp-muted); + font-size: var(--lp-fs-xs); + line-height: var(--lp-lh); +} + +.conn-directory .conn-tier + .conn-tier { + margin-top: calc(var(--lp-r-sm) * 8); +} + +.conn-directory .conn-grid { + grid-template-columns: repeat(auto-fill, minmax(min(100%, calc(var(--lp-fs-md) * 17)), 1fr)); + gap: calc(var(--lp-r-sm) * 4); +} + +.conn-directory .conn-card { + gap: calc(var(--lp-r-sm) * 4); + padding: calc(var(--lp-r-sm) * 4); + border: 0; + border-radius: var(--lp-r-md); + background: var(--lp-surface); + box-shadow: inset 0 0 0 calc(var(--lp-r-sm) / 4) var(--lp-line), + 0 var(--lp-r-sm) var(--lp-r-lg) color-mix(in srgb, var(--lp-ink) 8%, transparent); + transition: transform calc(var(--lp-fold-t) * 0.625) var(--lp-fold-e); +} + +.conn-card-open, +.conn-setup-action { + appearance: none; + border: 0; + background: transparent; + font: inherit; + color: inherit; +} + +.conn-directory .conn-card-open { + display: flex; + align-items: center; + justify-content: center; + gap: calc(var(--lp-r-sm) * 2); + margin: auto calc(var(--lp-r-sm) * -4) calc(var(--lp-r-sm) * -4); + padding: calc(var(--lp-r-sm) * 3) calc(var(--lp-r-sm) * 4); + border: 0; + border-top: calc(var(--lp-r-sm) / 4) solid var(--lp-line); + border-radius: 0 0 var(--lp-r-md) var(--lp-r-md); + background: transparent; + font: inherit; + font-size: var(--lp-fs-xs); + font-weight: 600; + color: var(--lp-ink); + cursor: pointer; +} + +/* The existing AI wizard frame and numbered guide, extracted for S70-T09. */ +.external-wizard { + position: relative; + overflow: hidden; + margin-top: calc(var(--lp-r-sm) * 4); + max-width: calc(var(--lp-fs-md) * 45); + background: var(--lp-surface); + border: calc(var(--lp-r-sm) / 4) solid var(--lp-line); + border-radius: var(--lp-r-lg); +} + +.external-wizard-progress { height: calc(var(--lp-r-sm) * 0.75); } +.external-wizard-progress > div { + height: 100%; + background: var(--lp-ink); + transform-origin: left; + transition: transform var(--lp-fold-t) var(--lp-fold-e); +} +.external-wizard-head { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: calc(var(--lp-r-sm) * 2.5); + padding: calc(var(--lp-r-sm) * 4) calc(var(--lp-r-sm) * 5); + border-bottom: calc(var(--lp-r-sm) / 4) solid var(--lp-line); +} +.external-wizard-head .lp-mark { + width: calc(var(--lp-r-sm) * 5.5); + height: calc(var(--lp-r-sm) * 5.5); +} +.external-wizard-head h2 { + margin: 0; + font-size: var(--lp-fs-md); + font-weight: 600; + color: var(--lp-ink); +} +.external-wizard-head h2:focus-visible { + outline: calc(var(--lp-r-sm) / 2) solid var(--lp-blue-deep); + outline-offset: var(--lp-r-sm); +} +.external-count { margin-left: auto; font-size: var(--lp-fs-xs); color: var(--lp-muted); } +.external-wizard-body { padding: calc(var(--lp-r-sm) * 5); min-width: 0; } +.external-wizard-foot { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: calc(var(--lp-r-sm) * 2); + padding: calc(var(--lp-r-sm) * 3) calc(var(--lp-r-sm) * 5); + border-top: calc(var(--lp-r-sm) / 4) solid var(--lp-line); +} +.external-wizard-foot .external-next { + margin-left: auto; + display: inline-flex; + align-items: center; + gap: calc(var(--lp-r-sm) * 1.5); +} +.external-sub { + margin: 0 0 calc(var(--lp-r-sm) * 3); + font-size: var(--lp-fs-xs); + color: var(--lp-muted); + line-height: var(--lp-lh); +} +.external-steps { + margin: 0; + padding-left: calc(var(--lp-r-sm) * 5); + display: grid; + gap: calc(var(--lp-r-sm) * 3); + font-size: var(--lp-fs-sm); + color: var(--lp-ink); + line-height: var(--lp-lh); +} +.external-steps li { padding-left: var(--lp-r-sm); min-width: 0; } +.external-steps strong { font-weight: 600; } + +.conn-tutorial-fields { display: grid; gap: calc(var(--lp-r-sm) * 4); } +.conn-tutorial-choice { + display: flex; + align-items: center; + gap: calc(var(--lp-r-sm) * 2); + font-size: var(--lp-fs-xs); + color: var(--lp-ink); +} +.conn-tutorial-choice input { accent-color: var(--lp-primary); } +.conn-tutorial-choice input:disabled { opacity: 0.5; cursor: not-allowed; } +.conn-tutorial-choice input:focus-visible { + outline: calc(var(--lp-r-sm) / 2) solid var(--lp-blue-deep); + outline-offset: var(--lp-r-sm); +} +.conn-tutorial-field { display: grid; gap: var(--lp-r-sm); } +.conn-tutorial-field label { font-size: var(--lp-fs-xs); font-weight: 500; color: var(--lp-ink); } +.conn-tutorial-field input, +.conn-tutorial-field select { + box-sizing: border-box; + width: 100%; + min-width: 0; + height: calc(var(--lp-r-sm) * 8); + padding: 0 calc(var(--lp-r-sm) * 2); + border: calc(var(--lp-r-sm) / 4) solid var(--lp-line); + border-radius: var(--lp-r-sm); + background: var(--lp-surface); + font: inherit; + font-size: var(--lp-fs-xs); + color: var(--lp-ink); +} +.conn-tutorial-field input:focus-visible, +.conn-tutorial-field select:focus-visible, +.conn-tutorial-button:focus-visible { + outline: calc(var(--lp-r-sm) / 2) solid var(--lp-blue-deep); + outline-offset: var(--lp-r-sm); +} +.conn-tutorial-button { + appearance: none; + border: 0; + background: transparent; + font: inherit; + color: inherit; + display: inline-flex; + align-items: center; + justify-content: center; + min-height: calc(var(--lp-r-sm) * 8); + padding: 0 calc(var(--lp-r-sm) * 3); + border-radius: var(--lp-r-sm); + box-shadow: inset 0 0 0 calc(var(--lp-r-sm) / 4) var(--lp-line); + font-size: var(--lp-fs-xs); + font-weight: 600; + text-decoration: none; + color: var(--lp-ink); + cursor: pointer; +} +.conn-tutorial-button.is-primary { + background: var(--lp-primary); + color: var(--lp-surface); + box-shadow: none; +} +.conn-tutorial-button:disabled, +.conn-tutorial-field input:disabled, +.conn-tutorial-field select:disabled { opacity: 0.5; cursor: not-allowed; } +.conn-tutorial-field .external-sub { margin: 0; } +.conn-tutorial-hint { margin-top: calc(var(--lp-r-sm) * 4); } +.conn-tutorial .external-steps + .conn-tutorial-button { margin-top: calc(var(--lp-r-sm) * 4); } +.conn-tutorial-error { + margin-top: calc(var(--lp-r-sm) * 4); + color: var(--lp-red-deep); + font-size: var(--lp-fs-xs); + line-height: var(--lp-lh); +} +.conn-tutorial-summary { margin: 0 0 calc(var(--lp-r-sm) * 3); font-size: var(--lp-fs-sm); } + +.conn-directory .conn-card-open[data-action="connect"] { + background: var(--lp-primary); + color: var(--lp-surface); + font-weight: 700; +} + +.conn-directory .conn-card-open:disabled, +.conn-setup-action:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.conn-directory .conn-card-open:focus-visible, +.conn-setup-action:focus-visible { + outline: calc(var(--lp-r-sm) / 2) solid var(--lp-blue-deep); + outline-offset: calc(var(--lp-r-sm) / -2); +} + +.conn-settings-icon { + width: var(--lp-fs-xs); + height: var(--lp-fs-xs); + flex: none; +} + +.conn-contact-mark { + width: calc(var(--lp-r-sm) * 6); + height: calc(var(--lp-r-sm) * 6); + color: var(--lp-green-deep); +} + +.conn-contact-mark-detail { + fill: var(--lp-surface); +} + +.conn-directory .conn-card-status { + font-size: var(--lp-fs-2xs); + color: var(--lp-yellow-deep); +} + +.conn-setup .conn-modal-name { + margin: 0; +} + +.conn-setup .conn-modal-close svg { + width: var(--lp-fs-md); + height: var(--lp-fs-md); +} + +.conn-setup .set-card, +.conn-setup .external-wizard { + border: 0; + box-shadow: none; +} + +.conn-setup .set-pane { + padding: 0; +} + +.conn-setup-action { + display: inline-flex; + padding: calc(var(--lp-r-sm) * 2) calc(var(--lp-r-sm) * 3); + border-radius: var(--lp-r-md); + font-size: var(--lp-fs-xs); + font-weight: 600; + color: var(--lp-primary); + text-decoration: none; + cursor: pointer; +} + +@media (hover: hover) and (pointer: fine) { + .conn-directory .conn-card:has(.conn-card-open:enabled):hover { + transform: translateY(calc(var(--lp-r-sm) / -2)); + } + .conn-directory .conn-card-open:enabled:hover, + .conn-setup-action:hover { + background: var(--lp-wash); + color: var(--lp-primary); + } + .conn-directory .conn-card-open[data-action="connect"]:enabled:hover { + background: var(--lp-primary-hover); + color: var(--lp-surface); + } + .conn-tutorial-button:enabled:hover { background: var(--lp-wash); } + .conn-tutorial-button.is-primary:enabled:hover { background: var(--lp-primary-hover); } +} + +@media (prefers-reduced-motion: reduce) { + .conn-directory .conn-card { + transition: none; + transform: none; + } + .conn-directory .conn-card:has(.conn-card-open:enabled):hover { + transform: none; + } + .external-wizard-progress > div { transition: none; } +} diff --git a/web/src/customer-grid/CustomerGrid.tsx b/web/src/customer-grid/CustomerGrid.tsx index 5979e5599749c4e81b944bd439fb19ad2a26bffc..4ee20bc69f7220027c08ad84ad96c6b3b875676f 100644 --- a/web/src/customer-grid/CustomerGrid.tsx +++ b/web/src/customer-grid/CustomerGrid.tsx @@ -7,7 +7,7 @@ import { useRef, useState, } from "react"; -import type { ReactNode } from "react"; +import type { CSSProperties, ReactNode } from "react"; import { DataEditor, GridCellKind } from "@glideapps/glide-data-grid"; import type { CellClickedEventArgs, @@ -42,8 +42,8 @@ import { GridPager, pageLabel } from "./GridPager"; // lands, which is the done-when's own observable rather than a workaround. import type { AggregateResult } from "./apiBridge"; import { - EMPTY_WINDOW_PREDICATE, limitSummary, lockedRecordsNote, - windowedCapabilityNote, windowedFoldNote, windowPredicateKey, + EMPTY_WINDOW_PREDICATE, importantCountNote, limitSummary, lockedRecordsNote, + pagingSummary, serverViewCount, windowedCapabilityNote, windowedFoldNote, windowPredicateKey, } from "./counts"; // ⭐ W35-T29/T30 (C5) — the record star's client, and its own stylesheet imported HERE, by the // module that renders its classes. W35-T22 in this same wave was the cost of doing otherwise. @@ -132,7 +132,7 @@ import { bindHostEvent, eventId } from "./hostBridge"; // list is the only answer; the rail's alert door is a one-way create. import { fetchAlerts } from "../alerts/alertsApi"; import { API_V1, CREDENTIALS, NAV_MINIMIZE_EVENT, ROWS_STALE_EVENT, TOAST_EVENT, VIEW_OPEN_EVENT, - WORKSPACE_STALE_EVENT, signal } + WORKSPACE_STALE_EVENT, UNAUTHORIZED_EVENT, checkTenant, signal } from "../apiContract"; import type { ViewOpenDetail } from "../apiContract"; import { addTableField, addTableRow, clearTopicRowsCache, deleteTableField, @@ -198,7 +198,7 @@ import { avatarInitials, formatDisplay, imageCellRenderer, matchChoices, pickerT ratingCellRenderer, setAvatarRepaint, userCellRenderer } from "./cells"; // ⭐ W50-T05 — ONE numeric reading of a stored cell, the same one `aggregations.ts` folds with. // A second parse here is how a cell painted blank while the total under it counted 1,234.50. -import { numericOrUndefined } from "./display"; +import { actionHref, numericOrUndefined } from "./display"; // ⭐ W50-T05 — the value/share COMPOSITION. The share arithmetic stays in `formatShare`. import { withShare } from "../ui/fmt"; import { optionTint, pickTint } from "./choiceColors"; @@ -1354,6 +1354,303 @@ const SHOW_AS_LINE_GAP = 7; * this protocol cannot drift, and so a search for it finds both ends. */ const SHOW_AS_BREAK = "\n"; +// S70-T12: mirror modes keep the predicate on the server. Group keys stay JSON scalars, +// including null; the general aggregate parser turns them into strings for canvas headers. +const WINDOWED_VIEW_MODES: readonly DisplayMode[] = [ + "grid", "list", "gallery", "kanban", "calendar", "chart", "map", +]; +type ModeGroup = { key: string | number | null; label: string; count: number; + aggregates: Record }; +type ModeGroups = { groups: ModeGroup[]; totalGroups: number; totalRows: number; + aggregates: Record }; +type ModeAgg = { colId: string; agg: string; p?: number }; + +async function mirrorModeGroups(table: string, req: RowWindowRequest, by: string, + aggs: ModeAgg[], offset: number, size: number, abort: AbortSignal): Promise { + const response = await fetch(`${API_V1}/odoo-tables/${encodeURIComponent(table)}/aggregates`, { + method: "POST", credentials: CREDENTIALS, signal: abort, + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ filters: req.filters ?? [], filterConj: req.filterConj ?? "and", + search: req.search ?? "", related: req.related, ids: req.ids, + groupBy: by, groupOffset: offset, groupLimit: size, aggs }), + }); + if (checkTenant(response)) throw new Error("Your workspace changed. Reloading."); + if (response.status === 401) { signal(UNAUTHORIZED_EVENT); throw new Error("Sign in to read this window."); } + const body = await response.json().catch(() => null); + if (!response.ok) throw new Error(refusalMessage(body, response.status)); + if (!body || !Array.isArray(body.groups) || typeof body.total_groups !== "number" + || typeof body.total_rows !== "number") + throw new Error("Groups could not be read for this window."); + // A dropped group, range or subtotal would describe a different population. Refuse the + // mode in words, instead of painting a plausible partial answer or a page-derived total. + if (Array.isArray(body.limits) && body.limits.length) { + const cause = body.limits.map((limit: { cause?: string }) => limit.cause).filter(Boolean).join(". "); + throw new Error(`This window could not be grouped${cause ? `: ${cause}` : "."}`); + } + return { groups: body.groups, totalGroups: body.total_groups, totalRows: body.total_rows, + aggregates: body.aggregates ?? {} }; +} + +// Count-only reads use the existing permission-scoped aggregate door. Unlike the summary +// bridge, an empty aggs list is intentional and a malformed total never defaults to zero. +async function readImportantViewCount(table: string, view: SavedView, + related: RelatedPin | null, abort: AbortSignal): Promise { + try { + const response = await fetch(`${API_V1}/odoo-tables/${encodeURIComponent(table)}/aggregates`, { + method: "POST", credentials: CREDENTIALS, signal: abort, + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ filters: view.config.filters ?? [], + filterConj: view.config.filterConj ?? "and", search: "", related, aggs: [] }), + }); + if (checkTenant(response)) return null; + if (response.status === 401) { signal(UNAUTHORIZED_EVENT); return null; } + if (!response.ok) return null; + return serverViewCount(await response.json().catch(() => null)); + } catch { + return null; + } +} + +function MirrorModeSkeleton() { + return
+ + + +
; +} + +function MirrorModeFailure({ message, retry }: { message: string; retry: () => void }) { + const plain = message.replace(/[\u2013\u2014]/g, ","); + const sentence = plain.split(/\.\s/)[0]; + return
{sentence}{" "} + +
; +} + +// Small group/day windows have the same buttons as a grid footer. Previous/Next remain +// disabled while loading; there is no Show all door or bound on the reachable population. +function MirrorWindowPager({ offset, size, total, pending, label, onPage }: { + offset: number; size: number; total: number; pending: boolean; label: string; + onPage: (offset: number) => void; +}) { + return ; +} + +function useMirrorGroups(table: string, request: RowWindowRequest, field: Field, + aggs: ModeAgg[], offset: number, size: number, active = true) { + const key = JSON.stringify([table, request, field.key, aggs, offset, size, active]); + const [answer, setAnswer] = useState<{ key: string; data?: ModeGroups; error?: string }>({ key: "" }); + const [retry, setRetry] = useState(0); + useEffect(() => { + const abort = new AbortController(); + let live = true; + setAnswer({ key }); + if (!active) return () => { live = false; abort.abort(); }; + void mirrorModeGroups(table, request, field.key, aggs, offset, size, abort.signal).then( + (data) => { if (live) setAnswer({ key, data }); }, + (error: unknown) => { if (live) setAnswer({ key, error: error instanceof Error + ? error.message : "Groups could not be read for this window." }); }); + return () => { live = false; abort.abort(); }; + // The serialized key is the complete request; equal objects do not restart a fetch. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [key, retry]); + return { data: answer.key === key ? answer.data : undefined, + error: answer.key === key ? answer.error : undefined, retry: () => setRetry((n) => n + 1) }; +} + +function MirrorRecordWindow({ table, request, field, group, titleKey, fieldByKey, + cardKeys, size, calendar = false, onOpen }: { + table: string; request: RowWindowRequest; field: Field; group: ModeGroup; + titleKey: string; fieldByKey: Map; cardKeys: string[]; size: number; + calendar?: boolean; onOpen: (record: Row) => void; +}) { + const [offset, setOffset] = useState(0); + const [retry, setRetry] = useState(0); + const key = JSON.stringify([table, request, field.key, group.key, offset, size]); + const [page, setPage] = useState<{ key: string; rows?: Row[]; total?: number; error?: string }>({ key: "" }); + const seq = useRef(0); + useEffect(() => { + const current = ++seq.current; + let live = true; + setPage({ key }); + let failure = ""; + void fetchTableWindow(table, { ...request, offset, limit: size, + group: { by: field.key, key: group.key } }, (reason) => { failure = reason; }) + .then((result) => { + if (!live || seq.current !== current) return; + if (!result) { setPage({ key, error: failure || "These records could not be loaded." }); return; } + // A deep OFFSET may be slow but remains reachable. Only a changed predicate or + // unsupported projection refuses; informational latency never becomes a row cap. + const limit = limitSummary(result.limits?.filter((entry) => entry.effect !== "slow" + && entry.effect !== "none"), result.counts); + if (limit) { setPage({ key, error: `This window could not be read: ${limit.full}` }); return; } + const total = result.counts?.matched ?? group.count; + if (offset >= total && offset > 0) { setOffset(Math.max(0, Math.ceil(total / size) - 1) * size); return; } + setPage({ key, rows: result.rows, total }); + }, () => { if (live && seq.current === current) + setPage({ key, error: "These records could not be loaded." }); }); + return () => { live = false; }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [key, retry]); + const settled = page.key === key; + const rows = settled ? page.rows : undefined; + const error = settled ? page.error : undefined; + const pending = !rows && !error; + const open = (row: Row) => { signal(NAV_MINIMIZE_EVENT); onOpen(row); }; + return <> +
+ {pending ? : error + ? setRetry((n) => n + 1)} /> + : rows?.length === 0 ?
No records
+ : rows?.map((row) => calendar + ? + :
open(row)} onKeyDown={(event) => { + if (event.key === "Enter" || event.key === " ") { event.preventDefault(); if (!event.repeat) open(row); } + }}> + {String(row[titleKey] ?? "")} + {cardKeys.map((key) => { + const f = fieldByKey.get(key); + if (!f) return null; + const text = formatDisplay(f, row[key]); + if (!text) return null; + const href = actionHref(f, row[key]); + const tint = f.type === "select" || f.type === "status" ? optionTint(f, text) : undefined; + return ; + })} +
)} +
+ {(page.total ?? group.count) > size && } + ; +} + +function MirrorKanban({ table, request, field, fieldByKey, cardKeys, titleKey, size, + clamped, aggs, summaryMap, onOpen, onAddOption }: { + table: string; request: RowWindowRequest; field: Field; fieldByKey: Map; + cardKeys: string[]; titleKey: string; size: number; clamped: boolean; aggs: ModeAgg[]; + summaryMap: SummaryMap; + onOpen: (record: Row) => void; onAddOption: ((anchor: AnchorRect) => void) | null; +}) { + const [offset, setOffset] = useState(0); + const columnsPerPage = 12; + const { data, error, retry } = useMirrorGroups(table, request, field, aggs, offset, columnsPerPage); + useEffect(() => { + if (data && offset >= data.totalGroups && offset > 0) + setOffset(Math.max(0, Math.ceil(data.totalGroups / columnsPerPage) - 1) * columnsPerPage); + }, [data, offset]); + return
+
Cards are read-only.
+ {error ? : !data ? + : data.groups.length === 0 ?
No records match this view.
+ :
{data.groups.map((group) => { + const tint = typeof group.key === "string" && group.key ? optionTint(field, group.key) : undefined; + return
+
{group.label || "(empty)"} + {group.count.toLocaleString()}
+ {aggs.map((agg) => { + const f = fieldByKey.get(agg.colId); + const value = group.aggregates[agg.colId]; + return f && value != null ?
+ {f.label} + {formatServerAgg(f, resolveSummary(f, summaryMap), value, data.aggregates[agg.colId])} +
: null; + })} + +
; + })}{onAddOption &&
}
} + {data && data.totalGroups > columnsPerPage && } +
; +} + +function MirrorCalendar({ table, request, field, fieldByKey, titleKey, today, size, + summary, metrics, onOpen }: { + table: string; request: RowWindowRequest; field: Field; fieldByKey: Map; + titleKey: string; today?: string; size: number; summary: boolean; + metrics?: { id: string; field: string; agg: string }[]; onOpen: (record: Row) => void; +}) { + const [pickedMonth, setMonth] = useState(""); + const month = pickedMonth || (today && /^\d{4}-\d{2}-\d{2}/.test(today) ? today.slice(0, 7) : ""); + const year = Number(month.slice(0, 4)), m = Number(month.slice(5, 7)); + const days = month ? new Date(Date.UTC(year, m, 0)).getUTCDate() : 0; + const range: RowWindowRequest = { ...request, filterConj: "and", filters: [ + ...(request.filters?.length ? [{ conj: request.filterConj === "or" ? "or" as const : "and" as const, + children: request.filters }] : []), + { colId: field.key, op: "between", value: `${month}-01`, value2: `${month}-${String(days).padStart(2, "0")}` }, + ] }; + const aggs = summary ? (metrics ?? []).map((metric) => ({ colId: metric.field, + agg: metric.agg === "avg" ? "average" : metric.agg })) : []; + const { data, error, retry } = useMirrorGroups(table, range, field, aggs, 0, 42, !!month); + const shift = (delta: number) => setMonth(new Date(Date.UTC(year, m - 1 + delta, 1)).toISOString().slice(0, 7)); + const leading = month ? (new Date(Date.UTC(year, m - 1, 1)).getUTCDay() + 6) % 7 : 0; + const cells = Array.from({ length: Math.ceil((leading + days) / 7) * 7 }, (_, i) => + i >= leading && i < leading + days ? i - leading + 1 : null); + const groups = new Map(data?.groups.map((group) => [String(group.key), group])); + const calendarError = error || (data && (data.totalGroups !== data.groups.length + || data.groups.some((group) => !new RegExp(`^${month}-\\d{2}$`).test(String(group.key)))) + ? "This window could not be grouped by day." : undefined); + if (!month) return
Calendar needs a date anchor.
; + return
+
+ + {new Date(Date.UTC(year, m - 1, 1)).toLocaleDateString(undefined, { month: "long", year: "numeric", timeZone: "UTC" })} + + {today && } + {data && {data.totalRows.toLocaleString()} records} +
+ {calendarError ? : !data ? + : <>{data.totalRows === 0 &&
No records this month.
} +
{["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].map((day) => +
{day}
)} + {cells.map((day, index) => { + const iso = day ? `${month}-${String(day).padStart(2, "0")}` : ""; + const group = groups.get(iso); + return
{day}
+ {group && <>{group.count.toLocaleString()} records + {summary ? (metrics ?? []).map((metric) => { + const f = fieldByKey.get(metric.field), value = group.aggregates[metric.field]; + return f ?
{f.label}{" "} + {value == null ? "Unavailable" : formatServerAgg(f, + { agg: (metric.agg === "avg" ? "average" : metric.agg) as SummaryChoice["agg"] }, value)}
: null; + }) : } + } +
; + })} +
} +
; +} + /** The session key a view's own fold is remembered under. ⛔ EXACT SHAPE, named by the ticket: * `cg-fold::`. `tableKey` is the surface scope (`customer`, `product`, * `ut_*`), the same identity `useGridSelection` keys its own per-surface memory on. */ @@ -1773,6 +2070,8 @@ interface CustomerGridProps { /** Exact linked pids to page on the server. */ embeddedRecordIds?: readonly number[]; embeddedInitialPage?: RecordIdInitialPage; + /** A fresh drill is resolving ids; withhold the previous id page during that request. */ + embeddedIdsPending?: boolean; onEmbeddedTotalChange?: (total: number | null) => void; /** Selection mode used by an editable ordinary-Link modal. */ embeddedSelectable?: boolean; @@ -1869,6 +2168,7 @@ interface LinkGridModalProps { table: SurfaceScope; recordIds: readonly number[]; initialPage?: RecordIdInitialPage; + idsPending?: boolean; editable?: boolean; single?: boolean; excludeRecordId?: number; @@ -1897,7 +2197,7 @@ interface LinkGridModalProps { // chrome slots and a `cellView` modifier while leaving existing callers and the focus trap intact. export function LinkGridModal({ label, table, recordIds, editable = false, single = false, excludeRecordId, cellView = false, cellFilter, cellFields, onCellFilterChange, onCellDisplayChange, - headerActions, footer, initialPage, + headerActions, footer, initialPage, idsPending = false, onOpenRecord, onSave, onClose }: LinkGridModalProps) { const panelRef = useRef(null); const [selectedIds, setSelectedIds] = useState(() => [...recordIds]); @@ -1906,7 +2206,7 @@ export function LinkGridModal({ label, table, recordIds, editable = false, singl const reportTotal = useCallback((total: number | null) => { setServerCount({ key: countKey, total }); }, [countKey]); - const recordCount = editable ? selectedIds.length + const recordCount = idsPending ? null : editable ? selectedIds.length : serverCount?.key === countKey ? serverCount.total : null; const changeSelectedIds = useCallback((ids: number[]) => { const next = single ? ids.slice(-1) : ids; @@ -1917,7 +2217,7 @@ export function LinkGridModal({ label, table, recordIds, editable = false, singl useOverlayLayer({ panelRef, onDismiss: onClose, - dismissOnOutside: true, + dismissOnOutside: false, initialFocus: "[data-overlay-autofocus]", trapFocus: true, }); @@ -1966,6 +2266,7 @@ export function LinkGridModal({ label, table, recordIds, editable = false, singl onCellDisplayChange={onCellDisplayChange} embeddedRecordIds={editable ? undefined : recordIds} embeddedInitialPage={editable ? undefined : initialPage} + embeddedIdsPending={!editable && idsPending} onEmbeddedTotalChange={editable ? undefined : reportTotal} embeddedExcludeRecordId={editable ? excludeRecordId : undefined} embeddedSelectable={editable} @@ -2098,9 +2399,9 @@ function CustomerGrid(props: CustomerGridProps = {}) { /** An ID-backed grid supplies pages through the existing caller-owned payload seam. */ function useRecordIdWindow(scope: SurfaceScope, ids: readonly number[] | undefined, - related: RelatedPin | null, initialPage?: RecordIdInitialPage) { - const key = JSON.stringify([scope, ids, related, initialPage]); - const seed = ids !== undefined && !related && initialPage && initialPage.offset === 0 + related: RelatedPin | null, initialPage?: RecordIdInitialPage, idsPending = false) { + const key = JSON.stringify([scope, ids, related, initialPage, idsPending]); + const seed = !idsPending && ids !== undefined && !related && initialPage && initialPage.offset === 0 ? initialPage.payload : null; const currentKey = useRef(key); const sequence = useRef(0); @@ -2114,7 +2415,7 @@ function useRecordIdWindow(scope: SurfaceScope, ids: readonly number[] | undefin key: string; payload: CustomersPayload | null; pending: boolean; } | null>(null); const requestWindow = useCallback((req: RowWindowRequest, fresh = false) => { - if (ids === undefined) return; + if (idsPending || ids === undefined) return; const seq = ++sequence.current; lastRequest.current = req; if (!fresh && seed && initialPage && req.offset === 0 @@ -2133,7 +2434,7 @@ function useRecordIdWindow(scope: SurfaceScope, ids: readonly number[] | undefin if (seq !== sequence.current || currentKey.current !== key) return; setPage({ key, payload, pending: false }); }); - }, [scope, ids, related, key, seed, initialPage]); + }, [scope, ids, related, key, seed, initialPage, idsPending]); useEffect(() => { const refresh = () => { if (lastRequest.current) requestWindow(lastRequest.current, true); }; // React can replay effect setup after cancelling its first request in StrictMode. @@ -2145,8 +2446,8 @@ function useRecordIdWindow(scope: SurfaceScope, ids: readonly number[] | undefin }; }, [requestWindow]); return { - payload: page?.key === key ? page.payload : seed, - pending: page?.key === key ? page.pending : !seed && ids !== undefined, + payload: idsPending ? null : page?.key === key ? page.payload : seed, + pending: idsPending || (page?.key === key ? page.pending : !seed && ids !== undefined), requestWindow, }; } @@ -2169,6 +2470,7 @@ function CustomerGridSurface({ embeddedPayload, embeddedRecordIds, embeddedInitialPage, + embeddedIdsPending = false, onEmbeddedTotalChange, embeddedSelectable = false, embeddedSelectedIds = [], @@ -2215,7 +2517,7 @@ function CustomerGridSurface({ const embeddedIdsKey = recordIdsKey(embeddedPayload === undefined ? embeddedRecordIds : undefined); const embeddedIds = useMemo( () => embeddedIdsKey ? JSON.parse(embeddedIdsKey) as number[] : undefined, [embeddedIdsKey]); - const idWindow = useRecordIdWindow(scope, embeddedIds, relatedPin, embeddedInitialPage); + const idWindow = useRecordIdWindow(scope, embeddedIds, relatedPin, embeddedInitialPage, embeddedIdsPending); /** * ⭐⭐ W35-T23 (owner item 4 / R2) — **`previewReadOnly` IS GONE, AND THE SPLIT IS THE TICKET.** * @@ -2249,6 +2551,9 @@ function CustomerGridSurface({ payload, loading, windowPending: ordinaryWindowPending, + workspacePending, + workspaceError, + retryWorkspace, overlayEdits, setOverlayEdits, patchOverlay, @@ -2600,6 +2905,7 @@ function CustomerGridSurface({ { pid: number; x: number; y: number; size: number } | null >(null); const [detailPid, setDetailPid] = useState(null); + const [mirrorDetail, setMirrorDetail] = useState<{ key: string; record: Row } | null>(null); const [columnMenu, setColumnMenu] = useState(null); const [columnDelete, setColumnDelete] = useState<{ field: Field; confirm: () => void } | null>(null); /** @@ -2881,6 +3187,15 @@ function CustomerGridSurface({ return; } const local = embedded ? null : readLocal(storageKey); + if (local && hostWorkspace && (!payload.workspace || workspaceError)) { + // The parsed browser copy is a presentation draft, never an authority for revoked columns. + local.fields = []; + local.views = []; + local.fieldStamps = {}; + local.viewWrites = {}; + local.activeViewId = ALL_VIEW_ID; + local.activeViewAt = undefined; + } // Item 3c: the def half of the no-blip layer. A RECENT local stamp beats a // lagged host echo (rename survives, retype holds, a delete stays deleted); // a caught-up echo returns host objects byte-identical (see optimism.ts). @@ -3044,7 +3359,7 @@ function CustomerGridSurface({ reemittedRef.current = { ...(local?.reemitted ?? {}) }; } }, [payload, payloadFields, storageKey, hostWorkspace, scope, isQueryPreview, queryBinding, isApplicationSurface, - cellView, cellFilterKey, cellFieldsKey, personalLayout.ready, personalLayout.layout, emitHostEvent]); + cellView, cellFilterKey, cellFieldsKey, personalLayout.ready, personalLayout.layout, emitHostEvent, workspaceError]); // Initialization runs once per scope. Keep the columns current after a cell filter settles, // retaining hidden keys in the order so the reader can show them again. @@ -3174,6 +3489,18 @@ function CustomerGridSurface({ // `hostWorkspace`: this adopts views out of `payload.workspace`, which a Query surface never // fetches. Keyed on `embedded` it would read a workspace that is not there. if (!workspaceReady || !hostWorkspace) return; + if (workspaceError) { + // Metadata is unavailable, so the successful row response owns both columns and cells. + // Recent local stamps cannot restore a field that this response no longer offers. + if (fields !== payloadFields) setFields(payloadFields); + if (views.length !== 1 || views[0].id !== ALL_VIEW_ID) { + setViews([allRecordsView(payloadFields, scope)]); + setActiveViewId(ALL_VIEW_ID); + } + const safeConfig = normalizeConfig(configRef.current, payloadFields); + if (!sameConfig(safeConfig, configRef.current)) setConfig(safeConfig); + return; + } const now = Date.now(); /** * ⭐⭐ W36-T02 (owner item 1) — CORRECT, THEN ADOPT. The two steps answer two questions and @@ -3229,7 +3556,7 @@ function CustomerGridSurface({ // their input by reference and `nextViews === views`, so the next pass sets nothing and the // effect stops. That is why `views` can be a dependency at all. }, [workspaceReady, hostWorkspaceViews, payloadFields, fields, views, activeViewId, - hostWorkspace]); + hostWorkspace, workspaceError, scope]); /** Item 3c — stamp a def write / a delete. Pruned at every touch so the persisted blob * stays a recent window, never an archive. */ @@ -3715,9 +4042,8 @@ function CustomerGridSurface({ const serverWindowed = mode === "server-windowed"; // Owner items 4+6 — the Cohort page's grid renders without the Views sidebar. const hideViews = (!hostWorkspace && !related) || payload?.workspace?.hideViews === true; - // Wave-6 item 10 — how this view displays. A WINDOWED table is always the grid: list/ - // calendar/kanban compute over the whole matched set, and one page is not it (CG-3's rule, - // the same reason grouping is off there). + // S70-T12 / R16: card modes on a mirror ask for server groups/ranges. Embedded and query + // surfaces retain their original mode withholding; unsupported saved modes say why. const displaySpec = resolvedDisplaySpec; const [routePanelOpen, setRoutePanelOpen] = useState(false); /** @@ -3728,7 +4054,8 @@ function CustomerGridSurface({ const appliedApplicationPath = useRef(""); const [applicationAddress, setApplicationAddress] = useState<{ key: string; ok: boolean }>({ key: "", ok: false }); - const displayMode: DisplayMode = serverWindowed ? "grid" : routeMapMode ? "map" : displaySpec?.mode ?? "grid"; + const displayMode: DisplayMode = serverWindowed && (embedded || isQueryPreview) + ? "grid" : routeMapMode ? "map" : displaySpec?.mode ?? "grid"; const addressedApplicationKey = dataPath?.application && dataPath?.database ? `${dataPath.database}::${dataPath.application}` : ""; @@ -3738,7 +4065,7 @@ function CustomerGridSurface({ applicationAddress.key !== addressedApplicationKey || (applicationAddress.ok && !resolvedApplicationMatches) ); - const routeApplicationSurface = isApplicationSurface && displayMode === "map"; + const routeApplicationSurface = isApplicationSurface && !serverWindowed && displayMode === "map"; /** * ⭐⭐ D-673 / W54-T21 — THE TOP RUNG OF THE MAP'S ESCAPE LADDER, AND IT HAS TO BE HERE. * @@ -3816,6 +4143,25 @@ function CustomerGridSurface({ }), [config.filters, config.filterConj, config.sorts, search, pageSize, embeddedIds] ); + const mirrorModeRequest = useMemo(() => ({ ...windowRequest(0), related: relatedPin }), + [windowRequest, relatedPin]); + const [mirrorRevision, setMirrorRevision] = useState(0); + useEffect(() => { + const refresh = () => { + if (!serverWindowed) return; + dropTableRowsCache(scope); + setMirrorRevision((revision) => revision + 1); + }; + window.addEventListener(ROWS_STALE_EVENT, refresh); + window.addEventListener(WORKSPACE_STALE_EVENT, refresh); + return () => { + window.removeEventListener(ROWS_STALE_EVENT, refresh); + window.removeEventListener(WORKSPACE_STALE_EVENT, refresh); + }; + }, [serverWindowed, scope]); + const mirrorDetailKey = JSON.stringify([scope, activeViewId, windowPredicate, relatedPinKey, displayMode, + fields.map((field) => field.key), mirrorRevision]); + const mirrorDetailRecord = mirrorDetail?.key === mirrorDetailKey ? mirrorDetail.record : null; /* ⭐⭐ HOTFIX 2026-09-23 — THE PAGE. The owner revoked the virtualise-only ruling: *"I want fucking either pagination or next page and only load 50 at a time"*. A read-through grid now holds ONE page of `WINDOW_ROWS` rows at `pageOffset`, moved by the pager under the canvas. @@ -4263,7 +4609,7 @@ function CustomerGridSurface({ * loaded window with `foldNote` naming its scope — W30-T42's second honest state, kept for * exactly as long as the first one is unavailable. Absent is still not one of the two. */ - const [serverAggs, setServerAggs] = useState(null); + const [serverAggAnswer, setServerAggAnswer] = useState<{ key: string; value: AggregateResult | null } | null>(null); const summaryRequest = useMemo( () => visibleCols.flatMap((col) => { const field = fieldByKey.get(String(col.id)); @@ -4289,11 +4635,14 @@ function CustomerGridSurface({ [visibleCols, fieldByKey, summaryMap] ); const summaryRequestKey = JSON.stringify(summaryRequest); + const serverAggRequestKey = JSON.stringify([topic.key, summaryRequestKey, windowPredicate, + config.groupBy, relatedPinKey, embeddedIdsKey, mirrorRevision]); + const serverAggs = serverAggAnswer?.key === serverAggRequestKey ? serverAggAnswer.value : null; useEffect(() => { // HOTFIX 2026-09-23: a grouped windowed table needs its groups even when no column carries // a summary, because the groups ARE the canvas now (per-group paging). if (!serverWindowed || ((!summaryRequestKey || summaryRequestKey === "[]") && !config.groupBy)) { - setServerAggs(null); + setServerAggAnswer(null); return; } let live = true; @@ -4316,7 +4665,7 @@ function CustomerGridSurface({ ...(config.groupBy ? { groupBy: config.groupBy } : {}), aggs: JSON.parse(summaryRequestKey || "[]") as { colId: string; agg: string; p?: number }[], }).then((result) => { - if (live) setServerAggs(result); + if (live) setServerAggAnswer({ key: serverAggRequestKey, value: result }); }); return () => { live = false; }; // ⚠ KEYED ON THE SERIALISED REQUEST, NOT ON THE ARRAY. `summaryRequest` rebuilds on every @@ -4331,7 +4680,8 @@ function CustomerGridSurface({ // ⚠ W51-T06 — `relatedPin` JOINS THE DEPS for the same reason `config.groupBy` did: the pin // is part of the question, so moving between two tabs on one target table must re-ask. Its // identity is stable (see `relatedPinKey`), so this cannot loop. - }, [serverWindowed, topic.key, summaryRequestKey, windowPredicate, config.groupBy, relatedPin, embeddedIds]); + }, [serverWindowed, topic.key, summaryRequestKey, windowPredicate, config.groupBy, relatedPin, embeddedIds, + serverAggRequestKey]); /** * What the bar actually paints: C1's numbers when the server answered, this browser's fold * over the loaded window otherwise — and the two are never mixed within one paint, because a @@ -4459,14 +4809,18 @@ function CustomerGridSurface({ ReadonlyMap >(() => new Map()); const groupPageSeq = useRef(0); + const groupPageRequests = useRef(new Map()); useEffect(() => { groupPageSeq.current += 1; + groupPageRequests.current.clear(); setGroupPages(new Map()); - }, [serverWindowed, windowPredicate, config.groupBy, relatedPin]); + }, [serverWindowed, windowPredicate, config.groupBy, relatedPin, pageSize, mirrorRevision]); const loadGroupPage = useCallback((key: string, offset: number) => { const by = config.groupBy; if (!by || !groupKeyRaw.has(key)) return; const seq = groupPageSeq.current; + const request = (groupPageRequests.current.get(key) ?? 0) + 1; + groupPageRequests.current.set(key, request); setGroupPages((prev) => { const next = new Map(prev); next.set(key, { offset, rows: prev.get(key)?.rows ?? [], pending: true }); @@ -4483,7 +4837,7 @@ function CustomerGridSurface({ }, (reason) => { failure = reason; }).then((page) => page, () => null).then((page) => { // HOTFIX 2026-09-28: a REJECTED fetch settles too. Without the rejection handler above a // network failure left `pending` true forever and the group's pager stayed disabled. - if (seq !== groupPageSeq.current) return; + if (seq !== groupPageSeq.current || groupPageRequests.current.get(key) !== request) return; setGroupPages((prev) => { const cur = prev.get(key); if (cur && cur.offset !== offset) return prev; @@ -4584,6 +4938,8 @@ function CustomerGridSurface({ () => limitSummary(payload?.limits, payload?.counts), [payload?.limits, payload?.counts] ); + // Shared by every display mode, including modes whose page controls live inside groups. + const pagingNote = useMemo(() => pagingSummary(payload?.counts), [payload?.counts]); /** * ⛔ AND THE LIMIT NOBODY DECLARED, WHICH IS THE ONE A PERSON ACTUALLY NOTICES. Nine controls * this component gates on `serverWindowed` vanish the moment a grid becomes a window — export, @@ -5340,8 +5696,11 @@ function CustomerGridSurface({ const rowByPid = useMemo(() => { const map = new Map(); for (const r of rawRows) map.set(r.pid, r); + for (const page of groupPages.values()) if (!page.pending && !page.failed) + for (const record of page.rows) map.set(record.pid, record); + if (mirrorDetailRecord) map.set(mirrorDetailRecord.pid, mirrorDetailRecord); return map; - }, [rawRows]); + }, [rawRows, groupPages, mirrorDetailRecord]); const currentValue = useCallback( (pid: number, key: string): UndoValue => { const edited = overlayEdits[pid]?.[key]; @@ -5370,6 +5729,7 @@ function CustomerGridSurface({ */ const openRecord = useCallback( (pid: number) => { + setMirrorDetail(null); if (!onOpenRecord) { setDetailPid(pid); return; @@ -5380,6 +5740,13 @@ function CustomerGridSurface({ }, [onOpenRecord, rowByPid, overlayEdits, topic.key, fields] ); + const openMirrorRecord = useCallback((record: Row) => { + // The adapter supplies a record fetched through the SAME walled rows door, even when + // it is outside the grid's current page. Never resolve that identity from the page. + if (onOpenRecord) { onOpenRecord(record.pid, topic.key, record, fields); return; } + setMirrorDetail({ key: mirrorDetailKey, record }); + setDetailPid(record.pid); + }, [onOpenRecord, topic.key, fields, mirrorDetailKey]); // C21: an Inbox ticket address waits for its record, then uses the shared record-open door. const addressedRecordRef = useRef({ hash: "", opened: false }); @@ -6350,16 +6717,18 @@ function CustomerGridSurface({ : fullSetMode ? pidToIndex : displayPidToIndex; const detailIndex = detailPid !== null ? detailIdxMap.get(detailPid) : undefined; useEffect(() => { - if (detailPid !== null && detailIndex === undefined) setDetailPid(null); - }, [detailPid, detailIndex]); + if (detailPid !== null && detailIndex === undefined && mirrorDetailRecord?.pid !== detailPid) setDetailPid(null); + }, [detailPid, detailIndex, mirrorDetailRecord]); const detailRecord = useMemo(() => { + if (mirrorDetailRecord && mirrorDetailRecord.pid === detailPid) return overlayEdits[mirrorDetailRecord.pid] + ? { ...mirrorDetailRecord, ...overlayEdits[mirrorDetailRecord.pid] } : mirrorDetailRecord; if (detailIndex === undefined) return null; const row = detailRows[detailIndex]; if (!row || row.kind !== "data") return null; return overlayEdits[row.record.pid] ? { ...row.record, ...overlayEdits[row.record.pid] } : row.record; - }, [detailIndex, detailRows, overlayEdits]); + }, [detailIndex, detailRows, overlayEdits, mirrorDetailRecord, detailPid]); const dataPosition = useMemo(() => { if (detailIndex === undefined) return 0; return detailRows @@ -7160,7 +7529,7 @@ function CustomerGridSurface({ (id: string) => { if (isQueryPreview || isApplicationSurface) return; const current = views.find((view) => view.id === activeViewId); - if (current && !sameConfig(current.config, config)) + if (!workspacePending && current && !sameConfig(current.config, config)) persistView({ ...current, config }); /** * ⭐⭐ W35-T30 (C5) — "Starred records" IS SELECTABLE AND IS NOT IN `views`, DELIBERATELY. @@ -7173,10 +7542,10 @@ function CustomerGridSurface({ * (see `railViews`) and is resolved here by name, which is the same split the comment on * `applyViewOrder` makes for view ORDER: a rendering fact about one surface. */ - const next = views.find((view) => view.id === id) - ?? (id === recordStars.stars.view?.id ? recordStars.stars.view : undefined); + const next = workspacePending ? undefined : (views.find((view) => view.id === id) + ?? (id === recordStars.stars.view?.id ? recordStars.stars.view : undefined)); if (!next) { - if (!workspaceReady) { + if (workspacePending || !workspaceReady) { parkedViewOpenRef.current = { topic: scope, viewId: id }; } else { // Resolve before signalling, including a miss received after initialization. @@ -7233,7 +7602,7 @@ function CustomerGridSurface({ }, [views, activeViewId, config, persistView, fields, isQueryPreview, recordStars.stars.view, dataPath, selectAssignedView, clearAssignedView, isApplicationSurface, emitHostEvent, - workspaceReady, scope, databases, applicationDatabase, adoptFold] + workspaceReady, workspacePending, scope, databases, applicationDatabase, adoptFold] ); useEffect(() => { @@ -7275,10 +7644,11 @@ function CustomerGridSurface({ useEffect(() => { const parked = parkedViewOpenRef.current; if (!parked || parked.topic !== scope) return; + if (workspacePending) return; if (!workspaceReady && !views.some((view) => view.id === parked.viewId)) return; parkedViewOpenRef.current = null; selectView(parked.viewId); - }, [scope, selectView, views, workspaceReady]); + }, [scope, selectView, views, workspaceReady, workspacePending]); const addressedFieldRef = useRef(null); useEffect(() => { @@ -7779,9 +8149,8 @@ function CustomerGridSurface({ * every ALERTED view and paints it as a red pill; R5 asks for exactly that number on a view * the user marked. Building a parallel counter would give one rail two ideas of "how many * records match this view", and they would drift the first time the pipeline changed. - * ⛔ AND IT ADDS NO QUERY — R8's whole subject is that `/nav` is too slow. This folds over - * `computedRows`, which the grid already holds; the memo below still walks the book once per - * counted view, and on a table with neither an alert nor a mark it does nothing at all. + * Whole-book counts still use the existing pipeline. S70-T24 asks the server for windowed + * counts only for this same set of views; the loaded page never substitutes for the total. */ const importantIds = useMemo( () => views.filter((v) => v.config?.important === true).map((v) => v.id), @@ -7794,8 +8163,43 @@ function CustomerGridSurface({ [alerted, importantIds] ); const alertedKey = countedIds.join(","); + // Pinned inclusions and saved selections cannot be represented by the aggregate request's + // filter alone. State that limitation instead of counting a wider population. + const serverCountViews = useMemo(() => views.filter((view) => countedIds.includes(view.id)) + .map((view) => ({ view, eligible: !view.assigned && view.kind !== "locked" + && !view.config.cohortLock && !view.config.memberPids?.length + && pendingMeasures(view.config.filters, measureSets) === 0 })), + [views, countedIds, measureSets]); + const serverCountKey = JSON.stringify([scope, hostWorkspace, relatedPinKey, mirrorRevision, + payload?.counts?.total, fields.map((field) => field.key), serverCountViews.map(({ view, eligible }) => + [view.id, view.config.filters, view.config.filterConj, eligible])]); + const [serverCountAnswer, setServerCountAnswer] = useState<{ + key: string; counts: Record; + }>({ key: "", counts: {} }); + useEffect(() => { + if (!serverWindowed || !hostWorkspace || !serverCountViews.length) return; + const abort = new AbortController(); + let live = true; + setServerCountAnswer({ key: serverCountKey, counts: {} }); + for (const { view, eligible } of serverCountViews) { + if (!eligible) continue; + void readImportantViewCount(topic.key, view, relatedPin, abort.signal).then((count) => { + if (!live) return; + setServerCountAnswer((previous) => previous.key === serverCountKey + ? { key: serverCountKey, counts: { ...previous.counts, [view.id]: count } } : previous); + }); + } + return () => { live = false; abort.abort(); }; + // The key includes the complete count predicate and scope, excluding display-only edits. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [serverWindowed, hostWorkspace, serverCountKey]); const alertCounts = useMemo(() => { const out: Record = {}; + if (serverWindowed && serverCountAnswer.key === serverCountKey) { + for (const [id, count] of Object.entries(serverCountAnswer.counts)) { + if (count !== null) out[id] = count; + } + } if (serverWindowed || !countedIds.length) return out; for (const id of countedIds) { const view = views.find((v) => v.id === id); @@ -7824,33 +8228,11 @@ function CustomerGridSurface({ // `alertedKey` is the scalar identity of the list; `alerted` itself is a fresh array on // every fetch even when the answer has not changed. // eslint-disable-next-line react-hooks/exhaustive-deps - }, [alertedKey, views, computedRows, fields, measureSets, cohortSets, today, serverWindowed]); + }, [alertedKey, views, computedRows, fields, measureSets, cohortSets, today, serverWindowed, + serverCountAnswer, serverCountKey]); - /** - * ⭐⭐ WAVE 33 · T14 (D-205) — WHY THERE IS NO NUMBER, said out loud. - * - * ⛔ THE COMPLAINT THIS ANSWERS. `alertCounts` above returns `{}` for a server-windowed grid - * and skips any view with an unresolved measure — both correct, because the client holds ONE - * WINDOW and a count computed from it would be the size of a page wearing the label of a - * scope. But it returned them SILENTLY, so the badge simply was not there, on exactly the - * big Odoo grids the owner has been chasing. An absent badge and "nothing matches" are - * indistinguishable to a reader. - * - * ⭐ THIS IS R6'S SECOND SENTENCE, WHICH IS THE HALF THAT GETS DROPPED: *"if there is lag or - * it can't be done, you need to explicitly tell me why and recommend a fix."* A limit that - * genuinely cannot be removed must be REPORTED with its cause — a silent truncation is the - * violation, not the limit. So this memo produces the SENTENCE for every counted view that - * did not get a number, and the rail renders it. - * - * ⛔ IT DOES NOT INVENT A COUNT, and must not. Counting a windowed grid honestly means a - * server-side `count(*)` per view — a new query per view on the read path, which is exactly - * what R8 refused for this badge ("it adds no query"). The recommended fix rides in the - * sentence instead of being silently attempted. - * - * ⚠ Keyed on the SAME `countedIds` and the SAME conditions as the memo above, in the same - * order — two lists that decide "is there a number here" by different rules would drift, and - * the drift would show as a row wearing both a count and an excuse. - */ + /** D-205: a counted view carries its server count or a named loading/unavailable reason. + * The rail checks the number first, so a view never wears both a count and an excuse. */ /* * ⛔⛔ `importantTotal` IS DELETED (W35-T27, owner item 9 / R4). * @@ -7871,19 +8253,19 @@ function CustomerGridSurface({ const view = views.find((v) => v.id === id); if (!view) continue; if (serverWindowed) { - out[id] = - "No count: this database is read through its source in windows, so the browser holds " + - "one page rather than the whole table. A number from it would be the size of that " + - "page, not of this view. Counting it honestly needs a server-side count per view."; + if (alertCounts[id] !== undefined) continue; + const eligible = serverCountViews.find((entry) => entry.view.id === id)?.eligible; + const count = serverCountAnswer.key === serverCountKey ? serverCountAnswer.counts[id] : undefined; + out[id] = importantCountNote(!hostWorkspace ? "surface" : pendingMeasures(view.config.filters, measureSets) > 0 + ? "measure" : !eligible ? "selection" : count === undefined ? "loading" : "unavailable"); } else if (pendingMeasures(view.config.filters, measureSets) > 0) { - out[id] = - "No count yet: this view filters on a measure the server has not answered for these " + - "records. The number appears once that answer arrives."; + out[id] = importantCountNote("measure"); } } return out; // eslint-disable-next-line react-hooks/exhaustive-deps - }, [alertedKey, views, measureSets, serverWindowed]); + }, [alertedKey, views, measureSets, serverWindowed, hostWorkspace, alertCounts, serverCountViews, + serverCountAnswer, serverCountKey]); const addToList = useCallback( (viewId: string, cohortId: string, name: string) => { @@ -9975,7 +10357,8 @@ function CustomerGridSurface({ // W45-T14 keeps the outer workspace and View rail mounted. Only the keyed right pane enters // loading, so a database switch cannot replace either navigation DOM subtree. - const workspaceLoading = loading || !workspaceReady; + const rowsLoading = loading || !workspaceReady; + const workspaceLoading = rowsLoading || workspacePending; const activeView = views.find((view) => view.id === activeViewId); const routeSourceView: RouteSourceView | null = activeView @@ -10062,21 +10445,14 @@ function CustomerGridSurface({ const nextOrder = order.map((candidate) => visible.has(candidate) ? moved[index++] : candidate); updateConfig({ ...config, order: nextOrder }); }; - // Item 10 — the toolbar's mode switcher (grid-only on windowed tables, see displayMode). - // - // ⭐⭐ W56-T24 / R4 — `cellView` IS THE ONE RELAXATION, AND THE PREDICATE IS SPELT IN TWO PARTS - // ON PURPOSE. `modeWithheld` is the original three-way test, unchanged and unrewritten, because - // `verify_grid_ux.py` reads it as a LITERAL to prove W33-T74's claim that the artefact's KIND is - // not a Query preview's to change. Folding `cellView` into that expression made the gate red on a - // claim this ticket never touched: a Query preview still has no switcher, since `cellViewModes` - // demands `!isQueryPreview` as well. A windowed table is refused for the same reason it always - // was - list, kanban and calendar compute over the whole matched set, and a page is not it. - const modeWithheld = serverWindowed || embedded || isQueryPreview; + // S70-T12: same sibling switch, with only the server-backed modes and R16's two next modes. + const modeWithheld = embedded || isQueryPreview; const cellViewModes = cellView && !serverWindowed && !isQueryPreview; const modeControl = modeWithheld && !cellViewModes ? undefined : ( ) : null; - const mapSettingsRow = displayMode === "map" && !pivotOn ? ( + const mapSettingsRow = displayMode === "map" && !serverWindowed && !pivotOn ? ( ` on every render without removing the last (21 copies measured, live). Both sides are prefixed so neither can be spelled by the other. */}
- {workspaceLoading ? ( -
- + {workspaceError &&
+ Saved views unavailable. + +
} + {rowsLoading ? ( +
+
) : ( <> @@ -10973,7 +11363,8 @@ function CustomerGridSurface({ searchRef={searchRef} onSearchEscape={() => requestAnimationFrame(() => gridRef.current?.focus())} recordCount={recordCount} - scopeCounts={payload?.counts} + scopeCounts={serverWindowed && payload?.counts && (displayMode === "kanban" || displayMode === "calendar") + ? { ...payload.counts, shown: payload.counts.matched } : payload?.counts} pool={payload?.workspace?.pool} serverWindowed={serverWindowed} cohortAction={cohortAction} @@ -11026,7 +11417,7 @@ function CustomerGridSurface({ and guard shape as `MapSettings`/`CalendarSettings`: after the primary Toolbar, before the grid. The primary mode trigger stays on row one; only the date-field chooser Timeline used to render there lives here now. */} - {displayMode === "timeline" && !pivotOn && ( + {displayMode === "timeline" && !serverWindowed && !pivotOn && ( )} {displayMode === "list" && ( - + {serverWindowed && config.groupBy && !serverAggs?.groups ? ( + serverAggAnswer?.key !== serverAggRequestKey ? + :
Grouping is unavailable for this window.
+ ) :
+ />
} + {serverWindowed && config.groupBy && !groupSummaryOn && serverAggs?.groups?.map((group) => { + const key = group.key == null ? "" : String(group.key); + if (!expanded.has(key)) return null; + const page = groupPages.get(key); + return
+ {group.label || "(empty)"} + {page?.failed && loadGroupPage(key, page.offset)} />} + {(!page || page.pending) && } + {group.count > pageSize && loadGroupPage(key, offset)} />} +
; + })} +
)} {displayMode === "calendar" && (calendarField ? ( + serverWindowed ? : )} - {displayMode === "timeline" && + {displayMode === "timeline" && !serverWindowed && (calendarField ? ( : {displayMode === "chart" ? "Chart comes next." : "Map comes next."} + )} + {serverWindowed && !WINDOWED_VIEW_MODES.includes(displayMode) && ( +
This view cannot read a server window yet.
+ )} + {displayMode === "chart" && !serverWindowed && ( + {pagingNote && ( + + {pagingNote.short} + + )} {foldNote && {foldNote}} {/* ⭐⭐ W50-T03 / C1 — a column the SERVER could not summarise says so, in the strip that already carries every other "why is this number what it is" sentence. C1 is @@ -13368,9 +13801,9 @@ function CustomerGridSurface({ {...(recordStars.ready ? { onStar: (on: boolean) => recordStars.toggle(detailPid, on) } : {})} - positionLabel={`Record ${dataPosition.toLocaleString()} of ${recordCount.toLocaleString()}`} - canPrev={neighborExists(-1)} - canNext={neighborExists(1)} + positionLabel={mirrorDetailRecord ? "Record" : `Record ${dataPosition.toLocaleString()} of ${recordCount.toLocaleString()}`} + canPrev={!mirrorDetailRecord && neighborExists(-1)} + canNext={!mirrorDetailRecord && neighborExists(1)} onPrev={() => go(-1)} onNext={() => go(1)} onClose={() => setDetailPid(null)} diff --git a/web/src/customer-grid/JsonViewer.tsx b/web/src/customer-grid/JsonViewer.tsx index 4356a55603c0eb0334392b4d03887896733b7067..e784937651a3f8d4be2142a421716c7cf88a267f 100644 --- a/web/src/customer-grid/JsonViewer.tsx +++ b/web/src/customer-grid/JsonViewer.tsx @@ -663,7 +663,7 @@ export function CodeViewer({ label, value, language, onSave, onClose }: CodeView useOverlayLayer({ panelRef, onDismiss: onClose, - dismissOnOutside: true, + dismissOnOutside: false, initialFocus: "[data-overlay-autofocus]", trapFocus: true, }); @@ -858,7 +858,7 @@ export default function JsonViewer({ label, value, onSave, onClose }: JsonViewer useOverlayLayer({ panelRef, onDismiss: onClose, - dismissOnOutside: true, + dismissOnOutside: false, initialFocus: "[data-overlay-autofocus]", trapFocus: true, }); diff --git a/web/src/customer-grid/RecordDetail.tsx b/web/src/customer-grid/RecordDetail.tsx index 7acb7b853fc8cca7e17e080a6ce26c6f68f30600..a397c0f7f5b2e1a3f670249cb7e08c4f912222cb 100644 --- a/web/src/customer-grid/RecordDetail.tsx +++ b/web/src/customer-grid/RecordDetail.tsx @@ -41,7 +41,7 @@ import { } from "./cells"; // The ONE scheme guard, shared with the kanban card since wave 26 (item 11). From `display` // rather than `cells` because `cells` re-exports only the handful the canvas needed. -import { actionHref, numericEditText, numericIsBlank } from "./display"; +import { actionHref, linkedRecordIds as linkedPids, linkedRecordRefusal, linkedRecordText, numericEditText, numericIsBlank } from "./display"; import type { CellValue } from "./display"; import JsonViewer, { CodeViewer } from "./JsonViewer"; import { StarRow, StarIcon } from "./Stars"; @@ -63,11 +63,11 @@ import type { CustomerDoc, DisplayMode, Field, RelatedEntry, RelatedPin, Related /* ⭐ W53-T22 adds a FOURTH and a FIFTH, and for the same reason: `fetchTableWindow` + `WINDOW_ROWS` are the one windowed read this product owns, and the sibling set a pushed frame's chevrons page is that read with the frame's own pin on it. */ -import { fetchLinkTargets, fetchRecordMeasureValues, fetchRelated, fetchRelatedProductMeasureValues, +import { dropTableRowsCache, fetchLinkTargets, fetchRecordMeasureValues, fetchRelated, fetchRelatedProductMeasureValues, fetchRelatedMeasureValues, fetchTableWindow, fetchTopicRows, isRelatedMetricScope, isRelatedProductMetricScope, probeRelatedRead, readThroughTables, usesPinnedRows, WINDOW_ROWS } from "./apiBridge"; -import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, checkTenant, signal } from "../apiContract"; +import { API_V1, CREDENTIALS, ROWS_STALE_EVENT, WORKSPACE_STALE_EVENT, UNAUTHORIZED_EVENT, checkTenant, signal } from "../apiContract"; /** * ⭐⭐ WAVE-34 (owner ruling R13) — whose value sits in this enrichment cell, in words. @@ -859,25 +859,103 @@ function writeTabViews(value: Record): void { * ⭐⭐ W53-T25 — THE PIDS IN A LINK CELL, and the shape is the one this product already stores. * * A link cell is a COMMA-JOINED PID STRING. The precedent is `CustomerGrid`'s own reader - * (`linkValue.split(",")…`), and this is that reader, inlined rather than shared: there is no - * exported helper for it today and `CustomerGrid.tsx` is not this ticket's fence, so the choice - * is one more copy or an edit to a file this lane does not own. ⚠ IF ONE IS EVER EXPORTED, THIS - * IS THE CALL SITE TO DELETE — two spellings of one storage format is how a link column comes to - * mean different things in two panels. + * (`linkValue.split(",")…`). S69-T19 shares this drawer's parser with its readable link value + * through `display.linkedRecordIds`, imported as `linkedPids` for the existing navigation sites. * * ⛔ DE-DUPLICATED AND FAIL-CLOSED. A blank, a stray separator or anything that is not a positive * integer is DROPPED rather than coerced: a `NaN` in this list projects nothing and reads on * screen as "no linked records", which is a wrong answer wearing the right chrome. */ -function linkedPids(value: unknown): number[] { - const raw = value === null || value === undefined ? "" : String(value); - if (!raw.trim()) return []; - const seen = new Set(); - for (const part of raw.split(",")) { - const pid = Number(part.trim()); - if (Number.isInteger(pid) && pid > 0) seen.add(pid); - } - return [...seen]; +/** Labels use the existing ID-constrained row door; Open keeps the existing link editor. */ +export function RecordLinkValue({ field, value, inputId, onOpen }: { + field: Field; + value: CellValue; + inputId: string; + onOpen: () => void; +}) { + const ids = linkedPids(value); + const idsKey = JSON.stringify(ids); + const [revision, setRevision] = useState(0); + const sequence = useRef(0); + const [answer, setAnswer] = useState<{ + field: Field; idsKey: string; revision: number; text: string; refused: boolean; + } | null>(null); + useEffect(() => { + const invalidate = () => { + sequence.current += 1; + if (field.link?.table) dropTableRowsCache(field.link.table); + setAnswer(null); + setRevision((current) => current + 1); + }; + window.addEventListener(ROWS_STALE_EVENT, invalidate); + window.addEventListener(WORKSPACE_STALE_EVENT, invalidate); + return () => { + window.removeEventListener(ROWS_STALE_EVENT, invalidate); + window.removeEventListener(WORKSPACE_STALE_EVENT, invalidate); + }; + }, [field.link?.table]); + useEffect(() => { + const requested = JSON.parse(idsKey) as number[]; + const table = field.link?.table; + if (!table || !requested.length) return; + let live = true; + const asked = ++sequence.current; + const current = () => live && asked === sequence.current; + void (async () => { + const rows: Row[] = []; + let targetFields: Field[] = []; + let offset = 0; + let total = 0; + let schema = ""; + do { + let refusal = ""; + const payload = await fetchTableWindow(table, { ids: requested, offset, limit: WINDOW_ROWS }, (reason) => { refusal = reason; }); + if (!current()) { + // The shared reader caches before it returns. A superseded response must not seed a + // later drawer with labels fetched before a permission/row invalidation. + dropTableRowsCache(table); + return; + } + if (!payload) { + setAnswer({ field, idsKey, revision, text: linkedRecordRefusal(refusal), refused: true }); + return; + } + const pageTotal = payload.counts?.matched ?? payload.rows.length; + const pageSchema = JSON.stringify(payload.fields); + if (offset > 0 && (pageTotal !== total || pageSchema !== schema + || payload.rows.some((row) => rows.some((prior) => prior.pid === row.pid)))) { + dropTableRowsCache(table); + setAnswer({ field, idsKey, revision, text: "Linked records are unavailable.", refused: true }); + return; + } + targetFields = payload.fields; + rows.push(...payload.rows); + total = pageTotal; + schema = pageSchema; + // A shortened/moving page never becomes an unscoped retry or a fabricated empty set. + if (!payload.rows.length && offset < total) { + setAnswer({ field, idsKey, revision, text: "Linked records are unavailable.", refused: true }); + return; + } + offset += payload.rows.length; + } while (offset < total); + if (current()) setAnswer({ field, idsKey, revision, + text: linkedRecordText(requested, rows, targetFields), refused: false }); + })(); + return () => { live = false; }; + }, [field, idsKey, revision]); + const settled = answer?.field === field && answer.idsKey === idsKey && answer.revision === revision; + const state = !String(value ?? "").trim() ? "empty" : !ids.length || !field.link?.table ? "unavailable" + : !settled ? "pending" : answer.refused ? "unavailable" : "ready"; + return ( + + {state === "pending" + ? + : {state === "empty" ? "No linked records" + : state === "unavailable" ? settled ? answer.text : "Linked records are unavailable." : answer?.text}} + {field.link?.table && } + + ); } interface RelationTargetAnswer { @@ -2256,19 +2334,10 @@ export default function RecordDetail({ (dragKey === field.key ? " is-dragging" : ""); let slot: ReactNode; - if (field.type === "link" && field.link) { + if (field.type === "link") { slot = ( - - {formatDisplay(field, record[field.key]) || "No linked records"} - - + setOpenLinkField(field)} /> ); } else if (isEdit) { const raw = String(record[field.key] ?? ""); diff --git a/web/src/customer-grid/apiBridge.ts b/web/src/customer-grid/apiBridge.ts index aa3f8a3ae9106e0a452072a9e758b8561ec1d508..771703d2d7edf601e50670524893bb114652d433 100644 --- a/web/src/customer-grid/apiBridge.ts +++ b/web/src/customer-grid/apiBridge.ts @@ -103,7 +103,15 @@ export function savePersonalGridLayout(key: string, layout: PersonalGridLayout): keepalive: true, }); if (handledSession(res)) { entry.next = null; return false; } - if (!res.ok) throw new Error("layout write refused"); + if (!res.ok) { + const body = await readJson(res); + if ((body as { error?: { code?: string } } | null)?.error?.code === "data_input_frozen") { + entry.next = null; + signal(TOAST_EVENT, refusalMessage(body, res.status)); + return false; + } + throw new Error("layout write refused"); + } } catch { saved = false; signal(TOAST_EVENT, "Your grid layout could not be saved. Change a view control to retry."); @@ -149,7 +157,7 @@ async function readJson(res: Response): Promise { * be the same person, and a cached book must never cross that boundary. */ function handledUnauthorized(status: number): boolean { if (status !== 401) return false; - rowsCache.clear(); + clearCustomersCache(); signal(UNAUTHORIZED_EVENT); return true; } @@ -230,8 +238,10 @@ const rowsCache = new Map(); * field, switch away, switch back, and be served the pre-write envelope — the write silently * undone on screen, which is worse than the wait it replaced. */ -const wsCache = new Map(); -type WorkspaceRead = { ws: GridWorkspace | null; failure?: string }; +const wsCache = new Map(); +type WorkspaceRead = { ws: GridWorkspace | null; failure?: string; superseded?: boolean }; +export type WorkspaceStatus = { scope: SurfaceScope; state: "ready" | "absent" | "error"; message?: string }; +export const WORKSPACE_ERROR_EVENT = "aios:workspace-error"; // Pending reads belong to the same memo, not a second result cache. Uncached post-write // reads replace the joinable flight; an older completion must not remove that newer one. const wsFlights = new Map>(); @@ -1295,12 +1305,11 @@ export async function probeRelatedRead( * The body is `{workspace: GridWorkspace}` — the client's own built type * (types.ts `GridWorkspace`), which is what the embed's host already projects. * - * ⚠ ABSENT IS FINE AND MEANS TODAY'S BEHAVIOUR. A 404 (route not shipped) or a - * body carrying no workspace return null and the grid runs exactly as it does - * now — views in memory, no `storageKey`, nothing durable. It degrades to the + * ⚠ A 404 retains compatibility with a host that does not ship this route: + * views in memory, no `storageKey`, nothing durable. It degrades to the * honest gap rather than failing the whole read, because the workspace is an - * enhancement of the table, not a precondition for it. A 401 still ends the - * session: that is the one status that is never about this route. + * enhancement of the table, not a precondition for it. A malformed 200 needs + * a scoped error and Retry. A 401 still ends the session. * * ⛔ WAVE 21 item 3 (3c) — BUT A REFUSAL IS NOT AN ABSENCE, and conflating the * two is half of the "RI fields on a new database" report. @@ -1313,68 +1322,112 @@ export async function probeRelatedRead( * `/workspace` 403s for `ut_*` therefore got ANOTHER TABLE'S views and fields, * silently, with the grid looking entirely healthy. * - * `announceFailure` is the caller saying "I am the INITIAL load; if this fails - * for a reason that is not 'no workspace here', say so". It raises the same - * `DATA_ERROR_EVENT` a failed rows read raises, so the frame renders its honest - * failure card instead of a grid furnished with somebody else's schema. + * Workspace failures use a scoped status and a nonfatal notification. The rows + * reader owns DATA_ERROR_EVENT; metadata cannot replace independently ready rows. * * ⚠ IT IS OFF BY DEFAULT, and that is load-bearing rather than cautious. * `reread()` calls this on every workspace-stale signal, and its own rule is * "absent stays absent — never blank a live panel"; a signal from there would - * let one dropped packet replace a working table with an error card. The flag - * is passed explicitly at the one call site that owns the first paint. + * let one dropped packet replace a working table with an error card. Callers + * can consume onStatus to offer an inline metadata retry instead. */ /** * HOTFIX 2026-09-29 (owner: "clicking edit for cell database ... is still very slow"). The memo's * answer for `scope` when it is inside its freshness window, WITHOUT a request, else null. A * panel paints from it at once and still asks the server; the server's answer replaces it. */ -export function peekWorkspace(scope: SurfaceScope): GridWorkspace | null { - const hit = wsCache.get(scope); +export function peekWorkspace(scope: SurfaceScope, opts: { fieldsOnly?: boolean } = {}): GridWorkspace | null { + const key = opts.fieldsOnly ? `${scope}:fields` : scope; + const hit = wsCache.get(key); return hit && Date.now() - hit.at < CUSTOMERS_FRESH_MS ? workspaceWithoutMetricCells(hit.ws) : null; } export async function fetchWorkspace( scope: SurfaceScope = "customer", - opts: { announceFailure?: boolean; allowCached?: boolean; refresh?: boolean } = {}, + opts: { announceFailure?: boolean; allowCached?: boolean; refresh?: boolean; fieldsOnly?: boolean; + onStatus?: (status: WorkspaceStatus) => void } = {}, ): Promise { const loud = opts.announceFailure === true; + const fieldsOnly = opts.fieldsOnly === true; + const key = fieldsOnly ? `${scope}:fields` : scope; + const requestEpoch = wsCacheEpoch; + const fullAtStart = wsCache.get(scope); // ⭐ W31-T21 — OPT IN, NEVER BY DEFAULT. Only the first-paint read of a surface may be served // from the memo; the post-write `reread()` calls this with no options and therefore always goes // to the server, which is what keeps the memo from becoming a second source of truth. if (opts.allowCached === true) { - const hit = wsCache.get(scope); + const hit = wsCache.get(key); // A warm-open refresh bypasses only the completed memo, and still joins pending work. - if (!opts.refresh && hit && Date.now() - hit.at < CUSTOMERS_FRESH_MS) + if (!opts.refresh && hit && Date.now() - hit.at < CUSTOMERS_FRESH_MS) { + opts.onStatus?.({ scope, state: "ready" }); return workspaceWithoutMetricCells(hit.ws); + } } - let pending = opts.allowCached === true ? wsFlights.get(scope) : undefined; + let pending = opts.allowCached === true ? wsFlights.get(key) : undefined; if (!pending) { - pending = readWorkspace(scope, wsCacheEpoch); - wsFlights.set(scope, pending); - const flight = pending; + // A fresh full question invalidates older schema offers through this same memo. + if (!fieldsOnly) { + wsCache.delete(`${scope}:fields`); + wsFlights.delete(`${scope}:fields`); + const previousFull = wsCache.get(scope); + if (previousFull) previousFull.current = false; + } + const epoch = wsCacheEpoch; + let flight: Promise; + flight = readWorkspace(scope, epoch, fieldsOnly, () => wsFlights.get(key) === flight); + pending = flight; + wsFlights.set(key, pending); void flight.then(() => { - if (wsFlights.get(scope) === flight) wsFlights.delete(scope); + if (wsFlights.get(key) === flight) wsFlights.delete(key); }); } - const result = await pending; - // Failure belongs to each caller: a loud joiner of a quiet request must still hear - // the server's sentence. Parsing and the session check belong to the response once. - if (loud && result.failure) signal(DATA_ERROR_EVENT, result.failure); + let result = await pending; + if (fieldsOnly && result.superseded && requestEpoch === wsCacheEpoch) { + // A Sheet full read may supersede the editor's schema read. Follow the current + // existing flight, then use only an authoritative full memo published since this + // question began. A failed full read cannot turn an older offer into a fresh one. + let full = wsFlights.get(scope); + while (full) { + result = await full; + if (requestEpoch !== wsCacheEpoch) return null; + const next = wsFlights.get(scope); + full = next !== full ? next : undefined; + } + const current = wsCache.get(scope); + result = current && current.current && current !== fullAtStart && Date.now() - current.at < CUSTOMERS_FRESH_MS + ? { ws: workspaceWithoutMetricCells(current.ws) } : { ws: null, failure: result.failure }; + } + // A departed session or superseded request cannot publish even a failure into the new scope. + if (requestEpoch !== wsCacheEpoch || result.superseded) return null; + // Each joiner receives its own scoped status; response parsing/session handling happen once. + const status: WorkspaceStatus = result.failure + ? { scope, state: "error", message: result.failure } + : { scope, state: result.ws ? "ready" : "absent" }; + opts.onStatus?.(status); + if (loud && result.failure) signal(WORKSPACE_ERROR_EVENT, status); const good = result.ws; return good ? workspaceWithoutMetricCells(good) : null; } -async function readWorkspace(scope: SurfaceScope, epoch: number): Promise { +async function readWorkspace(scope: SurfaceScope, epoch: number, fieldsOnly: boolean, + isCurrent: () => boolean): Promise { let res: Response; try { - res = await fetch(`${API_V1}/workspace?scope=${encodeURIComponent(scope)}`, + res = await fetch(`${API_V1}/workspace?scope=${encodeURIComponent(scope)}${fieldsOnly ? "&fields_only=1" : ""}`, { credentials: CREDENTIALS }); } catch { - return { ws: null, failure: "Cannot reach the server." }; + return epoch === wsCacheEpoch && isCurrent() + ? { ws: null, failure: "Cannot reach the server." } : { ws: null, superseded: true }; } if (handledSession(res) || epoch !== wsCacheEpoch) return { ws: null }; if (!res.ok) { + if (!isCurrent()) return { ws: null, superseded: true }; + if (isCurrent() && (res.status === 403 || res.status === 404)) { + wsCache.delete(scope); + wsCache.delete(`${scope}:fields`); + wsFlights.delete(scope); + wsFlights.delete(`${scope}:fields`); + } // 404 is the ONE status that means "this host does not serve a workspace", // which is the enhancement posture above and stays quiet. Everything else // is a server that had an answer and would not give it. @@ -1387,21 +1440,38 @@ async function readWorkspace(scope: SurfaceScope, epoch: number): Promise 0 && Array.isArray(w.views) + && w.views.every((view) => view && typeof view.id === "string") + && (w.fields === undefined || (Array.isArray(w.fields) + && w.fields.every((field) => field && typeof field.key === "string"))) + && (w.measures === undefined || (Array.isArray(w.measures) + && w.measures.every((measure) => measure && typeof measure.key === "string"))) + && (w.derived === undefined || (w.derived && typeof w.derived === "object" && !Array.isArray(w.derived) + && Object.values(w.derived).every((cells) => cells && typeof cells === "object" && !Array.isArray(cells)))) + && (fieldsOnly ? w.fieldsOnly === true : w.fieldsOnly !== true) ? w : null; + if (!isCurrent()) return { ws: null, superseded: true }; + if (!good) { + wsCache.delete(fieldsOnly ? `${scope}:fields` : scope); + return { ws: null, failure: "Saved views could not be loaded." }; + } // ⭐ W31-T21 — EVERY SUCCESSFUL READ REFRESHES THE MEMO, including the post-write `reread()` // that is not allowed to CONSUME it. That asymmetry is the whole safety argument: the write // path can never leave a stale envelope behind for the next switch to serve. // ⚠ A FAILURE LEAVES THE PREVIOUS ENTRY ALONE rather than poisoning it with `null` — the same // "absent stays absent, never blank a live panel" posture `reread()` already takes. - if (good) { - const previous = wsCache.get(scope)?.ws; - wsCache.set(scope, { at: Date.now(), ws: good }); + if (good && isCurrent()) { + const key = fieldsOnly ? `${scope}:fields` : scope; + const previous = wsCache.get(key)?.ws; + wsCache.set(key, { at: Date.now(), ws: good, current: true }); + if (!fieldsOnly) { + wsCache.delete(`${scope}:fields`); + wsFlights.delete(`${scope}:fields`); + } // A refreshed metric definition or answer is read again through each grid's own pin. // The same rows door still supplies the ordinary record's global Details values. - if (previous && JSON.stringify(metricWorkspaceCells(previous)) !== JSON.stringify(metricWorkspaceCells(good))) { + if (!fieldsOnly && previous && JSON.stringify(metricWorkspaceCells(previous)) !== JSON.stringify(metricWorkspaceCells(good))) { rowsCache.clear(); signal(ROWS_STALE_EVENT); } @@ -2424,7 +2494,15 @@ async function drain(): Promise { // turn one expired cookie into an unbounded request loop. if (!handledSession(res)) { if (res.ok) applyEventResult(await readJson(res)); - else requeue = true; + else { + const body = await readJson(res); + if ((body as { error?: { code?: string } } | null)?.error?.code === "data_input_frozen") { + signal(TOAST_EVENT, refusalMessage(body, res.status)); + clearCustomersCache(); + signal(ROWS_STALE_EVENT); + signal(WORKSPACE_STALE_EVENT); + } else requeue = true; + } } } catch { requeue = true; diff --git a/web/src/customer-grid/counts.ts b/web/src/customer-grid/counts.ts index 3e0b6863b3c9afe5e62d017e9eb8b00eb47d6147..12faa161d25683fee6317e66c5443b2aafcaea70 100644 --- a/web/src/customer-grid/counts.ts +++ b/web/src/customer-grid/counts.ts @@ -90,6 +90,44 @@ export function countLabelCompact(recordCount: number, scope?: ScopeCounts): str return `showing ${n(scope.shown)} of ${n(scope.matched)}`; } +/** Paging is a presentation choice, never a bound on the reachable records. */ +export function pagingSummary(counts?: ScopeCounts): { short: string; full: string } | null { + if (!counts?.windowed) return null; + return { + short: "Records load one page at a time", + full: "Use the page controls in a supported view to browse every matching record.", + }; +} + +/** A missing or limited aggregate answer must never become a zero badge. */ +export function serverViewCount(body: unknown): number | null { + const answer = body as { + ok?: unknown; total_rows?: unknown; aggregates?: unknown; limits?: unknown; + } | null; + if (!answer || answer.ok !== true || typeof answer.total_rows !== "number" + || !Number.isSafeInteger(answer.total_rows) || answer.total_rows < 0 + || !answer.aggregates || typeof answer.aggregates !== "object" + || Array.isArray(answer.aggregates) || !Array.isArray(answer.limits)) return null; + if (answer.limits.some((limit: GridLimit | null) => !limit + || (limit.effect !== "none" && limit.effect !== "slow"))) return null; + return answer.total_rows; +} + +export function importantCountNote(status: "loading" | "unavailable" | "selection" | "measure" | "surface"): string { + switch (status) { + case "loading": return "Counting this view…"; + case "selection": return "Count unavailable: this view uses a saved record selection."; + case "measure": return "Count pending: this view's measure has not loaded."; + case "surface": return "Count unavailable: this surface does not load saved-view counts."; + default: return "Count unavailable: the full-view count could not be loaded. Reload to try again."; + } +} + +/** The server's prose is subject to the same copy rule as the local template. */ +export function gridLimitCopy(text: string): string { + return text.replace(/\s*[\u2013\u2014]\s*/g, "; "); +} + // ═══════════════════════════════════════════════════════════════════════════════════════════ // ⭐⭐ WAVE 30 · W30-T42 (contract C2) — THE THREE DECISIONS A WINDOWED GRID MAKES, // EXTRACTED AS PURE FUNCTIONS SO THEY CAN BE RUN RATHER THAN GREPPED. @@ -200,9 +238,9 @@ export function limitSummary( ); if (binding.length === 0) return null; const full = binding - .map((l) => `${l.subject}: ${l.cause ?? ""}${l.recommendation ? ` — ${l.recommendation}` : ""}`) + .map((l) => gridLimitCopy(`${l.subject}: ${l.cause ?? ""}${l.recommendation ? `; ${l.recommendation}` : ""}`)) .join("\n\n"); - const short = binding.map((l) => plainLimit(l, counts)).join(" · "); + const short = binding.map((l) => gridLimitCopy(plainLimit(l, counts))).join(" · "); return { short, full }; } @@ -222,7 +260,7 @@ export function limitSummary( * ⛔ THE REWRITE IS A REWORD, NEVER A DELETION, AND THAT IS W30/R6's SECOND SENTENCE. *"A limit * that genuinely cannot be removed must be REPORTED — with its cause and a recommended fix — never * silently enforced. A truncation nobody was told about is the violation, not the limit."* So - * `full` (the hover) is untouched, every binding limit still produces a clause, and the `null` case + * `full` (the hover) keeps the cause and recommendation, every binding limit still produces a clause, and the `null` case * is still only "nothing binds". * * ⚠ KEYED ON `effect`, NOT ON `subject` OR ON THE PROSE. `effect` is the machine-readable enum the @@ -245,8 +283,8 @@ function plainLimit(l: GridLimit, counts?: { shown?: number; matched?: number } // The done-when's own example. The numbers come from `counts`, which the payload already // carries for every windowed grid — and when it does not, the sentence still names the cause. return shown !== null && matched !== null && matched > shown - ? `Showing ${n(shown)} of ${n(matched)} rows; this database is too large to load at once` - : "This database is too large to load at once, so only the first rows are here"; + ? `Showing ${n(shown)} of ${n(matched)} rows; use the page controls to see more` + : "Records load one page at a time; use the page controls to see more"; case "slow": return "Rows this far down load slowly; each page is reached by walking every row above it"; case "filter_ignored": @@ -273,33 +311,16 @@ function firstSentence(cause: string | undefined): string { } /** - * ⭐⭐ WAVE 30 · W30-T42 / R6's SECOND SENTENCE — WHAT A WINDOWED GRID CANNOT DO, SAID OUT LOUD. - * - * ⛔ THE THING THIS EXISTS TO PREVENT, AND IT IS A CLIFF NOBODY HAD WRITTEN DOWN. Turning - * `counts.windowed` on for the orders grid does not only change how rows arrive — nine controls - * `CustomerGrid` already gates on `serverWindowed` DISAPPEAR the same instant, every one of them - * with no explanation on screen. They were all working on that grid the day before, because the - * whole table was in the browser. Each refusal is individually correct — a cohort built from the - * 200 rows that happen to be in memory is a wrong cohort, an export of one page is a wrong - * export, a group header over a window subtotals the PAGE while claiming to describe the group — - * and a person who just watched eight buttons vanish is owed the reason. - * - * That is R6's second sentence exactly: *"if there is lag or it can't be done, you need to - * explicitly tell me why and recommend a fix."* A limit that cannot be removed must be REPORTED - * with its cause and a recommendation, never silently enforced. Silently enforcing this one would - * have been the violation, not the limit. - * - * ⚠ THE LIST IS THE CODE'S, NOT A GUESS: every entry below is a control `CustomerGrid.tsx` turns - * off on `serverWindowed`, and `verify_grid_ux.py` counts the gates so an item cannot be added - * there without a line here. + * S70 R16 supersedes the blanket alternative-view refusal. Grid, List, Gallery, Kanban and + * Calendar page their records; Chart and Map retain their one-line withholding. Server-count + * badges and server grouping are available too. These remaining tools need the full selection. */ export const WINDOWED_TOOLS_OFF = [ - "the kanban, calendar, list, map and catalog views", + "chart and map views", "export", "add to cohort, and remove from cohort", "add to folder", "select from file", - "view alert badges", ]; export function windowedCapabilityNote(matched: number): { short: string; full: string } { @@ -309,10 +330,9 @@ export function windowedCapabilityNote(matched: number): { short: string; full: full: `This database is read one page at a time out of ${n} records, so the tools that need ` + `every matching row in the browser are off here: ${WINDOWED_TOOLS_OFF.join(", ")}. ` - + `Filtering, searching and sorting are NOT affected; they run on the server across all ` - + `${n}. Restoring the rest needs the server to do those folds: an export, a cohort ` - + `membership and a group summary computed over the whole match set rather than over the ` - + `page in memory.`, + + `Filtering, searching and sorting are not affected; they run on the server across all ` + + `${n}. Restoring the rest needs the server to export and select the whole match set ` + + `instead of the page in memory.`, }; } diff --git a/web/src/customer-grid/display.ts b/web/src/customer-grid/display.ts index 0725c4fac4929807c3e8d1542ce305d816507c37..cac08efc3d910c6d54e2fc18404b8e9c170344c2 100644 --- a/web/src/customer-grid/display.ts +++ b/web/src/customer-grid/display.ts @@ -18,7 +18,7 @@ // and leave a re-export: what must not happen is a second copy. // --------------------------------------------------------------------------- -import type { Field, FieldFormat } from "./types"; +import type { Field, FieldFormat, Row } from "./types"; import { isUserSchemaField, ratingMax } from "./types"; // ⭐ W50-T05 — the value/share COMPOSITION, not the share arithmetic. `ui/fmt.ts` is React-free // and glide-free like this module, so the node gate still reaches every branch below. @@ -28,6 +28,45 @@ import { withShare } from "../ui/fmt"; export type CellValue = string | number | null | undefined; +/** The stored link set, shared by drawer navigation and its readable value. */ +export function linkedRecordIds(value: unknown): number[] { + const seen = new Set(); + for (const part of String(value ?? "").split(",")) { + const pid = Number(part.trim()); + if (Number.isSafeInteger(pid) && pid > 0) seen.add(pid); + } + return [...seen]; +} + +/** Retain the refusal's meaning without disclosing its target key or diagnostic payload. */ +export function linkedRecordRefusal(reason: string): string { + return /\b(access|permissions?|forbidden)\b/i.test(reason) + ? "You do not have access to these linked records." + : "Linked records are unavailable."; +} + +/** Read only the primary field in the server's permitted field/row projection. */ +export function linkedRecordText(ids: readonly number[], rows: readonly Row[], fields: readonly Field[]): string { + if (!ids.length) return "No linked records"; + const primary = fields.find((field) => field.pinned) ?? fields[0]; + const byId = new Map(rows.map((row) => [Number(row.pid), row])); + const labels: string[] = []; + let unavailable = false; + for (const pid of ids) { + const row = byId.get(pid); + if (!row) { + unavailable = true; + continue; + } + // Match the grid's permitted primary rule, never borrow an undeclared row property or ID. + const name = primary && primary.key !== "pid" && primary.type !== "link" + ? formatDisplay(primary, row[primary.key]).trim() : ""; + labels.push(name || "Unnamed record"); + } + if (unavailable) labels.push("Some linked records are unavailable"); + return labels.join(", "); +} + // --- C-AVATAR (wave-14 item 11): the two PURE halves of the assignee avatar --- // They live here rather than in cells.ts for this module's founding reason: cells.ts imports // glide, so nothing in it can be reached by a node gate. The canvas drawing stays there; the @@ -737,6 +776,11 @@ export function formatDisplay(field: Field, v: CellValue, share?: string): strin * function has returned since wave 5, moved behind `formatDisplay` rather than changed. */ function valueText(field: Field, v: CellValue): string { switch (field.type) { + case "link": { + // Surfaces without a permitted target projection use the grid's anonymous count. + const count = linkedRecordIds(v).length; + return count === 0 ? "" : count === 1 ? "1 record" : `${count} records`; + } case "currency": return numericIsBlank(v) ? "" : "$" + numberText(num(v), field.format); case "formula": { diff --git a/web/src/customer-grid/formulaEngine.ts b/web/src/customer-grid/formulaEngine.ts index 187617349d6072a02be0af452dc2e5f736db5a90..3ffbbc62aa01e601a545ddff74fe1f86e0b892bf 100644 --- a/web/src/customer-grid/formulaEngine.ts +++ b/web/src/customer-grid/formulaEngine.ts @@ -105,7 +105,7 @@ export type FormulaFn = | "IF" | "AND" | "OR" | "NOT" | "IFERROR" | "ISBLANK" | "SWITCH" | "CONCATENATE" | "LEFT" | "RIGHT" | "MID" | "LEN" | "TRIM" | "UPPER" | "LOWER" | "PROPER" | "VALUE" | "TEXT" - | "SUBSTITUTE" | "FIND" | "REGEX_EXTRACT" | "REGEX_REPLACE" + | "SUBSTITUTE" | "FIND" | "SEARCH" | "SEARCHB" | "ISFORMULA" | "REGEX_EXTRACT" | "REGEX_REPLACE" | "TODAY" | "YEAR" | "MONTH" | "DAY" | "DAYS" | "DATEADD" | "DATETIME_DIFF" | "DATETIME_FORMAT" // ── wave 50, W50-T16: the Excel core ────────────────────────────────────── @@ -284,6 +284,9 @@ const FN_ARITY: Record = { SUBSTITUTE: [3, 4], // FIND(stringToFind, whereToSearch, [startFromPosition]) — NEEDLE FIRST. FIND: [2, 3], + SEARCH: [2, 3], + SEARCHB: [2, 3], + ISFORMULA: [1, 1], REGEX_EXTRACT: [2, 2], REGEX_REPLACE: [3, 3], TODAY: [0, 0], @@ -384,7 +387,7 @@ export const SHEET_ONLY_FNS: ReadonlySet = new Set([ "SUMIF", "SUMIFS", "COUNTIF", "COUNTIFS", "AVERAGEIF", "AVERAGEIFS", "MAXIFS", "MINIFS", "SUMPRODUCT", "VLOOKUP", "HLOOKUP", "XLOOKUP", "INDEX", "MATCH", "ROWS", "COLUMNS", "RANK", "PERCENTILE", "QUARTILE", "LARGE", "SMALL", "COUNTBLANK", - "ROW", "COLUMN", + "ROW", "COLUMN", "ISFORMULA", ]); /** @@ -395,7 +398,7 @@ export const SHEET_ONLY_FNS: ReadonlySet = new Set([ * to supply, and there is no range that would help. ROW and COLUMN want an address, so the * sentence they produce says address. */ -const ADDRESS_ONLY_FNS: ReadonlySet = new Set(["ROW", "COLUMN"]); +const ADDRESS_ONLY_FNS: ReadonlySet = new Set(["ROW", "COLUMN", "ISFORMULA"]); export type ParseResult = | { ok: true; ast: FormulaAst; refs: string[] } @@ -1400,6 +1403,25 @@ function evalNode( const at = hay.indexOf(needle, start - 1); return at < 0 ? 0 : at + 1; } + case "SEARCH": case "SEARCHB": { + const within = Array.from(asText(ev(n.args[1]))); + const startValue = n.args.length > 2 ? strictNum(ev(n.args[2]), diag) : 1; + const start = startValue === null ? 0 : Math.trunc(startValue); + if (start < 1 || start > within.length + 1) { diag.push("bad_value"); return null; } + const needle = Array.from(asText(ev(n.args[0]))); + let pattern = ""; + const literal = (ch: string) => ch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + for (let i = 0; i < needle.length; i++) { + const ch = needle[i]; + if (ch === "~" && "?*~".includes(needle[i + 1] ?? "") && i + 1 < needle.length) { + pattern += literal(needle[++i]); + } else pattern += ch === "*" ? ".*?" : ch === "?" ? "." : literal(ch); + } + const subject = within.slice(start - 1).join(""); + const found = new RegExp(pattern, "isu").exec(subject); + if (!found) { diag.push("bad_value"); return null; } + return start + Array.from(subject.slice(0, found.index)).length; + } case "REGEX_EXTRACT": { // REGEX_EXTRACT(text, pattern) -> the FIRST WHOLE MATCH, blank when // the pattern does not match. (Airtable returns the whole match, not @@ -1750,13 +1772,17 @@ function evalNode( // deliberately, which is Excel's rule for the operator, so EXACT is the only way to // ask the strict question and it must not be routed through the same helper. return asText(ev(n.args[0])) === asText(ev(n.args[1])); - case "ISNUMBER": + case "ISNUMBER": { // ⚠ THE VALUE'S TYPE DECIDES, NOT A COERCION. ISNUMBER("12") is FALSE in Excel: the // cell holds text that looks like a number, and saying otherwise would make the // function useless for the one job it has, which is telling those two apart. - return typeof ev(n.args[0]) === "number"; - case "ISTEXT": - return typeof ev(n.args[0]) === "string"; + const local: string[] = []; + return typeof evalNode(n.args[0], get, env, local) === "number" && local.length === 0; + } + case "ISTEXT": { + const local: string[] = []; + return typeof evalNode(n.args[0], get, env, local) === "string" && local.length === 0; + } } // The 24 range-and-address functions never arrive here: `badPattern` refuses them at // authoring time. If one somehow does, blank is the engine's one rule, never a throw. diff --git a/web/src/customer-grid/useCustomerData.ts b/web/src/customer-grid/useCustomerData.ts index 6551ddb7d33845ec17d427e23277bb12880ac5f4..c310f20a0994abf55e93b91b3c1f31cdb46c5ba7 100644 --- a/web/src/customer-grid/useCustomerData.ts +++ b/web/src/customer-grid/useCustomerData.ts @@ -45,7 +45,7 @@ import { subscribeChanges, readPageSize, } from "./apiBridge"; -import type { PersonalGridLayout, RowWindowRequest } from "./apiBridge"; +import type { PersonalGridLayout, RowWindowRequest, WorkspaceStatus } from "./apiBridge"; import { DATA_ERROR_EVENT, DERIVED_CELLS_EVENT, ROWS_STALE_EVENT, WORKSPACE_STALE_EVENT } from "../apiContract"; import { planChangeReaction, rowsRefetchDelay } from "./liveWorkspace"; import { signal } from "../apiContract"; @@ -136,6 +136,8 @@ function withWorkspace(payload: CustomersPayload, ws: GridWorkspace, if (adoptFields && Array.isArray(ws.fields) && ws.fields.length > 0 && JSON.stringify(ws.fields) !== JSON.stringify(payload.fields)) out.fields = ws.fields; + // Offers and saved predicates are metadata. Row values remain the row response's authority + // on initial loads; a failed metadata refresh removes these offers together with the workspace. if (Array.isArray(ws.measures)) out.measures = ws.measures; if (ws.measureSets && typeof ws.measureSets === "object") out.measureSets = ws.measureSets; // ⭐⭐ WAVE 31 · T22 — THE WORKSPACE'S OWN LIMITS REACH THE STRIP THAT RENDERS LIMITS. @@ -161,7 +163,7 @@ function withWorkspace(payload: CustomersPayload, ws: GridWorkspace, if (Array.isArray(ws.userOptions)) out.userOptions = ws.userOptions; if (Array.isArray(ws.permissionUserOptions)) out.permissionUserOptions = ws.permissionUserOptions; const derived = ws.derived; - if (derived && typeof derived === "object" && Object.keys(derived).length > 0) { + if (adoptFields && derived && typeof derived === "object" && Object.keys(derived).length > 0) { out.rows = payload.rows.map((r) => { const cells = derived[String(r.pid)]; return cells ? { ...r, ...cells } : r; @@ -233,6 +235,9 @@ export interface CustomerData { rawRows: Row[]; payload: CustomersPayload | null; loading: boolean; + workspacePending: boolean; + workspaceError: string | null; + retryWorkspace: () => void; windowPending: boolean; /** pid -> partial overlay of user-authored edits, layered OVER the fetched row. */ overlayEdits: OverlayEdits; @@ -372,12 +377,16 @@ export function useCustomerData( const scopedOverlayCache = useRef(new Map()); const dataScopeRef = useRef(scope); const dataRef = useRef(data); + // Only successful row reads authorize cells after a metadata failure. Cached metadata + // must not bring back a field or derived value that the current row wall omitted. + const rowAuthorityRef = useRef(null); if (dataScopeRef.current !== scope) { const previousScope = dataScopeRef.current; if (data) scopedDataCache.current.set(previousScope, data); scopedOverlayCache.current.set(previousScope, overlayEdits); dataScopeRef.current = scope; + rowAuthorityRef.current = null; windowSeq.current += 1; setWindowPending(false); inflightOffset.current = null; @@ -396,15 +405,20 @@ export function useCustomerData( if (data) scopedDataCache.current.set(scope, data); scopedOverlayCache.current.set(scope, overlayEdits); }, [scope, data, overlayEdits, suppliedPayload]); - /** Item 3 (3c) — the LAST scope whose first paint has already been attempted. See the - * `firstPaint` note in the load effect: this is what keeps a workspace failure loud on - * arrival and silent on every refetch afterwards. */ + /** The last opened scope. Each open owns one initial read or joined warm revalidation; + * rowsBump re-runs retain that ownership. Failures always use scoped metadata status. */ const loudScope = useRef(null); // One refresh per open, retained across rowsBump effect re-runs. A cold open must not // become a warm open just because its own workspace request has filled the memo. const workspaceOpen = useRef<{ cached: boolean; refresh: Promise | null | false; } | null>(null); + const [workspaceRead, setWorkspaceRead] = useState<{ + scope: SurfaceScope; pending: boolean; error: string | null; + }>({ scope, pending: includeWorkspace, error: null }); + const workspaceSeq = useRef(0); + const workspaceRetryRef = useRef<() => void>(() => {}); + const retryWorkspace = useCallback(() => workspaceRetryRef.current(), []); useEffect(() => { if (suppliedPayload || STREAMLIT_COMPONENT) return; // the caller owns refresh const onStale = () => setRowsBump((b) => b + 1); @@ -550,11 +564,23 @@ export function useCustomerData( }; } const openWorkspace = workspaceOpen.current; + if (firstPaint) setWorkspaceRead({ scope, pending: includeWorkspace && !peekWorkspace(scope), error: null }); + + function workspaceStatus(status: WorkspaceStatus, seq: number) { + if (cancelled || workspaceSeq.current !== seq || dataScopeRef.current !== scope) return; + const error = status.state === "error" ? status.message ?? "Saved views could not be loaded." : null; + setWorkspaceRead({ scope, pending: false, error }); + if (error) setData(rowAuthorityRef.current); + } readThroughRef.current = false; async function load() { const seq = ++windowSeq.current; + const metadataSeq = ++workspaceSeq.current; + const rowFailure = (reason: string) => { + if (!cancelled && seq === windowSeq.current) signal(DATA_ERROR_EVENT, reason); + }; let workspace = includeWorkspace ? peekWorkspace(scope) : null; // A workspace that cannot be read at first paint is not "a table with no saved // views" — it is a table whose schema we do not have, and the grid used to fill @@ -571,10 +597,14 @@ export function useCustomerData( // it, switching back to a database whose ROWS were still warm still paid a full // `/workspace` (2,430 ms live for `?scope=customer` on `283b815`) for an envelope that // had not changed. - ? fetchWorkspace(scope, { announceFailure: firstPaint, allowCached: true }) + ? fetchWorkspace(scope, { allowCached: true, + onStatus: (status) => workspaceStatus(status, metadataSeq) }) : Promise.resolve(null); // Record an early answer without making the rows wait for a late one. - void wsPromise.then((ws) => { workspace = ws; }); + void wsPromise.then((ws) => { + if (cancelled || metadataSeq !== workspaceSeq.current) return; + workspace = ws; + }); // A pin selects the universal window reader before any status request. Unpinned // grids retain their existing roster-based choice of window or whole-book reads. const tableKey = topic.key; @@ -592,6 +622,7 @@ export function useCustomerData( refusal.reason = reason; refusal.code = code; }); + if (cancelled || seq !== windowSeq.current) return null; if (book || refusal.reason === undefined) return book; // ⚠ Spelled differently from the roster read above ON PURPOSE: verify_grid_ux's // `read-through-roster-hardcoded` control mutates the FIRST occurrence of that read, and a @@ -601,10 +632,10 @@ export function useCustomerData( if (cancelled || seq !== windowSeq.current) return null; if (reasked.has(tableKey)) { readThroughRef.current = true; - return fetchTableWindow(tableKey, { offset: 0, limit: readPageSize(), related }); + return fetchTableWindow(tableKey, { offset: 0, limit: readPageSize(), related }, rowFailure); } } - signal(DATA_ERROR_EVENT, refusal.reason); + rowFailure(refusal.reason); return null; }; const payload = await (readThrough && tableKey !== null @@ -616,33 +647,38 @@ export function useCustomerData( grid applies later: it is the PREMISE of the tab, so the very first read has to carry it or the tab paints the whole database for one frame and then narrows, which is a wrong answer that happens to be brief. */ - ? fetchTableWindow(tableKey, { offset: 0, limit: readPageSize(), related }) + ? fetchTableWindow(tableKey, { offset: 0, limit: readPageSize(), related }, rowFailure) // A related read always takes the pinned window door, even with an empty roster. : wholeBookOrWindow()); if (cancelled || seq !== windowSeq.current) return; setWindowPending(false); if (!payload) return; + rowAuthorityRef.current = payload; + if (metadataSeq !== workspaceSeq.current) workspace = null; // ⚠ `false`: this call has just fetched `payload` FROM THE SERVER, and the workspace // beside it may be a memo hit. See `withWorkspace`'s `adoptFields` note — letting the // cached list win here is what kept a newly created column off the screen until reload. setData(workspace ? withWorkspace(payload, workspace, false) : payload); if (!workspace) void wsPromise.then((ws) => { - if (cancelled || seq !== windowSeq.current || !ws) return; + if (cancelled || seq !== windowSeq.current || metadataSeq !== workspaceSeq.current || !ws) return; setData((prev) => (prev ? withWorkspace(prev, ws, false) : prev)); }); // The memo has already painted. Revalidate once for this open, joining any newer // flight. Re-runs subscribe to that same answer rather than stacking refreshes. if (includeWorkspace && openWorkspace.cached) { - if (openWorkspace.refresh === null) { - const refresh = fetchWorkspace(scope, { allowCached: true, refresh: true }); + if (openWorkspace.refresh !== false) { + setWorkspaceRead((previous) => ({ scope, pending: true, + error: previous.scope === scope ? previous.error : null })); + const refresh = fetchWorkspace(scope, { allowCached: true, refresh: true, + onStatus: (status) => workspaceStatus(status, metadataSeq) }); openWorkspace.refresh = refresh; // Remember completion, not its answer. Replaying a settled refresh on a later // rowsBump could overwrite the workspace that a subsequent write just reread. - void refresh.then(() => { openWorkspace.refresh = false; }); + void refresh.then(() => { if (openWorkspace.refresh === refresh) openWorkspace.refresh = false; }); } if (openWorkspace.refresh) void openWorkspace.refresh.then((ws) => { - if (cancelled || seq !== windowSeq.current || !ws) return; - setData((prev) => (prev ? withWorkspace(prev, ws) : prev)); + if (cancelled || seq !== windowSeq.current || metadataSeq !== workspaceSeq.current || !ws) return; + setData((prev) => (prev ? withWorkspace(prev, ws, false) : prev)); }); } } @@ -658,10 +694,15 @@ export function useCustomerData( // re-read too — no pool refetch. async function reread() { if (!includeWorkspace) return; - const ws = await fetchWorkspace(scope); - if (cancelled || !ws) return; // absent stays absent — never blank a live panel + const seq = ++workspaceSeq.current; + const rowsSeq = windowSeq.current; + setWorkspaceRead((previous) => ({ scope, pending: true, + error: previous.scope === scope ? previous.error : null })); + const ws = await fetchWorkspace(scope, { onStatus: (status) => workspaceStatus(status, seq) }); + if (cancelled || seq !== workspaceSeq.current || rowsSeq !== windowSeq.current || !ws) return; setData((prev) => (prev ? withWorkspace(prev, ws) : prev)); } + workspaceRetryRef.current = () => { void reread(); }; /** * ⭐ owner item 2 (2026-08-03) — THE SHORTCUT. The events response can carry the derived * cells a write just made computable (a new measure column's numbers), which lands them a @@ -704,6 +745,7 @@ export function useCustomerData( return () => { cancelled = true; + workspaceRetryRef.current = () => {}; if (typeof window !== "undefined") { window.removeEventListener(WORKSPACE_STALE_EVENT, onStale); window.removeEventListener(DERIVED_CELLS_EVENT, onDerived); @@ -816,10 +858,13 @@ export function useCustomerData( /* ⭐⭐ W51-T06 / C6 — THE PAGE CARRIES THE PIN. `related` IS IN THE DEPENDENCY LIST BELOW FOR THIS ONE LINE: left out, every page after the first would be the WHOLE table under a `total` that still described the pinned set. */ - void fetchTableWindow(scope, { ...req, related, offset }).then((next) => { + void fetchTableWindow(scope, { ...req, related, offset }, (reason) => { + if (seq === windowSeq.current) signal(DATA_ERROR_EVENT, reason); + }).then((next) => { if (seq !== windowSeq.current) return; setWindowPending(false); if (!next) return; + rowAuthorityRef.current = next; setData((prev) => mergeWindow(prev, next, 0)); }).catch(() => { if (seq === windowSeq.current) setWindowPending(false); @@ -833,6 +878,7 @@ export function useCustomerData( const seq = ++windowSeq.current; void fetchAssignedCustomerRows(viewId).then((next) => { if (!next || seq !== windowSeq.current) return; + rowAuthorityRef.current = next; setData((prev) => { if (prev && assignedBaseRows.current === null) assignedBaseRows.current = prev.rows; return prev ? { ...prev, fields: next.fields, rows: next.rows, today: next.today ?? prev.today } : next; @@ -846,6 +892,7 @@ export function useCustomerData( if (!base) return; assignedBaseRows.current = null; ++windowSeq.current; // a late assigned response must not re-apply after leaving it + if (rowAuthorityRef.current) rowAuthorityRef.current = { ...rowAuthorityRef.current, rows: base }; setData((prev) => prev ? { ...prev, rows: base } : prev); }, [suppliedPayload]); @@ -864,6 +911,11 @@ export function useCustomerData( rawRows: visibleData?.rows ?? [], payload: visibleData, loading: visibleData === null, + workspacePending: includeWorkspace && !suppliedPayload + && (workspaceRead.scope !== scope || workspaceRead.pending), + workspaceError: includeWorkspace && !suppliedPayload && workspaceRead.scope === scope + ? workspaceRead.error : null, + retryWorkspace, windowPending, overlayEdits: suppliedPayload ? EMPTY_OVERLAY_EDITS : overlayEdits, setOverlayEdits: suppliedPayload ? ignoreOverlayEdits : setOverlayEdits, diff --git a/web/src/filter-kit/FieldsHidePanel.tsx b/web/src/filter-kit/FieldsHidePanel.tsx index 406e0a2c5d58ad14db1505a31eff8f5c44247987..db8215e9334dc0a7d8d3c9f04aab988632bf577b 100644 --- a/web/src/filter-kit/FieldsHidePanel.tsx +++ b/web/src/filter-kit/FieldsHidePanel.tsx @@ -14,6 +14,8 @@ // --------------------------------------------------------------------------- import { useEffect, useMemo, useRef, useState } from "react"; +import { useEscapeClose } from "../ui/useEscapeClose"; +import { useOverlayLayer } from "../customer-grid/OverlaySurface"; // ⭐⭐ W46-T31 (contract C6) — the lane's own sheet, imported from the COMPONENT and never from // `index.css`. An @imported sheet is inlined at the TOP of the bundle's cascade and loses every // equal-specificity fight with the rules below it; a sheet a component imports lands after @@ -74,6 +76,7 @@ import { mayRemoveField, originBadge, permRuleWarningTitle, + supersededNoteOf, FIELD_SECTION_TITLES, LOCKED_HIDE_REFUSAL, PRESET_BADGE_TITLE, @@ -352,7 +355,7 @@ export function brokenFieldReason( const FIELD_MENU_WIDTH = 168; const FIELD_MENU_HEIGHT = 76; -function FieldMetaIcon({ detail, kind }: { detail: string; kind: "origin" | "audience" | "owner" | "edit" | "lock" }) { +function FieldMetaIcon({ detail, kind }: { detail: string; kind: "origin" | "audience" | "owner" | "edit" | "lock" | "authority" }) { const shape = kind === "origin" ? <> // ⭐⭐ W46-T31 (owner instruction 21) — THE EDIT-AUDIENCE BADGE STOPS WEARING THE EDIT PENCIL. @@ -364,6 +367,8 @@ function FieldMetaIcon({ detail, kind }: { detail: string; kind: "origin" | "aud // neighbours: the diamond (origin), the padlock (lock), the person bust (owner/audience) and // the pencil itself. The BLURB in `aria-label`/`data-meta-detail` is what still distinguishes // the three edit modes; the glyph only has to stop lying about which control it is. + : kind === "authority" + ? <> : kind === "edit" ? <> : kind === "lock" @@ -394,10 +399,18 @@ export function FieldDeleteImpactDialog({ field, onCancel, onConfirm }: { onConfirm: () => void; }) { const [typedName, setTypedName] = useState(""); + const panelRef = useRef(null); + useOverlayLayer({ + panelRef, + onDismiss: onCancel, + dismissOnOutside: false, + initialFocus: ".cg-input", + trapFocus: true, + }); const name = rowLabelOf(field); const usage = fieldUsageOf(field); return
-
+
Delete {name}?
This permanently removes the field and its values.
{usage &&
    @@ -407,7 +420,7 @@ export function FieldDeleteImpactDialog({ field, onCancel, onConfirm }: { {usage.automations > 0 &&
  • {usage.automations} {usage.automations === 1 ? "automation" : "automations"}
  • } {usage.permRules > 0 &&
  • {usage.permRules} permission {usage.permRules === 1 ? "rule" : "rules"}
  • }
} - +
; @@ -503,14 +516,11 @@ export function FieldsHidePanel({ menuTriggerRef.current = null; if (restoreFocus) trigger?.focus(); }; + useEscapeClose(menuKey !== null, () => closeFieldMenu(true), { + container: () => menuRef.current, + }); useEffect(() => { if (menuKey === null) return; - const onKey = (e: KeyboardEvent) => { - if (e.key !== "Escape") return; - // Stop the panel's own overlay layer from closing the WHOLE Fields panel on the same key. - e.stopPropagation(); - closeFieldMenu(true); - }; const onDown = (e: Event) => { const target = e.target as Node | null; if (!target) return; @@ -521,11 +531,9 @@ export function FieldsHidePanel({ // ⚠ CAPTURE, AND `scroll` IS NOT OPTIONAL. The menu is viewport-positioned, so scrolling the // field list would leave it hovering over an unrelated row still wired to the row it opened on. const onScroll = () => closeFieldMenu(false); - document.addEventListener("keydown", onKey, true); document.addEventListener("pointerdown", onDown, true); window.addEventListener("scroll", onScroll, true); return () => { - document.removeEventListener("keydown", onKey, true); document.removeEventListener("pointerdown", onDown, true); window.removeEventListener("scroll", onScroll, true); }; @@ -849,6 +857,7 @@ export function FieldsHidePanel({ const audienceText = fc && audienceBadge(fc); const author = fieldAuthorOf(f); const editBadge = fieldEditBadge(f); + const supersededNote = supersededNoteOf(f); // ⭐⭐ W41-T28 (R3, R8) - WHERE THIS FIELD IS USED, read off C8's `usage` // block and NEVER counted here. This component can see one user's field list // and nothing else: not the other users' saved views, not the automations, not @@ -1129,6 +1138,7 @@ export function FieldsHidePanel({ : )} {author && } {editBadge && } + {supersededNote && } {/* ⭐ W41-T27 (R5, badge 2 of 3) — AUDIENCE: who can see this column. Sentence case per DESIGN §2 (`Private`, `Shared by Karen`, `Everyone`), worded once in `fieldClass.ts` so the four C1 surfaces cannot drift apart on it. diff --git a/web/src/filter-kit/FilterBuilderPanel.tsx b/web/src/filter-kit/FilterBuilderPanel.tsx index 79ba1c37f3c4156d104d387f84661877eb4fe9d6..c40fda171a33013e3a1a674575b1fcede58b9c34 100644 --- a/web/src/filter-kit/FilterBuilderPanel.tsx +++ b/web/src/filter-kit/FilterBuilderPanel.tsx @@ -300,7 +300,7 @@ function ConditionValue({ ]} searchable searchPlaceholder="Find a choice…" - emptyMessage="No choices yet." + emptyMessage={type === "user" ? "No choices yet." : "values come from data"} onChange={onValue} /> ); @@ -663,7 +663,7 @@ function MultiValuePicker({ // D-236: a column that declares nothing says so in its own words. A search that // matched nothing is a DIFFERENT sentence, because it is a different situation.
- {options.length === 0 ? "No values yet" : "No value matches"} + {options.length === 0 && !needle ? "values come from data" : "No value matches"}
)}
@@ -1552,7 +1552,7 @@ export interface FilterBuilderPanelProps { * (owner item 24, 2026-08-06). `stock_bucket` on the Product grid is declared `select` and * declares no `options` — its vocabulary is computed server-side and rides the rows, exactly * like a `status` column's. **Any column with no declared `options` needs this prop**, whatever - * its type says. A host that cannot enumerate one renders an empty picker; see `choicesFor`. + * its type says. A host that cannot enumerate one says "values come from data" in the picker. */ statusValues?: Record; /** @@ -1627,13 +1627,16 @@ export function FilterBuilderPanel({ }, [measures]); const choicesFor = useCallback( (colId) => { - // A supplied map WINS, including when it holds an empty list: "this status column has no - // values in the data" is an answer, not a missing one. - const declared = statusValues?.[colId]; - if (declared) return declared; + // Schema choices include unused values (odoo_status declares Active and Archived). + // An empty discovery result must not hide that vocabulary. Data-backed fields still + // accept an empty supplied list and disclose where their values come from. const f = fieldByKey.get(colId); + const declared = f ? choiceOptions(f) : []; + if (declared.length) return declared; + const discovered = statusValues?.[colId]; + if (discovered) return discovered; if (!f) return []; - return f.type === "user" ? userOptions ?? [] : choiceOptions(f); + return f.type === "user" ? userOptions ?? [] : []; }, [statusValues, fieldByKey, userOptions] ); diff --git a/web/src/filter-kit/SheetFilterPopup.tsx b/web/src/filter-kit/SheetFilterPopup.tsx index 0cc072b3db561631ddf6561f28c87f880e0d7ea2..fe6192e3bc904c70d8f9978bbb6df81ff6af0352 100644 --- a/web/src/filter-kit/SheetFilterPopup.tsx +++ b/web/src/filter-kit/SheetFilterPopup.tsx @@ -62,7 +62,7 @@ export interface SheetFilterPopupProps { /** The cell this filter belongs to, e.g. `B4`. Named in the title so the dialog says which * cell it is about, the way the binding panel behind it does. */ addr: string; - /** Escape, an outside pointerdown, the close mark, or Done. There is no second, cancelling + /** Escape, the close mark, or Done. There is no second, cancelling * door: see the note about draft state at the top of this file. */ onClose: () => void; fields: Field[]; @@ -193,7 +193,7 @@ export function SheetFilterDialog({ useOverlayLayer({ panelRef, onDismiss: onClose, - dismissOnOutside: true, + dismissOnOutside: false, initialFocus: "first", restoreFocus: true, trapFocus: true, diff --git a/web/src/forms/FormPublic.tsx b/web/src/forms/FormPublic.tsx index ac07981ff1686141e838b88f7966c5c0163a47ca..079c4afc2045c88007f1573f33e0def359b00a2d 100644 --- a/web/src/forms/FormPublic.tsx +++ b/web/src/forms/FormPublic.tsx @@ -21,7 +21,7 @@ // would be a second copy of the tokens. // --------------------------------------------------------------------------- -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; /** One field, exactly as `routes_forms._public_form` builds it. Nothing here is * optional-because-maybe: every key below is one the server always sends, and @@ -37,6 +37,8 @@ interface FormField { required: boolean; options?: string[]; max?: number; + secret?: boolean; + hint?: string; } interface FormSpec { @@ -47,35 +49,49 @@ interface FormSpec { /** The honeypot's field name — planted, never shown. Server-named so the two * halves cannot drift into a trap nobody checks. */ honeypot: string; + kind?: string; + singleUse?: boolean; } -type Phase = "loading" | "ready" | "sent" | "gone"; +type Phase = "loading" | "ready" | "sent" | "gone" | "failed" | "unavailable"; const LONG_TEXT_MIN = 0; // every text field gets a textarea when it is the only one export default function FormPublic({ token }: { token: string }) { + // Changing capabilities unmounts the old inputs and destroys all typed values. + return ; +} + +function PublicForm({ token }: { token: string }) { const [spec, setSpec] = useState(null); const [phase, setPhase] = useState("loading"); const [values, setValues] = useState>({}); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); + const post = useRef(null); useEffect(() => { let live = true; + const controller = new AbortController(); setPhase("loading"); - fetch(`/api/v1/forms/${encodeURIComponent(token)}`) + setSpec(null); + setValues({}); + setError(""); + fetch(`/api/v1/forms/${encodeURIComponent(token)}`, { signal: controller.signal }) .then((r) => (r.ok ? r.json() : Promise.reject(new Error(String(r.status))))) .then((data: FormSpec) => { if (!live) return; setSpec(data); setPhase("ready"); }) - // ⚠ ONE dead-end state for every failure, mirroring the server's uniform 403. Telling a - // visitor "this form is disabled" versus "no such form" would hand an enumerator the - // distinction the whole server-side refusal exists to withhold. - .catch(() => live && setPhase("gone")); + // Invalid capabilities share one expired state; a transport failure proves no expiry. + .catch((failure: unknown) => { + if (live) setPhase(failure instanceof Error && failure.message === "403" ? "gone" : "unavailable"); + }); return () => { live = false; + controller.abort(); + post.current?.abort(); }; }, [token]); @@ -87,28 +103,44 @@ export default function FormPublic({ token }: { token: string }) { if (busy) return; setBusy(true); setError(""); + const controller = new AbortController(); + post.current = controller; fetch(`/api/v1/forms/${encodeURIComponent(token)}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ values }), + signal: controller.signal, }) .then(async (r) => { + if (controller.signal.aborted) return; if (r.ok) { + setValues({}); setPhase("sent"); return; } - // The server's sentence, verbatim — it names the field by the label THIS PAGE showed, - // which is the only name the person has. A generic "something went wrong" here would - // discard the one useful thing the refusal carried. const body = await r.json().catch(() => null); - setError( - String(body?.error?.message || "That could not be sent; check your answers and try again.") - ); + if (controller.signal.aborted) return; + if (body?.error?.code === "data_input_frozen") { + setError(String(body.error.message)); + } else if (!spec?.singleUse) { + setError(String(body?.error?.message || "That could not be sent; check your answers and try again.")); + } else if (r.status === 403) { + setPhase("gone"); + } else if (body?.error?.code === "setup_write_failed") { + setPhase("failed"); + } else { + setError(r.status === 400 ? "Invalid fields" : r.status === 429 ? "Busy" : "Not sent"); + } + }) + .catch(() => { + if (!controller.signal.aborted) setError(spec?.singleUse ? "Not sent" : "That could not be sent; check your connection and try again."); }) - .catch(() => - setError("That could not be sent; check your connection and try again.") - ) - .finally(() => setBusy(false)); + .finally(() => { + if (controller.signal.aborted) return; + if (spec?.singleUse) setValues({}); + setBusy(false); + post.current = null; + }); }; if (phase === "loading") @@ -118,14 +150,13 @@ export default function FormPublic({ token }: { token: string }) { ); - if (phase === "gone") + if (phase === "gone" || phase === "failed" || phase === "unavailable") return (
-

This form is not available

- {/* ONE line (DESIGN.md §4). There is nothing this person can do about it and no - detail we may safely give, so the page does not pretend otherwise. */} -

The link may have expired or been turned off.

+

+ {phase === "failed" ? "Save failed" : phase === "unavailable" ? "Unavailable" : "Expired"} +

); @@ -134,18 +165,19 @@ export default function FormPublic({ token }: { token: string }) { return (
-

Thank you

-

Your response has been recorded.

- +

{spec?.singleUse ? "Saved" : "Submitted"}

+ {!spec?.singleUse && ( + + )}
); @@ -156,7 +188,7 @@ export default function FormPublic({ token }: { token: string }) {

{spec.title || "Form"}

- {spec.desc &&

{spec.desc}

} + {!spec.singleUse && spec.desc &&

{spec.desc}

} {spec.fields.map((f) => ( ))} @@ -184,7 +216,7 @@ export default function FormPublic({ token }: { token: string }) { aria-hidden /> - {error &&

{error}

} + {error &&

{error}

} @@ -201,23 +233,30 @@ function FieldControl({ field, value, onChange, + disabled, }: { field: FormField; value: string; onChange: (key: string, v: string) => void; + disabled: boolean; }) { const common = { className: "lpf-input", value, required: field.required, + disabled, + title: field.hint, onChange: (e: { target: { value: string } }) => onChange(field.key, e.target.value), }; + if (field.secret || field.type === "password") + return ; switch (field.type) { case "checkbox": return ( onChange(field.key, e.target.checked ? "1" : "")} /> @@ -229,7 +268,7 @@ function FieldControl({ {/* An explicit empty option, always. A ` : null}
: null} - {folderTiles.length > 0 ?
- {folderTiles.map((folder) => )} -
: null} + : null} {level.empty ?

This folder is empty.

: null} : null} @@ -934,8 +998,8 @@ export function HomeSurface({ {agentLevel.items.length > 0 ? (

{AGENTS_MODULE_LABEL}

-
- {agentLevel.items.map((row) => ( + + {(pageItems) => pageItems.map((row) => ( +
) : indexKnown && agentsKnown && !agentsError && !starredOnly && !level.current && level.folders.length === 0 ? (

This workspace has no agents yet. Create one above.

@@ -977,8 +1041,8 @@ export function HomeSurface({ ) : sheetsKnown && levelSheets.length > 0 ? (

Sheets

-
) : mode === "sheet" && sheetsKnown && !sheetsError && !starredOnly && !level.current && level.folders.length === 0 ? (

This workspace has no sheets yet. Create one above.

@@ -1176,8 +1240,8 @@ export function HomeSurface({ ) : mode !== "app" && mode !== "sheet" && mode !== "agents" && levelDatabases.length ? (
{homeFolds ? null :

Databases

} -
- {levelDatabases.map((tile) => ( + + {(pageItems) => pageItems.map((tile) => (
{ moduleTileEvents(tile.key).onClick?.(event); openTileLink(event, "database", tile.key); }}> @@ -1190,27 +1254,21 @@ export function HomeSurface({ />
))} -
+
) : null} - {mode === "home" && !homeFolds && !starredOnly && folders.some((folder) => !folder.parent) &&

Folders

} + {mode === "home" && !homeFolds && !starredOnly && folders.some((folder) => !folder.parent) &&

Folders

!folder.parent)} layout={layout} scopeKey={pageScope} label="Folders">{(pageItems) => pageItems.map((folder) => { const count = Object.values(folderPlacement).filter((id) => id === folder.id).length; return {folder.name}{count} database{count === 1 ? "" : "s"}; })}
} {mode !== "data" && mode !== "sheet" && mode !== "agents" && applicationsError ? (

{applicationsError}

) : null} {mode !== "data" && mode !== "sheet" && mode !== "agents" && levelApplications.length > 0 ? (

Applications

-
- {levelApplications.map((application) => { + + {(pageItems) => pageItems.map((application) => { const database = application.openTarget?.database ?? application.compatibleDatabases?.[0] ?? ""; - const kind = application.templateKind === "finance" ? "Finance" : application.templateKind === "content_studio" - ? "Content Studio" : application.templateKind === "catalog" - ? "Catalog" - : application.templateKind === "route" - ? "Routing" - : application.type.slice(0, 1).toUpperCase() + application.type.slice(1); return (
{application.name} - {kind} {application.capabilities?.star !== false ? ( @@ -1261,7 +1318,7 @@ export function HomeSurface({
); })} -
+
) : mode === "app" && applicationsKnown && !applicationsError && !level.current && level.folders.length === 0 ? (

No applications yet. Create one to add it here.

@@ -1303,8 +1360,8 @@ export function HomeSurface({ REQUIRED the defect — [[a-gate-that-pins-a-spelling-not-a-claim]]. Re-typing "Agents" here would have left the next rename in exactly the same trap. */}

{AGENTS_MODULE_LABEL}

-
- {visibleAutomations.map((a) => ( + + {(pageItems) => pageItems.map((a) => (
+
) : null ) : null} @@ -1353,8 +1410,8 @@ export function HomeSurface({ {mode === "home" && starredViews.length ? (

Starred views

-
- {starredViews.map((row) => ( + + {(pageItems) => pageItems.map((row) => ( ))} -
+
) : null} {mode === "home" && starredOnly && starredQueries.length ? (

Starred queries

-
- {starredQueries.map((row) => ( + + {(pageItems) => pageItems.map((row) => ( onOpenQuery(row.id)} /> ))} -
+
) : null} {/* verify_automation_ui.py pins the first two Agents clauses as adjacent, so keep their order. */} @@ -1721,11 +1778,10 @@ export default function HomePage(props: HomeProps) { return () => controller.abort(); }, [props.mode, applicationsEpoch]); - // S67-T41 owner correction: the shared `/applications` doors refuse an App bound to a user - // table, so a Content Studio's star and delete go to its own module's doors (same writers). - const applicationDoor = (application: ApplicationTile) => application.templateKind === "content_studio" - ? `${API_V1}/content-studio/apps/${encodeURIComponent(application.id)}` - : `${API_V1}/applications/${encodeURIComponent(application.id)}`; + // The governed Applications writer now admits readable user tables too. The retired + // content-studio/apps route is unmounted; legacy tiles keep the same permission/name checks. + const applicationDoor = (application: ApplicationTile) => + `${API_V1}/applications/${encodeURIComponent(application.id)}`; const toggleApplication = (application: ApplicationTile, on: boolean) => { if (applicationWrites.current.has(application.id) || deletedApplications.current.has(application.id)) return; applicationWrites.current.add(application.id); diff --git a/web/src/inbox/InboxPage.tsx b/web/src/inbox/InboxPage.tsx index 1cac1c24e9ec5afe4af341018a925d3ab786c6f9..e3716a40ee661634f76141f6bfe9aae57c0566e0 100644 --- a/web/src/inbox/InboxPage.tsx +++ b/web/src/inbox/InboxPage.tsx @@ -47,6 +47,7 @@ import { canOpen, inboxSections, previewOf, + routeForTarget, senderOf, subjectOf, targetOf, @@ -109,6 +110,15 @@ const CARD: React.CSSProperties = { minHeight: 0, }; +function actionLabelOf(notification: Notification): string { + const destination = routeForTarget(targetOf(notification)); + if (destination?.surface === "automation") return "Open the agent"; + if (destination?.surface === "map") return "Open the map"; + if (destination?.surface === "database" && destination.key.startsWith("app?")) + return "Open the App"; + return "Open the database"; +} + export default function InboxPage({ onOpenTarget, onInbox, @@ -447,13 +457,11 @@ export function InboxSurface({

) : view === "error" ? (

- These notifications could not be loaded. The count beside the bell is still this - account’s — “Mark all read” above clears it. + Notifications could not be loaded.

) : view === "empty" ? (

- Nothing new. An alert watches ONE view and tells you when a record it had never - matched arrives in it — make one from a view’s menu. + Nothing new.

) : ( <> @@ -560,17 +568,7 @@ export function InboxSurface({ className="login-submit" onClick={() => onOpen(selected)} > - {/* Item 19 (T29): the verb names the DESTINATION rather than promising - "open" generically — two kinds go two places. - ⛔ WAVE 34 · R12 — THIS COMPARED A DISPLAY STRING, AND THAT IS WHY IT IS - WRITTEN THIS WAY NOW. It read `kindLabel(selected.kind) === "Automation"`: - a second copy of a caption, in a file the rename had no reason to open. The - moment `kindLabel` returned "Agents" the test went false in silence and - every automation notification offered to open a DATABASE. Compare the KIND, - which is a stored wire value nobody renames for a caption. */} - {selected.kind === NOTIF_KIND_AUTOMATION - ? "Open the agent" - : "Open the database"} + {actionLabelOf(selected)} ) : ( /* ⛔ SAID ON THE ITEM, NOT ON THE CLICK. The pane raised this as a toast AFTER diff --git a/web/src/index.css b/web/src/index.css index 22311dacdd71f53eb511b70cfbaf852594ad11fd..07d1283946f78d5c4cd2a31ea9dadf4344b59ff5 100644 --- a/web/src/index.css +++ b/web/src/index.css @@ -63,6 +63,9 @@ `-deep` weight; flat surfaces take the pastel base. */ --cg-navy: var(--lp-blue-solid); --cg-gold: var(--lp-yellow-deep); + /* Maintenance colors mirror the existing canvas NAVY and GOLD in theme.ts. */ + --lp-freeze-navy: #1F4E78; + --lp-freeze-gold: #C8A24B; --cg-ink: var(--lp-ink); --cg-text: var(--lp-ink); --cg-muted: var(--lp-muted); @@ -284,7 +287,7 @@ ⚠ The comment above still stands and is not contradicted: the brand purple is deliberately OUTSIDE the UI palette. This token does not admit it to the palette; it names it so the ONE surface that is the brand mark can use it. */ - --lp-brand: #6e56cf; /* the loop mark (favicon.svg, _brandmark) */ + --lp-brand: #6e56cf; /* the rope in the two Loopable SVG sources */ /* --- Wave-15 C-BOLD (owner ruling R7) — THE PRIMARY ACTION ----------------- "Key buttons bold (dark purple)". A THIRD role in the palette, and it is @@ -3418,13 +3421,40 @@ body > .cg-overlay { border-radius: 50%; animation: lp-spin-rot 0.7s linear infinite; } -/* 20px for a surface with nothing else on it (a whole page mid-load) — the - 14px mark is sized to sit BESIDE things, and alone on an empty panel it reads - as dust. Border weight rises with it, or the ring goes spindly. */ +/* SDLC 70 T03 (R25, R27, DESIGN.md 4.3): a surface with nothing else on it (a + whole page mid-load) and the boot screen show the whole heart knot mark, which + BREATHES and never rotates. The small `.lp-spin` ring beside things is not + touched. Only transform and opacity animate. */ .lp-spin--lg { - width: 20px; - height: 20px; - border-width: 2.5px; + width: var(--lp-logo-loader-size); + height: var(--lp-logo-loader-size); + border: 0; + border-radius: 0; + background: url("/favicon.svg") center / contain no-repeat; + animation: lp-logo-breathe var(--lp-logo-loader-duration) var(--lp-logo-loader-easing) infinite; +} +.shell-boot .lp-mark { + width: var(--lp-logo-loader-size); + height: var(--lp-logo-loader-size); + animation: lp-logo-breathe var(--lp-logo-loader-duration) var(--lp-logo-loader-easing) infinite; +} +@keyframes lp-logo-breathe { + 0%, 100% { + transform: scale(var(--lp-logo-loader-scale-rest)); + opacity: var(--lp-logo-loader-opacity-rest); + } + 50% { + transform: scale(var(--lp-logo-loader-scale-peak)); + opacity: var(--lp-logo-loader-opacity-peak); + } +} +@media (prefers-reduced-motion: reduce) { + .lp-spin--lg, + .shell-boot .lp-mark { + animation: none; + transform: scale(var(--lp-logo-loader-scale-rest)); + opacity: var(--lp-logo-loader-opacity-peak); + } } @keyframes lp-spin-rot { to { transform: rotate(360deg); } @@ -3433,7 +3463,7 @@ body > .cg-overlay { calmer spinner, it is a broken one — the user loses the only signal that the app is still alive. Stepped so there is no continuous movement to track. */ @media (prefers-reduced-motion: reduce) { - .lp-spin { animation: lp-spin-rot 2.4s steps(8, end) infinite; } + .lp-spin:not(.lp-spin--lg) { animation: lp-spin-rot 2.4s steps(8, end) infinite; } } /* ---- shell (wave 4, 2026-07-27; EXIT wave 1 door + nav, 2026-07-30) -------- @@ -3454,9 +3484,9 @@ body > .cg-overlay { as much as to the login card, so the current row is marked with the same blue the grid marks a selected row with. */ -/* --- the Loopable lockup: shared by the login card and the sidebar --------- - The MARK itself is an pointing at the generated favicon.svg — see - shell/Brand.tsx for why there is no hand-drawn copy here. */ +/* --- the Loopable lockup: shared by sign-in and navigation ----------------- + Mark is an consuming favicon.svg. The two byte-identical SVG sources + carry the heart knot the owner selected 2026-10-05; see shell/Brand.tsx. */ .lp-mark { display: block; flex: 0 0 auto; } .lp-wordmark { color: var(--lp-ink); white-space: nowrap; } @@ -3467,18 +3497,27 @@ body > .cg-overlay { overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; } -/* ---- X5: the branded door ------------------------------------------------- - The card language is app.py `gate()`'s: 410px, ONE hairline closing all four - sides, the wave-9 I5 lockup (mark + wordmark side by side, SET weight and - tight tracking — wave-9 I4 superseded the wave-8 250-weight treatment), two - fields, one primary button. */ +/* ---- X5: the branded door (DESIGN.md §4.5 / C1) --------------------------- + One form column on the page surface, with the shared Loopable lockup. */ :root { - /* The lift shared by the login card and the toast. A NAMED token, like every - other colour here — an inline rgba() in a box-shadow is still an inline - colour, and it is the one an anti-slop sweep always misses. Tuned to - `gate()`'s --shadow-overlay. Declared on :root, not on the card, because - the toast is a sibling of the frame rather than a descendant of it. */ + /* Overlay shadow retained for the toast; sign-in has no raised panel. */ --shell-shadow-overlay: 0 1px 2px rgba(32, 36, 51, 0.05), 0 8px 24px rgba(32, 36, 51, 0.07); + /* DESIGN.md §5.1: DOM-only sign-in declarations. The canvas has no + consumer of this geometry, so theme.ts keeps its shared palette/type. */ + --lp-content-inset: 24px; + --lp-section-gap: 24px; + --lp-content-border-width: 0; + --lp-content-shadow: none; + --lp-fw-semibold: 600; + --lp-login-form-max-width: 400px; + --lp-login-control-height: 40px; + --lp-logo-loader-size: 56px; + --lp-logo-loader-duration: 1600ms; + --lp-logo-loader-easing: ease-in-out; + --lp-logo-loader-scale-rest: 1; + --lp-logo-loader-scale-peak: 1.04; + --lp-logo-loader-opacity-rest: 0.7; + --lp-logo-loader-opacity-peak: 1; } .login-root, @@ -3491,54 +3530,70 @@ body > .cg-overlay { color: var(--lp-ink); background: var(--lp-surface); } -.login-root { align-items: flex-start; padding: 12vh 20px 40px; } +.login-root { + flex-direction: column; + justify-content: flex-start; + align-items: center; + padding: 0 20px 40px; +} +.login-root > .login-card { margin-top: 12vh; } .shell-boot { align-items: center; } +.freeze-page { display: flex; flex-direction: column; min-height: 100vh; } +.freeze-page > .shell-boot { flex: 1; min-height: 0; } +.freeze-notice-band { + display: flex; + flex: 0 0 auto; + justify-content: flex-end; + width: 100%; + padding: 12px 16px; +} +.login-root > .freeze-notice-band { padding: 12px 0; } +.freeze-notice { + display: block; + margin-left: auto; + max-width: 100%; + padding: 4px 8px; + border: 1px solid var(--lp-freeze-gold); + border-radius: var(--lp-r-sm); + color: var(--lp-freeze-navy); + background: var(--lp-surface); + font-family: "Inter", system-ui, sans-serif; + font-size: var(--lp-fs-2xs); + line-height: 1.4; + overflow-wrap: anywhere; + text-align: right; +} + .login-card { + --login-control-h: var(--lp-login-control-height); width: 100%; - max-width: 410px; + max-width: var(--lp-login-form-max-width); background: var(--lp-surface); - border: 1px solid var(--lp-line); - border-radius: 8px; - padding: 26px 28px 18px; - box-shadow: var(--shell-shadow-overlay); + border: var(--lp-content-border-width); + padding: var(--lp-content-inset); + box-shadow: var(--lp-content-shadow); +} +.login-brand { + display: flex; + align-items: center; + gap: calc(var(--lp-content-inset) / 2); + margin-bottom: var(--lp-section-gap); } -.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; } .login-brand .lp-wordmark { font-size: var(--lp-fs-lg); - font-weight: 600; + font-weight: var(--lp-fw-semibold); letter-spacing: -0.02em; line-height: 1.15; } -/* ⭐ WAVE 34 R9 — the sign-in card's one line of sell. - ⚠ IT MUST NOT COMPETE WITH THE WORDMARK. `--lp-fs-sm` and the muted ink put it a clear step - below the brand line, which is what keeps the card reading as a door rather than a landing - page. The 18px bottom margin replaces `.login-brand`'s 12px as the gap before the form, so the - form does not move when this line is present: measured from the same total, not added to it. - ⛔ NO gold accent line and no rule above the form. The owner rejected exactly that once already - ("there's an orange line at the top, it's ugly"), and a slogan is the most tempting place to - reach for one. */ -/* ⚠ `--lp-muted`, NOT `--lp-ink-muted`. The first draft of this rule used the second name, which - does not exist -- and a ghost token does not fail loudly, it paints the INHERITED colour, so the - tagline would have rendered at full `--lp-ink` weight and competed with the wordmark. The token - block above carries a scar from the same class (`--lp-wash: var(--lp-wash)`, a self-reference - that silently painted nothing everywhere it was consumed) and notes that the ghost-token gate - only checks a consumed name is DECLARED. Measured: `--lp-muted` #6b7280 is 4.83:1 on white. */ -.login-tagline { - margin: 0 0 18px; - font-size: var(--lp-fs-sm); - color: var(--lp-muted); - letter-spacing: -0.005em; -} - /* ---- the 8-STATE INPUT DISCIPLINE ---------------------------------------- rest · placeholder · hover · focus · filled · error · disabled · autofill. border-WIDTH is 1px in EVERY state — state is carried by colour, background and the focus OUTLINE, never by a width change (a 1px→2px focus border moves every pixel below it). The control height is ONE token, shared with the button, so the two can never drift apart. */ -.login-form { --login-control-h: 38px; } +.login-form { --login-control-h: var(--lp-login-control-height); } /* FILLED-FIELD language, matched to the other door: the host's inputs are a quiet grey fill with no visible border (BaseWeb over secondaryBackground), @@ -3658,6 +3713,13 @@ body > .cg-overlay { where it measures 7.71:1 — visible against the surround, not against the button it would otherwise disappear into. */ .login-submit:focus-visible { outline: 2px solid var(--lp-primary); outline-offset: 2px; } +/* §4.5 keeps sign-in actions on the shared field focus color and button + radius. Outrank the older MFA card-radius rule without changing its file. */ +.login-card .login-form .login-submit { + border-radius: var(--lp-r-md); + font-weight: var(--lp-fw-semibold); +} +.login-card .login-submit:focus-visible { outline-color: var(--lp-blue-deep); } .login-submit:disabled { opacity: 0.6; cursor: not-allowed; filter: none; } /* Reserved in the layout ALWAYS, so an arriving error never shoves the card. @@ -3703,6 +3765,16 @@ body > .cg-overlay { background: var(--lp-surface); border-bottom: 1px solid var(--lp-line); } +.shell-topbar:has(.freeze-notice) { + flex: 0 0 auto; + height: auto; + min-height: var(--lp-rail-head-h); + flex-wrap: wrap; + row-gap: 4px; + padding-top: 8px; + padding-bottom: 8px; +} +.shell-topbar > .freeze-notice { flex: 0 1 auto; } /* ⛔ `min-height: 0` is load-bearing, not tidiness. A flex ITEM defaults to `min-height: auto`, which refuses to shrink below its content — so without this the rail's own scroll box and the grid host would size to their content and push the page past 100vh instead of scrolling inside @@ -6925,6 +6997,9 @@ a.cg-map-ctl-b { text-decoration: none; } font-size: var(--lp-fs-md); line-height: 1; cursor: pointer; + /* Above the positioned panes in `.set-body` (`.set-perms` is `position: relative` and comes + later), or the close control is visible and every click lands on the pane. */ + z-index: 2; } .set-close:hover { background: var(--lp-surface-2); color: var(--lp-ink); } .set-close:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: 1px; } @@ -11921,11 +11996,30 @@ a.cg-map-ctl-b { text-decoration: none; } no layout width, so the measured delta there is 0 while the owner sees ~15px on Windows Chrome. What the headless run DOES prove is the mechanism — the scroller identity flips between the two layouts. */ +:root { + /* DESIGN.md §5.1: DOM-only home geometry and feedback. No canvas consumers; + the content tokens already declared for sign-in are shared with these homes. */ + --lp-r-home-tile: 12px; + --lp-home-tile-inset: 16px; + --lp-home-tile-shadow: 0 1px 2px color-mix(in srgb, var(--lp-ink) 6%, transparent), 0 4px 12px color-mix(in srgb, var(--lp-ink) 4%, transparent); + --lp-home-tile-shadow-hover: 0 2px 4px color-mix(in srgb, var(--lp-ink) 7%, transparent), 0 8px 20px color-mix(in srgb, var(--lp-ink) 6%, transparent); + --lp-home-tile-lift: -2px; + --lp-home-disabled-opacity: 0.45; + --lp-motion-feedback-duration: 160ms; + --lp-motion-feedback-easing: ease-out; + --lp-hairline-width: 1px; + --lp-home-tile-min-width: 208px; + --lp-home-grid-gap: 16px; + --lp-home-page-rows: 2; + --lp-home-pagination-inset: 16px; + --lp-home-pagination-gap: 4px; + --lp-home-pagination-control-size: 32px; +} .shell-home { height: 100%; overflow-y: auto; scrollbar-gutter: stable; - padding: 34px 44px 56px; + padding: var(--lp-content-inset); box-sizing: border-box; /* W62 instruction 3, R4/R21: the wash is chrome and recedes; cards stay white. */ background: var(--lp-wash); @@ -11942,8 +12036,8 @@ a.cg-map-ctl-b { text-decoration: none; } gap: 12px; margin-bottom: 28px; } -/* One containment layer, hairlines not shadows (DESIGN.md §4). The card is a button and resets - the UA's chrome explicitly — the wave-19 "mystery outer lines and tinted fill" rule. */ +/* Shared quick-start anatomy. Module homes apply DESIGN.md §4.1 below. + The button resets native font and color along with its surface. */ .home-card { display: flex; flex-direction: column; @@ -11975,6 +12069,12 @@ a.cg-map-ctl-b { text-decoration: none; } stroke-linecap: round; stroke-linejoin: round; } +/* Create new uses primary CTA paint; shared glyphs keep their rail styling elsewhere. */ +.home-card .home-app-icon, +.home-card .shell-nav-icon { + stroke: var(--lp-primary); + opacity: 1; +} .home-card-title { font-size: var(--lp-fs-xs); font-weight: 600; @@ -12077,6 +12177,47 @@ a.cg-map-ctl-b { text-decoration: none; } .home-tiles { display: grid; gap: 10px; } .home-tiles.is-grid { grid-template-columns: repeat(auto-fill, minmax(258px, 1fr)); } .home-tiles.is-list { grid-template-columns: 1fr; } +.home-tiles.home-paged-grid { gap: var(--lp-home-grid-gap); } +.home-paged-grid.is-grid { + grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--lp-home-tile-min-width)), 1fr)); +} +/* S70-T06 / C1: quiet numbered pages below the grid, with native keyboard focus. */ +.home-pagination { + display: flex; + flex-wrap: wrap; + justify-content: center; + gap: var(--lp-home-pagination-gap); + margin-top: var(--lp-home-pagination-inset); +} +.home-pagination-control { + border: 0; + background: transparent; + font: inherit; + color: inherit; + display: inline-flex; + align-items: center; + justify-content: center; + box-sizing: border-box; + min-width: var(--lp-home-pagination-control-size); + height: var(--lp-home-pagination-control-size); + padding: 0 var(--lp-home-pagination-gap); + border-radius: var(--lp-r-md); + font-size: var(--lp-fs-xs); + color: var(--lp-muted); + font-variant-numeric: tabular-nums; + cursor: pointer; +} +.home-pagination-control[aria-current="page"] { + background: var(--lp-primary-tint); + color: var(--lp-primary); +} +.home-pagination-control:focus-visible { + outline: calc(var(--lp-hairline-width) + var(--lp-hairline-width)) solid var(--lp-blue-deep); + outline-offset: var(--lp-hairline-width); +} +@media (hover: hover) and (pointer: fine) { + .home-pagination-control:hover:not([aria-current="page"]) { background: var(--lp-surface-2); } +} /* ⭐ WAVE-33 T10 (owner item 15) — ONE CLASS, TWO TAGS, and that is the whole defect. A database tile is an `` (HomePage.tsx) and an automation tile is a ` -
+ ) : null} {view.description && !(editing && editing.id === view.id) ? (

{view.description}

diff --git a/web/src/settings/ExternalAccessPane.tsx b/web/src/settings/ExternalAccessPane.tsx index dfc462b3339e05c47c66a8781d087bd2837f57de..34630a929f9f69f38051e294a31d9af0de0e7506 100644 --- a/web/src/settings/ExternalAccessPane.tsx +++ b/web/src/settings/ExternalAccessPane.tsx @@ -1,6 +1,6 @@ // settings / ExternalAccessPane.tsx - W63-T06, rulings R4/R14, contract C7. // -// This is the single AI API surface mounted by the shell. OAuth clients receive only the remote +// This is the shared setup pane opened by the Connectors directory. OAuth clients receive the remote // MCP URL; API-key secrets appear only in the one-time reveal returned by create or rotate. All // editable metadata is seeded again from the server response. // @@ -8,19 +8,59 @@ // numbered steps, then skill.md ONLY for a coding agent that reads one. No starter prompt card. import { useCallback, useEffect, useMemo, useRef, useState } from "react"; -import type { FormEvent, ReactNode } from "react"; +import type { FormEvent, ReactNode, Ref } from "react"; import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, checkTenant } from "../apiContract"; import { Mark } from "../shell/Brand"; import { AiClientMark } from "./aiClientMarks"; -import SlackSection from "./channels/SlackSection"; -import WhatsAppSection from "./channels/WhatsAppSection"; +import "../connectors/connectors.css"; type Access = "read" | "readwrite"; type ResourceScope = string[] | "all"; type ConnectionKind = "mcp" | "key"; // One numbered instruction. `block` is copyable text shown under it in a grey code block. -interface GuideStep { text: ReactNode; block?: string } +export interface GuideStep { text: ReactNode; block?: string } + +// S70-T09: the AI setup and connector tutorials share this actual frame and guide renderer. +// Each caller owns its steps, fields and actions; the frame makes no requests. +export function GuideSteps({ steps, renderBlock }: { + steps: readonly GuideStep[]; + renderBlock?: (value: string, index: number) => ReactNode; +}) { + return
    + {steps.map((entry, index) =>
  1. + {entry.text} + {entry.block ? renderBlock?.(entry.block, index) : null} +
  2. )} +
; +} + +export function ConnectionWizard({ title, titleId, headingRef, stepKey, stepIndex, stepCount, + showProgress = true, footer, children }: { + title: string; + titleId: string; + headingRef?: Ref; + stepKey: string; + stepIndex: number; + stepCount: number; + showProgress?: boolean; + footer?: ReactNode; + children: ReactNode; +}) { + return
+ {showProgress ?
+
+
: null} +
+ +

{title}

+ {showProgress ? Step {stepIndex + 1} of {stepCount} : null} +
+
{children}
+ {footer ?
{footer}
: null} +
; +} // One spelling of the Claude Code command, shared by the copy text and the rendered step. const claudeCodeAdd = (mcpUrl: string) => `claude mcp add --transport http loopable ${mcpUrl}`; @@ -28,7 +68,7 @@ const claudeCodeAdd = (mcpUrl: string) => `claude mcp add --transport http loopa // Add a client only here: choices, setup, supported connections and grant matching all derive from this catalog. // `steps` is the plain copy of the guide; `guide` is how the card renders it. `skill` marks a coding // agent that reads skill.md, and only those clients get the skill step. -const CLIENTS = [ +export const CLIENTS = [ { id: "claude", label: "Claude.ai", connections: ["mcp"], skill: false, steps: (mcpUrl: string) => `Customize → Connectors → Add custom connector\nRemote MCP URL: ${mcpUrl}\nChoose Add, then Connect and finish the Loopable sign-in.`, @@ -101,9 +141,9 @@ const CLIENTS = [ match: readonly string[]; }[]; -type ClientId = (typeof CLIENTS)[number]["id"]; +export type ClientId = (typeof CLIENTS)[number]["id"]; -function isClientId(value: unknown): value is ClientId { +export function isClientId(value: unknown): value is ClientId { return typeof value === "string" && CLIENTS.some((entry) => entry.id === value); } @@ -284,12 +324,30 @@ function connectionLabel(connection: ExternalConnection | undefined): string { return "Not connected"; } -export default function ExternalAccessPane() { - const [clientId, setClientId] = useState(null); - const [connection, setConnection] = useState(null); - const [step, setStep] = useState("client"); +/** The directory selects a client; this pane remains the one setup and credential implementation. */ +export default function ExternalAccessPane({ + initialClient, + view = "all", + settings = false, + onChanged, + onOpenApi, +}: { + initialClient?: ClientId; + view?: "all" | "client" | "api"; + settings?: boolean; + onChanged?: () => void; + onOpenApi?: () => void; +} = {}) { + const initialEntry = CLIENTS.find((entry) => entry.id === initialClient); + const [clientId, setClientId] = useState(initialClient ?? null); + const [connection, setConnection] = useState( + initialEntry?.connections.length === 1 ? initialEntry.connections[0] : null, + ); + const [step, setStep] = useState(view === "api" ? "setup" : initialEntry + ? initialEntry.connections.length === 1 ? "setup" : "connection" : "client"); + const [showSetup, setShowSetup] = useState(!settings || view === "api"); const stepHeading = useRef(null); - const [keyChoice, setKeyChoice] = useState("create"); + const [keyChoice, setKeyChoice] = useState(view === "client" ? "saved" : "create"); const [selectedKeyId, setSelectedKeyId] = useState(""); const [readyToVerify, setReadyToVerify] = useState(false); const [verifyResult, setVerifyResult] = useState(null); @@ -298,6 +356,8 @@ export default function ExternalAccessPane() { const [keys, setKeys] = useState(null); const [keysError, setKeysError] = useState(""); const [connections, setConnections] = useState([]); + const [connectionsLoading, setConnectionsLoading] = useState(view !== "api"); + const [connectionsError, setConnectionsError] = useState(""); const [canMint, setCanMint] = useState(null); const [label, setLabel] = useState(""); const [access, setAccess] = useState("read"); @@ -326,7 +386,8 @@ export default function ExternalAccessPane() { const client = CLIENTS.find((entry) => entry.id === clientId); const connectionKinds: readonly ConnectionKind[] = client?.connections ?? []; - const steps = SETUP_STEPS.filter((entry) => (entry !== "connection" || connectionKinds.length !== 1) + const steps = SETUP_STEPS.filter((entry) => (view !== "client" || entry !== "client") + && (entry !== "connection" || connectionKinds.length !== 1) && (entry !== "skill" || client?.skill === true)); const stepIndex = steps.indexOf(step); const selectedKey = keys?.find((key) => key.id === selectedKeyId); @@ -398,17 +459,35 @@ export default function ExternalAccessPane() { }, []); const loadConnections = useCallback(async () => { + setConnectionsLoading(true); + setConnectionsError(""); const result = await call(CONNECTIONS_PATH); + setConnectionsLoading(false); if (!result.ok || !Array.isArray(result.data?.connections)) { // Key management remains usable if OAuth discovery is unavailable. - setConnections([]); return; + setConnectionsError(result.ok ? "Connections could not be loaded." : result.message); + return; } setConnections(result.data.connections.map(parseConnection).filter( (row): row is ExternalConnection => row !== null, )); }, []); - useEffect(() => { void load(); void loadConnections(); }, [load, loadConnections]); + useEffect(() => { + if (view !== "client" || connection === "key") void load(); + }, [load, view, connection]); + useEffect(() => { + if (view !== "api") void loadConnections(); + }, [loadConnections, view]); + + const visibleConnections = view === "client" + ? connections.filter((entry) => entry.clientId === initialClient) : connections; + // Report changed metadata only. Secrets remain inside this pane's one-time reveal. + const changed = useRef(false); + useEffect(() => { + if (changed.current) onChanged?.(); + changed.current = true; + }, [keys, connections, onChanged]); useEffect(() => { if (canMint !== true) return; @@ -699,14 +778,8 @@ export default function ExternalAccessPane() { if (kind === "mcp") { return (
-
    - {client.guide(mcpUrl).map((entry, index) => ( -
  1. - {entry.text} - {entry.block ? codeBlock(entry.block, `guide:${client.id}:${index}`) : null} -
  2. - ))} -
+ codeBlock(value, `guide:${client.id}:${index}`)} />

{client.caveat}

); @@ -793,26 +866,6 @@ export default function ExternalAccessPane() { } .external-access-pane .set-secondary.kc-danger { color: var(--lp-red-deep); } .external-access-pane button:disabled { opacity: 0.5; cursor: not-allowed; } - .external-wizard { - position: relative; overflow: hidden; margin-top: 16px; max-width: 720px; background: var(--lp-surface); - border: 1px solid var(--lp-line); border-radius: var(--lp-r-lg); - } - .external-wizard-progress { height: 3px; } - .external-wizard-progress > div { height: 100%; background: var(--lp-ink); transition: width 160ms ease-out; } - .external-wizard-head { - display: flex; align-items: center; gap: 10px; padding: 16px 20px; - border-bottom: 1px solid var(--lp-line); - } - .external-wizard-head h2 { margin: 0; font-size: var(--lp-fs-md); font-weight: 600; color: var(--lp-ink); } - .external-wizard-head h2:focus-visible { outline: 2px solid var(--lp-blue-deep); outline-offset: 4px; } - .external-count { margin-left: auto; font-size: var(--lp-fs-xs); color: var(--lp-muted); } - .external-wizard-body { padding: 20px; min-width: 0; } - .external-wizard-foot { - display: flex; align-items: center; gap: 8px; padding: 12px 20px; - border-top: 1px solid var(--lp-line); - } - .external-wizard-foot .external-next { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; } - .external-sub { margin: 0 0 12px; font-size: var(--lp-fs-xs); color: var(--lp-muted); } .external-clients { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; } .external-access-pane .external-tile { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 12px; @@ -830,12 +883,6 @@ export default function ExternalAccessPane() { font-size: var(--lp-fs-xs); color: var(--lp-muted); } .external-for strong { color: var(--lp-ink); font-weight: 500; } - .external-steps { - margin: 0; padding-left: 20px; display: grid; gap: 12px; - font-size: var(--lp-fs-sm); color: var(--lp-ink); line-height: var(--lp-lh); - } - .external-steps li { padding-left: 4px; min-width: 0; } - .external-steps strong { font-weight: 600; } .external-access-pane kbd { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.92em; padding: 1px 6px; color: var(--lp-ink); background: var(--lp-wash); @@ -876,26 +923,42 @@ export default function ExternalAccessPane() { } @media (max-width: 560px) { .external-clients { grid-template-columns: repeat(2, minmax(0, 1fr)); } } `} -

AI API

+ {view === "all" ?

Loopable connections

: null} {error ?

{error}

: null} {notice ?

{notice}

: null} -
-
-
-
-
- -

- {step === "client" ? "Which AI are you using?" : step === "connection" ? "Connect Loopable" - : step === "setup" ? (connection === "key" ? "Set up an API key" : "Set up Loopable MCP") - : step === "skill" ? "Set up Loopable Skill" : "Check the connection"} -

- Step {stepIndex + 1} of {steps.length} -
- -
+ {view !== "api" && connectionsLoading ?

Loading connections…

: null} + {view !== "api" && connectionsError ? ( +

{connectionsError}

+
+ ) : null} + {!showSetup && view === "client" && !connectionsLoading ? ( +
+ ) : null} + {showSetup ? + {stepIndex > 0 ? ( + + ) : null} + {step !== "verify" ? ( + + ) : null} + : null}> {step === "client" ? ( <>

Choose your AI for the best setup instructions.

@@ -934,19 +997,19 @@ export default function ExternalAccessPane() { ) : null} - {step === "setup" && client && connection ? ( + {step === "setup" && ((client && connection) || view === "api") ? ( <> {settingUpFor()} {connection === "mcp" ? clientGuide("mcp") : ( <> -
+ {view === "all" ?
-
+
: null} {keysError ? (

{keysError}

@@ -959,15 +1022,19 @@ export default function ExternalAccessPane() { ) : keys.length === 0 ? ( -

You have no saved keys yet, so create one here.

+

{view === "client" ? "No saved API keys." : "You have no saved keys yet, so create one here."}

) : !keys.some(keyIsActive) ? ( -

No saved key is active. Create one or update its expiry below.

+

{view === "client" ? "No active API keys." : "No saved key is active. Create one or update its expiry below."}

+ ) : null} + {view === "client" && onOpenApi ? ( +
) : null} - {canMint === false ? ( + {view !== "client" && canMint === false ? (

Creating API keys requires the Analyst module. Ask an administrator to grant it.

) : null} - {keyChoice === "create" && canMint === true ? ( + {(keyChoice === "create" || view === "api") && canMint === true ? (
void create(event)}>
) : null} - {keyChoice === "saved" && keys && keys.length > 0 ? ( + {keyChoice === "saved" && client && keys && keys.length > 0 ? (
) : ( diff --git a/web/src/settings/channels/SlackSection.tsx b/web/src/settings/channels/SlackSection.tsx index de62be85a526c7fddb0cdb3b4c73dd917d7dad67..91a4d1a12cd35c2eac1675699cba1e03b0c0831f 100644 --- a/web/src/settings/channels/SlackSection.tsx +++ b/web/src/settings/channels/SlackSection.tsx @@ -97,8 +97,8 @@ function connectedAt(value: string): string { }); } -// C18: a props-free channel section owns its reads and mounts below AI connections. -export default function SlackSection() { +// The existing channel section owns its reads; the directory refreshes only public status. +export default function SlackSection({ onChanged }: { onChanged?: () => void } = {}) { const [data, setData] = useState(null); const [admin, setAdmin] = useState(false); const [loading, setLoading] = useState(true); @@ -120,12 +120,13 @@ export default function SlackSection() { setData(parsed); setAdmin(isAdmin(parseUserEnvelope(identity))); setError(""); + onChanged?.(); } catch { if (!signal?.aborted) setError("Slack connections could not be loaded; try again."); } finally { if (!signal?.aborted) setLoading(false); } - }, []); + }, [onChanged]); useEffect(() => { const controller = new AbortController(); @@ -254,7 +255,7 @@ export default function SlackSection() { : "An administrator can connect a Slack workspace to Loopable."}

) : null} {data.canInstall && admin ? ( - Add to Slack + Add to Slack ) : null} {data.installs.length > 0 ? (
diff --git a/web/src/settings/channels/WhatsAppSection.tsx b/web/src/settings/channels/WhatsAppSection.tsx index 59ffcb61fbdb1926271f87db1ba504b8ae2d65c4..6f4a14a7fc72fb0ddc51c73438ae0565304b9954 100644 --- a/web/src/settings/channels/WhatsAppSection.tsx +++ b/web/src/settings/channels/WhatsAppSection.tsx @@ -2,7 +2,8 @@ import { useEffect, useState } from "react"; import type { FormEvent } from "react"; import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, checkTenant } from "../../apiContract"; -export type WhatsAppViewState = "empty" | "sending" | "code" | "connected" | "wrong" | "locked"; +export type WhatsAppViewState = + "empty" | "sending" | "code" | "connected" | "wrong" | "locked" | "undelivered"; type ViewProps = { state: WhatsAppViewState; @@ -21,6 +22,9 @@ type ViewProps = { const WRONG_CODE = "That code is not right. Check the latest WhatsApp message and try again."; const LOCKED = "Too many wrong codes. Try again in 15 minutes."; const UNAVAILABLE = "WhatsApp is unavailable right now. Try again."; +export const UNDELIVERED = "The code could not be delivered to this number."; +// The code state asks the server this often whether the send failed after acceptance. +const POLL_MS = 5000; function screenSentence(value: unknown, fallback: string): string { return typeof value === "string" && value.trim() && !/[\u2013\u2014]/.test(value) @@ -31,7 +35,8 @@ export function WhatsAppSectionView(props: ViewProps) { const { state, phone = "", code = "", busy = false, noPermission = false } = props; const disabled = busy || state === "sending" || state === "locked" || noPermission; const codeEntry = state === "code" || state === "wrong" || state === "locked"; - const fallback = state === "locked" ? LOCKED : state === "wrong" ? WRONG_CODE : ""; + const fallback = state === "locked" ? LOCKED : state === "wrong" ? WRONG_CODE + : state === "undelivered" ? UNDELIVERED : ""; const message = screenSentence(props.message, fallback); const submit = (event: FormEvent, action?: () => void) => { event.preventDefault(); @@ -41,7 +46,6 @@ export function WhatsAppSectionView(props: ViewProps) { return (

WhatsApp

-

Chat with Loopable on WhatsApp with your own access.

{state === "connected" ? ( <>

Connected {phone}

@@ -91,13 +95,21 @@ export function WhatsAppSectionView(props: ViewProps) { ); } -type Status = { - state: "none" | "pending" | "connected" | "locked"; +export type Status = { + state: "none" | "pending" | "connected" | "locked" | "undelivered"; phone?: string; + expiresAt?: number; lockedUntil?: number; message?: string; }; +/** Maps a server status to the view state and the screened sentence it shows. */ +export function statusView(result: Status): { state: WhatsAppViewState; message: string } { + const state = result.state === "none" ? "empty" : result.state === "pending" ? "code" : result.state; + const fallback = result.state === "locked" ? LOCKED : result.state === "undelivered" ? UNDELIVERED : ""; + return { state, message: screenSentence(result.message, fallback) }; +} + class ChannelError extends Error { status: number; code: string; @@ -127,7 +139,7 @@ async function call(method: string, path = "", body?: object, signal?: AbortSign return result as Status; } -export default function WhatsAppSection() { +export default function WhatsAppSection({ onChanged }: { onChanged?: () => void } = {}) { const [state, setState] = useState("empty"); const [phone, setPhone] = useState(""); const [code, setCode] = useState(""); @@ -135,13 +147,18 @@ export default function WhatsAppSection() { const [busy, setBusy] = useState(true); const [noPermission, setNoPermission] = useState(false); const [lockedUntil, setLockedUntil] = useState(null); + const [expiresAt, setExpiresAt] = useState(null); function accept(result: Status) { - setState(result.state === "none" ? "empty" : result.state === "pending" ? "code" : result.state); - setPhone(result.phone ?? ""); + const view = statusView(result); + setState(view.state); + // An undelivered code keeps the typed number so the user can fix it or send again. + setPhone((current) => result.phone ?? (result.state === "undelivered" ? current : "")); setCode(""); - setMessage(screenSentence(result.message, result.state === "locked" ? LOCKED : "")); + setMessage(view.message); setLockedUntil(result.lockedUntil ?? null); + setExpiresAt(result.expiresAt ?? null); + onChanged?.(); } function failure(error: unknown, verifying = false) { @@ -183,6 +200,26 @@ export default function WhatsAppSection() { return () => window.clearTimeout(timer); }, [state, lockedUntil]); + // A send Telnyx accepted can still fail at Meta, so the code state polls until expiry. + useEffect(() => { + if (state !== "code" || expiresAt === null) return; + let live = true; + let controller: AbortController | null = null; + const timer = window.setInterval(() => { + if (Date.now() >= expiresAt * 1000) { + window.clearInterval(timer); + return; + } + controller?.abort(); + controller = new AbortController(); + void call("GET", "", undefined, controller.signal).then((result) => { + // A pending answer changes nothing and must not clear a code being typed. + if (live && result.state !== "pending") accept(result); + }).catch(() => undefined); + }, POLL_MS); + return () => { live = false; window.clearInterval(timer); controller?.abort(); }; + }, [state, expiresAt]); + async function act(method: string, path: string, body?: object) { if (busy || noPermission) return; setBusy(true); diff --git a/web/src/settings/settingsApi.ts b/web/src/settings/settingsApi.ts index 4e0bc8ae5e00e7f07d8de4ef635d14f88d81e7f3..ffd967627e7c6611571e16c4e1d785b87d761adc 100644 --- a/web/src/settings/settingsApi.ts +++ b/web/src/settings/settingsApi.ts @@ -48,6 +48,7 @@ export interface AdminUser { } export interface SettingsPayload { + freezeNotice?: FreezeNoticeState; user: { username: string; name: string; role: string }; scope: { bus: string[] | "all"; @@ -83,6 +84,15 @@ export type ApiResult = | { ok: true; data: T } | { ok: false; status: number; message: string }; +export interface FreezeNoticeState { + enabled: boolean; + text: string; +} + +export function getFreezeNotice(signal?: AbortSignal): Promise> { + return call("/freeze-notice", { cache: "no-store", signal }); +} + async function call( path: string, init?: RequestInit & { body?: string } diff --git a/web/src/sheet/BindingEditor.tsx b/web/src/sheet/BindingEditor.tsx index a977ac48596c36125fa5ee570c6bb032293675fb..afe31a7b9ea37a3dc88f034b808f715a66ebe689 100644 --- a/web/src/sheet/BindingEditor.tsx +++ b/web/src/sheet/BindingEditor.tsx @@ -537,7 +537,7 @@ function ColumnPickerDialog({ useOverlayLayer({ panelRef, onDismiss: onClose, - dismissOnOutside: true, + dismissOnOutside: false, initialFocus: "first", restoreFocus: true, trapFocus: true, @@ -872,9 +872,12 @@ export default function BindingEditor({ // applying) and a cell narrowed to a period could not be widened back to all time from this panel // at all, which is standing rule 1's second sentence - a limit nobody was told about. The switch // below states which of the two a cell is, and either answer is one click from the other. - // A Metric starts with its mandatory period enabled. Field and view filters opt in; - // old periods remain in state until save, where eligibility drops an unused one. - const [valuePeriodOn, setValuePeriodOn] = useState(Boolean(migrated) || sourceKind === "number"); + // New Metrics require a period. A saved Metric with no period keeps its all-time scope; + // a malformed saved period is not an absent one. Field and view filters opt in. + const savedAllTimeMetric = useState(() => initial != null && initial.mode !== "view" + && initial.period == null && savedSource.kind === "number" && Boolean(savedSource.metric_key))[0]; + const [valuePeriodOn, setValuePeriodOn] = useState(Boolean(migrated) + || (sourceKind === "number" && !savedAllTimeMetric)); const [viewPeriodOn, setViewPeriodOn] = useState(Boolean(migrated)); const periodOn = shape === "view" ? viewPeriodOn : valuePeriodOn; const setPeriodOn = shape === "view" ? setViewPeriodOn : setValuePeriodOn; @@ -938,7 +941,7 @@ export default function BindingEditor({ // HOTFIX 2026-09-29: a scope this page read moments ago paints NOW from the memo, so a second // Edit does not sit on a skeleton for a multi-second assembly. The server read below still // runs and replaces these lists (and drops a view that no longer exists) when it answers. - const memo = peekWorkspace(scope); + const memo = peekWorkspace(scope, { fieldsOnly: true }); if (memo) { setFields((memo.fields ?? []) as Field[]); setMeasures(memo.measures ?? []); @@ -949,20 +952,16 @@ export default function BindingEditor({ } (async () => { // ONE door, and it is the one that knows what exists now. - const fresh = await fetchWorkspace(scope); + const fresh = await fetchWorkspace(scope, { fieldsOnly: true }); if (!live) return; if (!fresh) { - // Offline or refused. The memo is the only offer left; only claim the list is settled if - // it produced one. - const ws = await fetchWorkspace(scope, { allowCached: true }); - if (!live) return; - if (ws) { - setFields((ws.fields ?? []) as Field[]); - setMeasures(ws.measures ?? []); - setViews(viewList(ws.views)); - setUserOptions(ws.userOptions ?? []); - setViewsLoaded(true); - } + // A failed fresh permission question cannot leave an earlier offer writable. + setFields([]); + setMeasures([]); + setViews([]); + setUserOptions([]); + setViewsLoaded(false); + setNotice("These fields could not be loaded. Close and reopen this panel to try again."); // ⛔ W54-T33 — BUT THE PANEL REVEALS EITHER WAY. Nothing better is coming for this // database, and a skeleton that outlives the request it was waiting on tells the reader // the panel is broken rather than that the server would not answer. @@ -1201,6 +1200,9 @@ export default function BindingEditor({ * That is the same three-state rule `customer-grid/FieldSelect.tsx` documents at its head. */ const chosenField = useMemo(() => fields.find((f) => f.key === field), [fields, field]); const chosenIsMetric = shape === "value" && sourceKind === "number"; + const keepingAllTimeMetric = savedAllTimeMetric && chosenIsMetric + && initial?.database === database && savedSource.kind === "number" + && savedSource.metric_key === metricKey; const filterUsesMetric = usesMetric(driverTree, fields, measures); const showMetricPeriod = chosenIsMetric || filterUsesMetric; useEffect(() => { @@ -1404,7 +1406,8 @@ export default function BindingEditor({ // Preview and Save wait for the field list even while the editor body is already visible. // A cell bound to a live view satisfies every other clause the moment the panel mounts, and a // Save taken then would write back a binding assembled from a list nobody had seen yet. - // W59-T11: the rail and Save share these terms; only Metrics require a period. + // The rail and Save share these terms; new Metrics require a period, while an unchanged + // saved all-time Metric keeps its absent period. Source and reference checks still apply. const sourceComplete = databases.some((d) => d.key === database) && (shape === "view" ? viewFields.length > 0 && viewFields.every((key) => fields.some((candidate) => candidate.key === key)) @@ -1413,14 +1416,23 @@ export default function BindingEditor({ : fn === "count" || numericFields.some((candidate) => candidate.key === field)); const filterComplete = scopeKind === "filter" || Boolean(viewId); const frozenRangeComplete = !frozen || Boolean(range.from && range.to && range.from <= range.to); - const periodComplete = (!chosenIsMetric || periodOn) + const periodComplete = (!chosenIsMetric || periodOn || keepingAllTimeMetric) && (!showMetricPeriod || !periodOn || periodMode !== "calendar" || frozenRangeComplete); const dataComplete = sourceComplete && (!chosenIsMetric || periodComplete); const copiedComplete = !savedDragDriverMissing; const relatedPending = relatedOffer.database !== database || !relatedOffer.loaded; - const aliasesPending = bindingFilterClauses(tree).some((leaf) => - leaf.colId.startsWith("linked:") || leaf.colId.startsWith("related:")) && relatedPending; - const ready = optionsResolved && !aliasesPending && dataComplete && filterComplete && periodComplete && copiedComplete + const relatedKeys = bindingFilterClauses(tree).flatMap((leaf) => [leaf.colId, + ...(leaf.rhs && "colId" in leaf.rhs ? [leaf.rhs.colId] : [])]).filter((key) => + key.startsWith("linked:") || key.startsWith("related:")); + // The server validates this saved predicate again on Save. Pending discovery does not + // require an unchanged predicate to be reconstructed from an offer it already names. + const savedRelatedUnchanged = initial?.database === database && scopeKind === savedScope.kind + && JSON.stringify(tree) === JSON.stringify(savedScope.tree) && !relatedNotice; + const aliasesPending = relatedKeys.length > 0 && relatedPending && !savedRelatedUnchanged; + const relatedUnavailable = !relatedPending && relatedKeys.some((key) => + !relatedOffer.fields.some((offer) => offer.key === key)); + const ready = optionsResolved && viewsLoaded && !aliasesPending && !relatedUnavailable + && dataComplete && filterComplete && periodComplete && copiedComplete && (shape === "value" || activeSection === "place"); const sectionComplete = { @@ -1493,10 +1505,10 @@ export default function BindingEditor({
Metric period - {!chosenIsMetric &&
+ {(!chosenIsMetric || (keepingAllTimeMetric && !periodOn)) &&
@@ -1853,7 +1865,10 @@ export default function BindingEditor({ ariaLabel="Metric to summarise" placeholder="Choose a Metric…" value={metricKey} - onChange={(key) => { setMetricKey(key); setField(""); }} + onChange={(key) => { + setMetricKey(key); setField(""); + if (key !== metricKey) setValuePeriodOn(true); + }} fields={measures.filter((m) => isNumericType(m.type)).map((m) => ({ key: m.key, label: m.label, type: "measure" as const, }))} @@ -2193,6 +2208,9 @@ export default function BindingEditor({ )} {preview &&

{`This cell would show ${preview}`}

} + {relatedUnavailable &&

+ Some related fields in this filter are unavailable. Review the filter before saving. +

} {notice &&

{notice}

}
diff --git a/web/src/sheet/DrillModal.tsx b/web/src/sheet/DrillModal.tsx index 4ee448a8986a5ca5de9e4a7b635b612044c9ddb7..6f160c757c66f666b2dfb212a0ad58004e4f6bfb 100644 --- a/web/src/sheet/DrillModal.tsx +++ b/web/src/sheet/DrillModal.tsx @@ -343,8 +343,9 @@ function ViewRowsPreview({ workbookId, label, previewBinding, onClose, onPlace, {headerActions} -
- {!current &&

Loading view…

} +
+ {!current &&

Loading view…

} {current?.error &&

{current.error}

} {answer && <>

{showingLine(answer.rows.length, answer.total)}

@@ -496,6 +497,7 @@ function DrillModal( useEffect(() => { let live = true; const seq = ++drillSeqRef.current; + setIdsPending(true); (async () => { const got = await drillCell(workbookId, addr, tab); if (!live) return; @@ -507,6 +509,7 @@ function DrillModal( setError(got.message); return; } + setError(""); setDrill(got.value); })(); return () => { @@ -551,7 +554,7 @@ function DrillModal( setFields([]); setWorkspace(null); (async () => { - const ws = await fetchWorkspace(scope, { allowCached: true }); + const ws = await fetchWorkspace(scope, { allowCached: true, fieldsOnly: true }); if (!live) return; if (!ws?.fields) { setRowsError("The fields for these records could not be loaded. Reopen View data to try again."); @@ -585,14 +588,18 @@ function DrillModal( const drillDraft = useCallback((tree: FilterTree) => { setIdsPending(true); if (draftTimerRef.current !== null) window.clearTimeout(draftTimerRef.current); + const seq = ++drillSeqRef.current; draftTimerRef.current = window.setTimeout(() => { draftTimerRef.current = null; - const seq = ++drillSeqRef.current; void drillCell(workbookId, addr, tab, { filter: tree }).then((got) => { if (seq !== drillSeqRef.current) return; - // A refused draft releases the grid; the cell write and its re-drill still follow. setIdsPending(false); - if (got.ok) setDrill(got.value); + if (got.ok) { + setError(""); + setDrill(got.value); + } else { + setError(got.message); + } }); }, 150); }, [workbookId, addr, tab]); @@ -700,8 +707,7 @@ function DrillModal( return () => window.removeEventListener("keydown", onKey, true); }, []); - // Lane F consumes this optional seed in the shared ID-window hook at integration. A spread - // passes it harmlessly while the older LinkGridModal type and implementation are still here. + // The shared ID-window hook consumes the server page without a second row request. const initialPage = useMemo(() => { if (!drill?.page) return undefined; const payload: CustomersPayload = { @@ -882,8 +888,6 @@ function DrillModal( label={label} table={scope} recordIds={drill.record_ids} - // `idsPending` joins `initialPage` in the same way: lane F's LinkGridModal consumes it at - // the combine (COMBINE-AMEND in context/S68-T23.md) and ignores it until then. {...{ initialPage, idsPending }} editable={false} cellView diff --git a/web/src/sheet/ImportDialog.tsx b/web/src/sheet/ImportDialog.tsx index 99026d55c396f3c21252052acd1b14be82f6a411..a0f127f15d658a7cda786db76c61e940b9a99244 100644 --- a/web/src/sheet/ImportDialog.tsx +++ b/web/src/sheet/ImportDialog.tsx @@ -46,6 +46,8 @@ export interface ImportDialogProps { onCancel: () => void; /** The import landed: this is the workbook it made, and the host opens it. */ onOpened: (workbookId: string) => void; + /** Retain the server's success sentence before opening unmounts this dialog. */ + onSummary?: (summary: string) => void; } /** 750ms x 240 ticks is three minutes, which is the cap and the sentence below agrees with it. */ @@ -140,7 +142,7 @@ export function formulaCellsSeen(job: ImportJob | null): number { type Phase = "idle" | "running" | "ended"; -export default function ImportDialog({ open, onCancel, onOpened }: ImportDialogProps) { +export default function ImportDialog({ open, onCancel, onOpened, onSummary }: ImportDialogProps) { const [file, setFile] = useState(null); const [phase, setPhase] = useState("idle"); const [job, setJob] = useState(null); @@ -177,6 +179,7 @@ export default function ImportDialog({ open, onCancel, onOpened }: ImportDialogP if (read.value.state === "done") { setPhase("ended"); if (read.value.workbook_id) { + onSummary?.(read.value.sentence ?? ""); onOpened(read.value.workbook_id); return; } @@ -189,7 +192,7 @@ export default function ImportDialog({ open, onCancel, onOpened }: ImportDialogP `Nothing has come back from the import for ${POLL_MINUTES} minutes. ` + "Close this and try again.", ); - }, [onOpened]); + }, [onOpened, onSummary]); const start = useCallback(async () => { if (!file) { @@ -303,7 +306,9 @@ export default function ImportDialog({ open, onCancel, onOpened }: ImportDialogP no formulas shows no line rather than a row of zeroes. */} {report && seen > 0 ? (

- {`Matched ${report.matched} of ${seen} formula cells.`} + {seen === 1 + ? `Matched ${report.matched} of ${seen} formula cell.` + : `Matched ${report.matched} of ${seen} formula cells.`}

) : null} diff --git a/web/src/sheet/PivotTab.tsx b/web/src/sheet/PivotTab.tsx new file mode 100644 index 0000000000000000000000000000000000000000..d146221ae87c3eb7ecfaa4ac70260f61d0534f2a --- /dev/null +++ b/web/src/sheet/PivotTab.tsx @@ -0,0 +1,303 @@ +// S70-T17 / C6. This worksheet stores configuration only. The server owns all aggregation. +import { useEffect, useId, useRef, useState } from "react"; +import type { ReactNode } from "react"; +import { API_V1, CREDENTIALS } from "../apiContract"; +import { FieldTypeIcon } from "../customer-grid/icons"; +import { BodyPortal, OverlayProvider, useOverlayLayer } from "../customer-grid/OverlaySurface"; +import { choiceVocabulary, isFilterGroup } from "../customer-grid/types"; +import type { Field, FilterNode, FilterTree } from "../customer-grid/types"; +import { FilterBuilderPanel, FilterFieldStatusContext } from "../filter-kit/FilterBuilderPanel"; +import type { FilterValuesState } from "../filter-kit/FilterBuilderPanel"; +import { isNumericType } from "../filter-kit/ops"; +import { fetchDistinctValues } from "./sheetApi"; +import { pivotAxisLabel, readPivotAnswer } from "./types"; +import type { PivotAnswer, PivotSpec, SheetTab } from "./types"; + +const EMPTY_PIVOT: PivotSpec = { database: "", rows: [], columns: [], values: [], filters: [] }; +const AGGREGATES = [ + ["sum", "Sum"], ["count", "Count"], ["avg", "Average"], ["min", "Min"], ["max", "Max"], +] as const; +const numberFormat = new Intl.NumberFormat(undefined, { maximumFractionDigits: 6 }); + +function filtersAvailable(nodes: FilterNode[], keys: Set): boolean { + return nodes.every(node => isFilterGroup(node) ? filtersAvailable(node.children, keys) + : keys.has(node.colId) && (!node.rhs || !("colId" in node.rhs) || keys.has(node.rhs.colId))); +} + +/** C6 stores a node list; an OR at the builder root must keep its conjunction in a group. */ +export const pivotFilterNodes = (tree: FilterTree): FilterNode[] => tree.conj === "or" + && tree.nodes.length ? [{ conj: "or", children: tree.nodes }] : tree.nodes; + +function PivotFilterDialog({ children, onClose }: { children: ReactNode; onClose: () => void }) { + const panelRef = useRef(null); + const titleId = useId(); + useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false, + initialFocus: "first", restoreFocus: true, trapFocus: true }); + return
+
+
+

Filters

+ +
+
{children}
+
+ +
+
+
; +} + +function PivotFilters({ database, fields, userOptions, nodes, onChange }: { + database: string; fields: Field[]; userOptions: string[]; nodes: FilterNode[]; + onChange: (nodes: FilterNode[]) => void; +}) { + const [valuesState, setValuesState] = useState>({}); + const values = useRef>({}); + const live = useRef(true); + useEffect(() => { live.current = true; return () => { live.current = false; }; }, []); + const loadValues = async (key: string, search = "", more = false) => { + if (!fields.some(field => field.key === key)) return; + const held = values.current[key]; + if ((held?.pending && held.search === search) || (more && !held?.hasMore)) return; + const offset = more && held ? held.offset : 0; + const pending: FilterValuesState = { values: more && held ? held.values : [], search, offset, + pending: true, loaded: false, hasMore: more && held ? held.hasMore : false }; + values.current = { ...values.current, [key]: pending }; + setValuesState(values.current); + const got = await fetchDistinctValues(database, key, { offset, search, limit: 200 }); + if (!live.current || values.current[key] !== pending) return; + const next: FilterValuesState = got.ok ? { + ...pending, values: [...new Set([...pending.values, ...got.value.values])], pending: false, + loaded: true, hasMore: got.value.hasMore, offset: got.value.offset + got.value.limit, + } : { ...pending, pending: false, error: "Unavailable" }; + values.current = { ...values.current, [key]: next }; + setValuesState(values.current); + }; + const statusValues = Object.fromEntries(fields.map(field => { + const declared = choiceVocabulary(field, []); + return [field.key, field.type === "user" ? [...new Set([...userOptions, ...(valuesState[field.key]?.values ?? [])])] + : declared.length ? declared : valuesState[field.key]?.values ?? []]; + })); + // Unwrap only the single root group produced above. The builder can keep editing OR faithfully. + const filters: FilterTree = nodes.length === 1 && isFilterGroup(nodes[0]) + ? { conj: nodes[0].conj, nodes: nodes[0].children } : { conj: "and", nodes }; + return { void loadValues(key, search); }, + moreValues: key => { void loadValues(key, values.current[key]?.search, true); } }}> + onChange(pivotFilterNodes(tree))} /> + ; +} + +export default function PivotTab({ tab, databases, readOnly, onChange }: { + tab: SheetTab; databases: { key: string; label: string }[]; readOnly: boolean; + onChange: (pivot: PivotSpec) => void; +}) { + const spec = tab.pivot ?? EMPTY_PIVOT; + const [refresh, setRefresh] = useState(0); + const [search, setSearch] = useState(""); + const [filtersOpen, setFiltersOpen] = useState(false); + const [schema, setSchema] = useState<{ + key: string; fields: Field[]; userOptions: string[]; error?: string; + } | null>(null); + const [aggregate, setAggregate] = useState<{ + key: string; answer?: PivotAnswer; error?: string; + } | null>(null); + const allowed = databases.some(database => database.key === spec.database); + // Refresh rechecks permission as well as data. No memo may outrun a changed field wall. + const schemaKey = JSON.stringify([tab.id, spec.database, databases, refresh]); + const resultKey = JSON.stringify([schemaKey, spec]); + const schemaReady = allowed && schema?.key === schemaKey && !schema.error; + const fields = schemaReady ? schema.fields : []; + const keys = new Set(fields.map(field => field.key)); + const scoped = schemaReady && spec.rows.every(key => keys.has(key)) + && spec.columns.every(key => keys.has(key)) && spec.values.every(value => keys.has(value.field)) + && filtersAvailable(spec.filters, keys); + const ready = scoped && spec.values.length > 0; + const current = ready && aggregate?.key === resultKey ? aggregate : null; + + useEffect(() => { + const controller = new AbortController(); + setSchema(null); + setAggregate(null); + if (!allowed) return () => controller.abort(); + // Same schema-only door as BindingEditor. It declares fields/options without reading records. + const scope = spec.database === "customer_data" ? "customer" + : spec.database === "product_data" ? "product" : spec.database; + void (async () => { + try { + const response = await fetch(`${API_V1}/workspace?scope=${encodeURIComponent(scope)}&fields_only=1`, + { credentials: CREDENTIALS, signal: controller.signal }); + const body = await response.json(); + if (controller.signal.aborted) return; + if (!response.ok || body?.workspace?.fieldsOnly !== true || !Array.isArray(body.workspace.fields)) { + setSchema({ key: schemaKey, fields: [], userOptions: [], + error: response.status === 403 ? "No access" : "Unavailable" }); + return; + } + setSchema({ key: schemaKey, fields: body.workspace.fields, + userOptions: Array.isArray(body.workspace.userOptions) ? body.workspace.userOptions : [] }); + } catch { + if (!controller.signal.aborted) + setSchema({ key: schemaKey, fields: [], userOptions: [], error: "Unavailable" }); + } + })(); + return () => controller.abort(); + }, [schemaKey, allowed]); // All inputs to the read are in schemaKey. + + useEffect(() => { + const controller = new AbortController(); + setAggregate(null); + if (!ready) return () => controller.abort(); + void (async () => { + try { + const response = await fetch(`${API_V1}/sheets/pivot`, { + method: "POST", credentials: CREDENTIALS, signal: controller.signal, + headers: { "Content-Type": "application/json" }, body: JSON.stringify(spec), + }); + const raw = await response.json(); + if (controller.signal.aborted) return; + if (response.status === 403) { + // A fresh permission refusal invalidates the earlier field offer as well as the matrix. + setSchema({ key: schemaKey, fields: [], userOptions: [], error: "No access" }); + } + const answer = response.ok ? readPivotAnswer(raw) : null; + // A named field limit keeps the offered fields; only a 403 means the access changed. + const limit = (raw?.error?.code === "pivot_field_unsupported" || raw?.error?.code === "pivot_too_large") + && typeof raw.error.message === "string" ? raw.error.message : null; + setAggregate({ key: resultKey, ...(answer ? { answer } : { + error: response.status === 403 ? "No access" : response.ok && raw?.ok === true + ? "Invalid response" : limit ?? "Refused", + }) }); + } catch { + if (!controller.signal.aborted) setAggregate({ key: resultKey, error: "Unavailable" }); + } + })(); + return () => controller.abort(); + }, [resultKey, ready]); + + const change = (next: PivotSpec) => { if (!readOnly) { setAggregate(null); onChange(next); } }; + const label = (key: string) => fields.find(field => field.key === key)?.label ?? "Unavailable"; + const addField = (field: Field, area: "rows" | "columns" | "values") => { + if (area === "values") change({ ...spec, values: [...spec.values, + { field: field.key, agg: isNumericType(field.type) ? "sum" : "count" }] }); + else if (!spec[area].includes(field.key)) change({ ...spec, [area]: [...spec[area], field.key] }); + }; + const answer = current?.answer; + const limits = answer?.limits; + const limited = limits != null && (Array.isArray(limits) ? limits.length > 0 + : typeof limits === "object" ? Object.keys(limits).length > 0 : Boolean(limits)); + const status = !spec.database ? "Choose a database" : !allowed ? "No access" + : schema?.key === schemaKey && schema.error ? schema.error : !schemaReady ? "Loading" + : !scoped ? "Unavailable fields" : !spec.values.length ? "Choose a value" + : current?.error ?? (!answer ? "Loading" : !answer.rows.length ? "No results" : ""); + + return
+
+ + +
+
+ +
+ {status &&

{status}

} + {answer && <> +
+ {`${numberFormat.format(answer.total_rows)} records`} + {limited && Limited} +
+ + + + {answer.columns.map((column, index) => )} + + {answer.rows.map((row, rowIndex) => + + {answer.cells[rowIndex].map((cell, colIndex) => )} + )} +
+ {spec.rows.map(label).join(" / ") || "Total"} + {pivotAxisLabel(column)}
{pivotAxisLabel(row)} + {cell === null ? "" : numberFormat.format(cell)} +
+ } +
+
+ {filtersOpen && scoped && !readOnly && + setFiltersOpen(false)}> + change({ ...spec, filters })} /> + + } +
; +} diff --git a/web/src/sheet/Ribbon.tsx b/web/src/sheet/Ribbon.tsx index 7e1a0c4c26a07d1dd23339307c20e0b59411d682..2bb6bb91430f2d7d0478766ce44f21d7c225ae78 100644 --- a/web/src/sheet/Ribbon.tsx +++ b/web/src/sheet/Ribbon.tsx @@ -173,6 +173,8 @@ export interface RibbonAction { * DISABLED rather than wired to nothing, which is the fake affordance `canImport` refuses by name. */ export interface RibbonInsertDoor extends Partial { + /** S70-T17: workbook tab creation stays outside the cell action union. */ + onCreatePivot?: () => void; /** Fit the selected range using the grid's own visible geometry. */ zoomToSelection?: () => void; /** Tabs the user has hidden, for the View tab's Unhide list. `veryHidden` tabs are NOT here: @@ -199,6 +201,8 @@ export interface RibbonProps { style: CellStyle | null; /** No selection, or a workbook that is saving: every control is inert but still readable. */ disabled?: boolean; + /** Viewers can navigate and export, while document commands remain disabled. */ + readOnly?: boolean; /** Whether the sheet currently freezes its leading column, so the Freeze item reads back. */ frozen?: boolean; /** ⛔ NO FAKE AFFORDANCE. Import is offered only where the page actually has a door for it. */ @@ -286,6 +290,7 @@ const STRIP: readonly ControlKey[] = [ */ export interface RibbonTabContext { style: CellStyle | null; + readOnly?: boolean; frozen: boolean; open: Popover; anchorEl: HTMLElement | null; @@ -411,6 +416,13 @@ function partitionRibbonGroups(node: ReactNode, visibleCount: number, folded: bo /** Stateful child controls must be mounted by React, never called as groups(ctx) functions. * The recording context returns no buttons, and open is empty, so no overlay or action mounts. */ +export function ribbonCommandWrites(key: string, label?: string): boolean { + if (key === "findselect" && label !== undefined) return !/^(Find|Go To)/i.test(label); + return !key.startsWith("tab-") && !["copy", "findselect", "export", "fullscreen", "collapse", + "overflow", "cell-data", "data-queries-connections", "view-new-window", "view-fullscreen", + "view-go-to"].includes(key); +} + function RibbonCommandProbe({ node, context, tab, group, disabled, runControl, publish }: { node: React.ReactElement<{ ctx: RibbonTabContext }>; context: RibbonTabContext; @@ -425,7 +437,7 @@ function RibbonCommandProbe({ node, context, tab, group, disabled, runControl, p ...context, open: "", anchorEl: null, control: (key, label, _body, _onClick, _pressed, _expanded, _kt, _opens, controlDisabled) => { const command = { id: `${tab}:${key}`, key, label, location: group, - disabled: disabled || !!controlDisabled, run: () => runControl(tab, key) }; + disabled: disabled || !!controlDisabled || (!!context.readOnly && ribbonCommandWrites(key)), run: () => runControl(tab, key) }; const at = records.findIndex(record => record.id === command.id); if (at < 0) records.push(command); else records[at] = command; @@ -460,7 +472,8 @@ function recordRibbonCommands( const record: Recorded = { id: `${tab.id}:${key}`, key, label, location: tab.label, kind: "control", opensGallery: opensGallery === "gallery", - disabled: disabled || !!controlDisabled || (tab.id === "insert" && !context.insert), + disabled: disabled || !!controlDisabled || (tab.id === "insert" && !context.insert) + || (!!context.readOnly && ribbonCommandWrites(key)), run: () => runControl(tab.id, key), }; pending.push(record); records.push(record); @@ -471,7 +484,8 @@ function recordRibbonCommands( if (gallery) { const record: Recorded = { id: `${tab.id}:${gallery.key}:item:${label}`, key: gallery.key, label, - location: gallery.location, kind: "item", disabled: gallery.disabled, run: onClick, + location: gallery.location, kind: "item", disabled: gallery.disabled + || (!!context.readOnly && ribbonCommandWrites(gallery.key, label)), run: onClick, }; pending.push(record); records.push(record); } @@ -524,7 +538,7 @@ function recordRibbonCommands( } export default function Ribbon({ - style, disabled = false, frozen = false, canImport = false, onApply, onAction, insert, + style, disabled = false, readOnly = false, frozen = false, canImport = false, onApply, onAction, insert, }: RibbonProps) { const [focus, setFocus] = useState(TABS[0].key); const [open, setOpen] = useState(""); @@ -715,16 +729,18 @@ export default function Ribbon({ }, [focus, focusControl, open, reachable]); const apply = useCallback((patch: CellStylePatch) => { + if (readOnly) return; setOpen(""); if (overflowOpen) closeOverflow(); onApply(patch); - }, [closeOverflow, onApply, overflowOpen]); + }, [closeOverflow, onApply, overflowOpen, readOnly]); const act = useCallback((kind: RibbonActionKind, arg?: string | number) => { + if (readOnly && !["copy", "find", "go-to"].includes(kind)) return; setOpen(""); if (overflowOpen) closeOverflow(); onAction({ kind, arg }); - }, [closeOverflow, onAction, overflowOpen]); + }, [closeOverflow, onAction, overflowOpen, readOnly]); /** * ⭐⭐ W53-T07 — CLOSING A MENU RETURNS FOCUS TO THE CONTROL THAT OWNS IT, and that is the @@ -806,7 +822,7 @@ export default function Ribbon({ aria-expanded={expanded} aria-haspopup={expanded === undefined ? undefined : popupRole} tabIndex={focus === key ? 0 : -1} - disabled={(key !== "overflow" && disabled) || controlDisabled} + disabled={(key !== "overflow" && disabled) || controlDisabled || (readOnly && ribbonCommandWrites(key))} onFocus={() => setFocus(key)} // The grid keeps the keyboard: pressing a ribbon button must not blur the canvas, or glide // drops its selection and the act lands on nothing. Same reason as `CellActionBar`. @@ -826,7 +842,8 @@ export default function Ribbon({ let groupIndex = 0; const group = (name: string, body: ReactNode, extra = "") => (
-
{body}
+
{readOnly && name === "Font" + ?
{body}
: body}
{name}
); @@ -870,7 +887,8 @@ export default function Ribbon({ role={role} aria-checked={role === "menuitem" ? undefined : on} className={"sheet-ribbon-item" + (on ? " is-on" : "")} - data-kt={kt} + data-kt={kt} + disabled={disabled || (readOnly && ribbonCommandWrites(open, label))} onMouseDown={(e) => e.preventDefault()} onClick={onClick} > @@ -898,7 +916,7 @@ export default function Ribbon({ const pdf={...printSettings,from:pageFrom.trim()===""?undefined:Number(pageFrom),to:pageTo.trim()===""?undefined:Number(pageTo)}; if(format==="pdf") { planSheetPdf(snapshot.book,pdf); - await exportContext.savePrintSettings(pdf); + if (!readOnly) await exportContext.savePrintSettings(pdf); snapshot.book={...snapshot.book,printSettings:pdf}; } await runSheetExport(format,snapshot,{range:format==="csv"&&exportScope==="selection"&&selectionRange?selectionRange:undefined,pdf}); @@ -908,7 +926,7 @@ export default function Ribbon({ }; const tabContext: RibbonTabContext = { - style, frozen, open, anchorEl, onApply, apply, act, + style, frozen, open, anchorEl, readOnly, onApply: patch => { if (!readOnly) onApply(patch); }, apply, act, fullscreen, toggleFullscreen, newWindowBlocked, openNewWindow, openCellData, openPop, closePop, control, group, word, large, stack, unavailable, item, insert, diff --git a/web/src/sheet/SheetChartLayer.tsx b/web/src/sheet/SheetChartLayer.tsx index 61b4a53bc46cc1695d6f4ea97096a2a18e775950..f029d11feffa61aa54339af11a5ade683e0b8a49 100644 --- a/web/src/sheet/SheetChartLayer.tsx +++ b/web/src/sheet/SheetChartLayer.tsx @@ -7,6 +7,7 @@ import type { ChartAnchorDelta, ChartPreview } from "./SheetChart"; import { ChartWorkbookContext } from "./ChartDialog"; import { chartPaneProjections, lockstepScrollX } from "./sheetState"; import { chartDocumentGeometry } from "./types"; +import { useEscapeClose } from "../ui/useEscapeClose"; import type { CellMap, SheetChartSpec } from "./types"; export interface SheetChartLayerProps { @@ -48,6 +49,9 @@ export default function SheetChartLayer({ charts, cells, visCols, visRows, colum const root = useRef(null); const [size, setSize] = useState({ width: 0, height: 0 }); const selectedId = selectedChartId; + useEscapeClose(selectedId !== null, () => onSelectChart(null), { + container: () => root.current, + }); const [previews, setPreviews] = useState>({}); useLayoutEffect(() => { const node = root.current; @@ -77,7 +81,6 @@ export default function SheetChartLayer({ charts, cells, visCols, visRows, colum if ((event.ctrlKey || event.metaKey) && !event.altKey && (event.key === "c" || event.key === "C")) { event.preventDefault(); event.stopPropagation(); onCopyChart(selectedId); return; } - if (event.key === "Escape") onSelectChart(null); if (event.key !== "Delete" && event.key !== "Backspace") return; event.preventDefault(); event.stopPropagation(); deleteChart(selectedId); }; diff --git a/web/src/sheet/SheetGrid.tsx b/web/src/sheet/SheetGrid.tsx index 8c32076d3cf28d83a0611d8869e55a0f6a73a19a..929fb18e157942475ffd28e479811b0ea366b270 100644 --- a/web/src/sheet/SheetGrid.tsx +++ b/web/src/sheet/SheetGrid.tsx @@ -768,7 +768,7 @@ export interface SheetGridProps { */ onCommit: (edits: Record, move?: MovedBlock) => void; /** The one cell selected when this workbook and tab are adopted. */ - openAt: { workbookId: string; tabId: string; addr: string }; + openAt: { workbookId: string; tabId: string; addr: string; request?: number }; onUndo: () => void; onRedo: () => void; onNotice: (message: string) => void; @@ -838,6 +838,7 @@ export interface SheetGridProps { * answer, or a local copy outlives the state it mirrored and an undo repaints from a stale list. */ onCharts?: (charts: SheetChartSpec[]) => void; + onCreatePivot?: () => void; /** A dragged column width, once the drag has ENDED. See the note on `dragWidth` below. */ onColumnWidth: (colIndex: number, width: number) => void; @@ -1172,7 +1173,7 @@ export default function SheetGrid({ formulaBar, onRefresh, onSaveNow, onOpenImport, onCommit, openAt, onUndo, onRedo, onNotice, onEvaluateBindings, onSelect, onStructure, onGrow, onFreeze, onColumnWidth, onRowHeight, onHideBands, onRules, onCharts, - onTabView, onTabVisibility, onRowGroups, + onTabView, onTabVisibility, onRowGroups, onCreatePivot, onSelectTab, options, viewShadows, onDraftChange, readOnly = false, }: SheetGridProps) { @@ -1432,9 +1433,8 @@ export default function SheetGrid({ }>()); const borderRowLayout = useRef<{ offsets: number[]; heightAt: (index: number) => number } | null>(null); const spillPaintFrame = useRef(null); - // Spill owners are recomputed on render. Border repairs survive selection/viewport renders: - // Glide may blit their cells instead of drawing them again before this frame flushes. - spillPaintQueue.current.clear(); + // Selection renders can precede the deferred text frame while Glide only blits its cells. + // Document/geometry changes invalidate queued text below; selection alone keeps that run. const queueSpillPaint = useCallback((key: string, paint: () => void, border = false) => { (border ? borderPaintQueue : spillPaintQueue).current.set(key, paint); if (spillPaintFrame.current !== null) return; @@ -1880,6 +1880,14 @@ export default function SheetGrid({ borderPaintQueue.current.clear(); borderPaintViewports.current = new WeakMap(); } + const spillPaintRules = onRules ? tab.rules : localRules; + const spillPaintDocument = useMemo(() => ({}), + [borderPaintDocument, tab, workbook.sheets, spillPaintRules, sheetFilter]); + const spillPaintDocumentRef = useRef(spillPaintDocument); + if (spillPaintDocumentRef.current !== spillPaintDocument) { + spillPaintDocumentRef.current = spillPaintDocument; + spillPaintQueue.current.clear(); + } /** Imported merged ranges reserve their cells even though the client does not merge-paint them. */ const mergedSpillRanges = useMemo(() => { const raw = tab.excel?.mergedCells; @@ -2022,15 +2030,15 @@ export default function SheetGrid({ // cell's editor: `onCellActivated` opens it for an EXISTING formula, and `onKeyDown`'s // typed-character branch opens it the moment a fresh "=" starts one. glide's own overlay // would otherwise sit on top of the dropdown/ScreenTip a person may be typing INTO. - allowOverlay: !viewChromeRefusal(cell) && cell?.kind !== "bound" + allowOverlay: !readOnly && !viewChromeRefusal(cell) && cell?.kind !== "bound" && cell?.kind !== "formula" && !validationOf(cell), - readonly: !!viewChromeRefusal(cell) || cell?.kind === "bound" || validationOf(cell) !== null, + readonly: readOnly || !!viewChromeRefusal(cell) || cell?.kind === "bound" || validationOf(cell) !== null, }; }, // `cells` is the whole dependency: a new workbook object gives a new `cells`, which is what // makes glide repaint. Narrowing this to `tab.id` is the stale-closure bug. // Filter toggles must also invalidate Glide's cell paint, even before rows are hidden. - [addrAt, cells, sheetFilter], + [addrAt, cells, sheetFilter, readOnly], ); /** @@ -2052,6 +2060,33 @@ export default function SheetGrid({ [fromBand, getCellContent, bandRepaint], ); + const hasCellAnchor = anchor !== null; + useEffect(() => { + if (!hasCellAnchor || !wrapRef.current) return; + let frame = 0; + const project = () => { + cancelAnimationFrame(frame); + frame = requestAnimationFrame(() => { + frame = requestAnimationFrame(() => { + const item = selectionRef.current?.current?.cell; + if (!item) return; + const bounds = item[1] < 0 + ? bandGridRef.current?.getBounds(item[0], item[1] + bandRows) + : gridRef.current?.getBounds(item[0], item[1]); + if (!bounds || ![bounds.x, bounds.y, bounds.width, bounds.height].every(Number.isFinite)) return; + setAnchor(prior => !prior || (prior.x === bounds.x && prior.y === bounds.y + && prior.width === bounds.width && prior.height === bounds.height) ? prior : bounds); + }); + }); + }; + // A cell click can collapse the navigation rail after Glide emitted its old viewport rect. + // Reproject the selected cell when that layout settles, including the frozen grid. + const observer = new ResizeObserver(project); + observer.observe(wrapRef.current); + project(); + return () => { observer.disconnect(); cancelAnimationFrame(frame); }; + }, [hasCellAnchor, bandRows, zoom]); + const selected = useMemo(() => { const cur = selection?.current?.cell; if (!cur) return null; @@ -2159,6 +2194,9 @@ export default function SheetGrid({ }; selectionRef.current = accepted; setSelection(accepted); + // An explicit opening/name-box move also owns the next gesture-less reconciliation. + // The first real click or key still wins and consumes this same one-shot handoff. + if (selectionIntent === "app") replacementSelectionRef.current = accepted; // ⭐⭐ W54-T28 / R9(iv): "the run resets when the selection is moved by anything other than // Tab", said in the one place every move arrives at. The commit arm raises the flag for its // own move and this consumes it, so a Tab keeps the run and everything else ends it. @@ -2475,6 +2513,7 @@ export default function SheetGrid({ const commitWithRecalc = useCallback((edits: Record, move?: MovedBlock, landed?: SheetTab) => { + if (readOnly) return; const entries = Object.entries(edits); const reserved = entries.some(([addr, value]) => value && isViewReserved(addr)); if (reserved) { onNotice("Resize or remove this view before editing its reserved cells."); return; } @@ -2509,7 +2548,7 @@ export default function SheetGrid({ onCommit(calculated, move && Object.keys(document).length > 0 ? { ...move, pasteTab: document } : move); } - }, [commitViewRecord, isViewReserved, onCommit, onNotice, onTabView, tab, workbook]); + }, [commitViewRecord, isViewReserved, onCommit, onNotice, onTabView, tab, workbook, readOnly]); /** ONE typed commit. `commitTyped` picks literal or formula and drops a binding on its own. */ const onCellEdited = useCallback((item: Item, value: EditableGridCell) => { @@ -2571,6 +2610,7 @@ export default function SheetGrid({ const openFormulaEditor = useCallback(( addr: string, draft: string, caret: number, viewCell: readonly [number, number], inBand: boolean, ) => { + if (readOnly) return; const anchor = splitAddress(addr); if (!anchor) return; // A formula edit is the ONE editor at a time this component ever shows, same as every other @@ -2582,7 +2622,7 @@ export default function SheetGrid({ addr, inBand, viewCell, draft, caret, dropdownIndex: 0, dismissed: false, mode: "enter", caretOnly: false, anchor, insertStart: null, pointCell: null, pointBand: null, rangeAnchor: null, }); - }, []); + }, [readOnly]); /** * ⭐⭐ W54-T21 / INSTRUCTION 18 — ESCAPE CANCELS THE EDIT AND THE CELL GOES BACK TO WHAT IT HELD. @@ -3617,6 +3657,7 @@ export default function SheetGrid({ * ⚠ R10 IS UNTOUCHED: a format presents the stored value and never recomputes it. */ const applyStyle = useCallback((patch: CellStylePatch) => { + if (readOnly) return; const addrs = selectedAddrs(); if (addrs.length === 0) { onNotice("Select a cell first, then choose a format for it."); @@ -3625,7 +3666,7 @@ export default function SheetGrid({ const out = styleCells(tab, addrs, patch); if (Object.keys(out.edits).length === 0) return; onCommit(out.edits); - }, [onCommit, onNotice, selectedAddrs, tab]); + }, [onCommit, onNotice, selectedAddrs, tab, readOnly]); /** * ⭐⭐ S67-T11 (QI16, J16, C2) — EXCEL'S DRAW BORDER PENS. Lane B's Borders menu raises @@ -4068,11 +4109,9 @@ export default function SheetGrid({ * from pulling the reader back after they have already moved. */ useEffect(() => { - const key = `${workbook.id}:${tab.id}`; + const key = `${workbook.id}:${tab.id}:${openAt.addr}:${openAt.request ?? 0}`; if (openAt.workbookId !== workbook.id || openAt.tabId !== tab.id || openedSelectionRef.current === key) return; - openedSelectionRef.current = key; - const requested = splitAddress(openAt.addr) ?? { col: 1, row: 1 }; let col = viewIndexOf(visCols, requested.col - 1); let row = viewRowOf(requested.row - 1); @@ -4080,8 +4119,21 @@ export default function SheetGrid({ col = viewIndexOf(visCols, 0); row = viewRowOf(0); } - applySelection({ col0: col, row0: row, col1: col, row1: row }, [col, row], true, false); - }, [applySelection, openAt, realCol, realRow, tab.id, viewRowOf, visCols, workbook.id]); + // Glide's first viewport measurement follows mount. Reveal after that measurement so a + // deep link and a repeated name-box request use the same settled geometry. + let revealFrame: number | null = null; + const frame = window.requestAnimationFrame(() => { + revealFrame = window.requestAnimationFrame(() => { + openedSelectionRef.current = key; + applySelection({ col0: col, row0: row, col1: col, row1: row }, [col, row], true, false); + }); + }); + return () => { + window.cancelAnimationFrame(frame); + if (revealFrame !== null) window.cancelAnimationFrame(revealFrame); + }; + }, [applySelection, openAt.workbookId, openAt.tabId, openAt.addr, openAt.request, + realCol, realRow, tab.id, viewRowOf, visCols, workbook.id]); /** * W55-T05 / R4: KeyTips consumes its capture-phase key while focus is on the ribbon, then @@ -4553,8 +4605,9 @@ export default function SheetGrid({ * through the same builder. */ const copySelection = useCallback((cut: boolean) => { + if (readOnly && cut) return; runCopySelection(cut, viewRange, heldFor, copiedRef, setCopyMarquee, clearSelection, onNotice); - }, [clearSelection, heldFor, onNotice, viewRange]); + }, [clearSelection, heldFor, onNotice, viewRange, readOnly]); /** * PASTE. Native paste supplies its event payload; the ribbon reads the browser clipboard. @@ -5262,6 +5315,8 @@ export default function SheetGrid({ // W51-T18 turned the other one into a door. // ═══════════════════════════════════════════════════════════════════════════════════════════ const onRibbonAction = useCallback((action: RibbonAction) => { + if (readOnly && (!["copy", "find", "go-to"].includes(action.kind) + || (action.kind === "find" && action.arg === "replace"))) return; if (action.kind === "paste" && action.arg === "special") { openPasteSpecial(); return; } if (action.kind === "paste" && (action.arg === "transpose" || action.arg === "link")) { pasteFromClipboard(MENU_PASTE_OPTIONS[action.arg === "transpose" ? "paste-transpose" : "paste-link"]); @@ -5411,7 +5466,7 @@ export default function SheetGrid({ clearSelectionWith, copyChartImageToClipboard, copySelection, fillSelection, frozenCols, frozenRows, hideBand, onGrow, onNotice, onOpenImport, onRefresh, openPasteSpecial, pasteFromClipboard, rowHeight, rules, saveRules, selectedAddrs, selectedChartId, selectionRange, stepIndent, - structure, tab, toggleFormatPainter, unhideAll]); + structure, tab, toggleFormatPainter, unhideAll, readOnly]); // ═══════════════════════════════════════════════════════════════════════════════════════════ // ⭐⭐ W54-T19 (INSTRUCTION 13) — THE HALF OF THE DEFECT THAT NEVER REACHES glide AT ALL. @@ -5520,6 +5575,14 @@ export default function SheetGrid({ const onKeyDown = useCallback( (event: GridKeyEventArgs) => { + const viewerHit = readOnly ? matchShortcut(event) : null; + if (readOnly && ((viewerHit !== null && !["jump-edge", "extend-edge", "extend-cell", + "go-home", "go-end", "page-up", "page-down", "prev-sheet", "next-sheet", "select-all", + "select-column", "select-row", "select-region", "find", "show-shortcuts"].includes(viewerHit)) + || (!event.ctrlKey && !event.metaKey && !event.altKey && (event.key.length === 1 || event.key === "F2")))) { + event.cancel(); event.preventDefault(); event.stopPropagation(); + return; + } // glide invokes this before applying its own key-driven selection. Keep that callback tied // to the reader's key for this event only; a later data-identity reconciliation has no key. keyboardSelectingRef.current = true; @@ -5943,7 +6006,7 @@ export default function SheetGrid({ onSaveNow, onUndo, openFormulaEditor, openSheetFilter, options, openNumberFormat, openPasteSpecial, pasteShortcutContext, realCol, realRow, selectDocumentAxis, selectDocumentRegion, selection, structureForSelection, switchTab, tab, - viewRange, viewRowOf, viewShadows, visCols, visRows], + viewRange, viewRowOf, viewShadows, visCols, visRows, readOnly], ); // ══════════════════════════════════════════════════════════════════════════════════════════ @@ -6409,29 +6472,8 @@ export default function SheetGrid({ }, true); } - // ═══════════════════════════════════════════════════════════════════════════════ - // ⭐ WAVE 54 BASE KIT — THE SPARKLINE SEAM, ASKED AS EARLY AS THE SIGNATURE ALLOWS. - // - // `drawSparkline` is handed the cell, so this is the first statement that can possibly call - // it: `cell` has to be read first. It answers `false` today and NOTHING changes; when lane B - // fills it in, a `true` means that cell's whole paint belonged to the sparkline. - // - // ⛔ THE TRUE BRANCH IS A BARE `return;`, AND THAT IS DELIBERATE TWICE OVER. glide 6.0.3 - // ignores a `DrawCellCallback`'s return (see D-642 above), so `return;` is the only way to - // say "done"; and the gates COUNT the other calls rather than merely looking for them -- - // `verify_api.py` pins the default renderer at EXACTLY two call sites and `verify_grid_ux.py` - // pins the marquee closure at exactly three, on the RAW file, so adding either call here - // would red a leg belonging to a different ticket for a line that changed no pixel. ⚠ That - // also means naming those calls in prose HERE, with their parentheses and semicolon, moves a - // count: this note says them in words on purpose. - // - // ⚠ LANE B: THIS RETURNS BEFORE `ruleStyleAt` AND BEFORE ALL FOUR OVERLAY CLOSURES - // (`paintRefBorder`, `paintMarquee`, `paintShimmer`, `paintMark`). A sparkline cell therefore - // wears NO conditional-format rule, NO copy marquee, NO reference border and NO corner mark - // unless you decide it should and call them. That decision is yours; the seam does not make - // it for you. - // ═══════════════════════════════════════════════════════════════════════════════ - if (cell && drawSparkline(ctx, rect, cell, cells)) { paintFilterButton(); return; } + // Sparkline marks are painted in each value branch below, after the resolved fill and + // borders. Text and selection overlays still finish the cell, including no-text plots. // ═══════════════════════════════════════════════════════════════════════════════ // ⭐⭐ W51-T18 — THE RULE IS ASKED BEFORE THE EARLY RETURN, AND THAT IS THE HALF @@ -6744,6 +6786,7 @@ export default function SheetGrid({ // Give even an unstyled refusal the shared text routine and the cell's own spacing. const style = bindingRefusal(cell) ? { ...baseStyle, color: "--lp-red-deep" } : baseStyle; if ((!stylePaints(style) && !ruleStyle) || !style) { + if (cell) drawSparkline(ctx, rect, cell, cells, zoom); // ⭐ `drawContent()` IS the default renderer, and it must run BEFORE the mark so // the mark sits on top of the value rather than under it. if (!paintTextSpill()) drawContent(); @@ -6789,6 +6832,8 @@ export default function SheetGrid({ // be covered only inside the text band; unrelated segments and every outer edge survive. paintCellBorders(ctx, rect, style.border); + if (cell) drawSparkline(ctx, rect, cell, cells, zoom); + // 3. THE TEXT. Spill owns its expanded, separator-reaching clip; an ordinary run retains the // old inset clip. Clipping before asking the spill would also clip away its line mask. if (!paintTextSpill()) { @@ -6956,7 +7001,8 @@ export default function SheetGrid({ if (!selectionProjectsHeader(selection, "row", row, headerProjectionBounds)) return []; const grid = row < 0 ? bandGridRef.current : gridRef.current; const bounds = grid?.getBounds(-1, row < 0 ? row + bandRows : row); - if (!bounds || bounds.y + bounds.height <= origin.y || bounds.y >= origin.bottom) return []; + if (!bounds || ![bounds.x, bounds.y, bounds.width, bounds.height].every(Number.isFinite) + || bounds.y + bounds.height <= origin.y || bounds.y >= origin.bottom) return []; return [{ row, label: realRow(row) + 1, left: bounds.x - origin.x, top: bounds.y - origin.y, width: bounds.width, height: bounds.height }]; }); @@ -7039,13 +7085,14 @@ export default function SheetGrid({ row1: realRow(Math.max(range.row0, range.row1)), } : null, charts, + onCreatePivot, onCharts: writeCharts, onCells: onCommit, cellAt: (col: number, row: number) => cellAt(tab, addressOf(col, row)), }; }, [allVisRows, bandRows, charts, frozenCols, headingsVisible, onCommit, onTabView, onTabVisibility, realCol, realRow, rowHeight, tab, viewRange, visCols, widths, workbook, wrapHeights, writeCharts, - sheetFilter, toggleSheetFilter]); + sheetFilter, toggleSheetFilter, onCreatePivot]); return (
setRefusalHover(null)} @@ -7057,6 +7104,7 @@ export default function SheetGrid({ 0 || bandRows > 0} @@ -7092,6 +7140,7 @@ export default function SheetGrid({ const typing = target instanceof Element && !!target.closest('input, textarea, [contenteditable="true"]'); if (matchShortcutInGrid(event, pasteShortcutContext(target)) === "paste-special") { + if (readOnly) { event.preventDefault(); event.stopPropagation(); return; } event.preventDefault(); event.stopPropagation(); openPasteSpecial(); @@ -7102,6 +7151,7 @@ export default function SheetGrid({ && (event.key === "ArrowRight" || event.key === "ArrowLeft")) { event.preventDefault(); event.stopPropagation(); + if (readOnly) return; if (event.key === "ArrowRight") groupRows(); else ungroupRows(); return; @@ -7115,6 +7165,7 @@ export default function SheetGrid({ && (copyKey === "c" || copyKey === "x") && !!held?.current) { event.preventDefault(); event.stopPropagation(); + if (readOnly && copyKey === "x") return; copySelection(copyKey === "x"); return; } @@ -7314,6 +7365,7 @@ export default function SheetGrid({ onKeyDown={onKeyDown} onPaste={(target, values) => onPaste(fromBand(target), values)} onCellActivated={(item: Item) => { + if (readOnly) return; const shifted = fromBand(item); const addr = addrAt(shifted[0], shifted[1]); const cell = cellAt(tab, addr); @@ -7466,6 +7518,7 @@ export default function SheetGrid({ // either any more (`getCellContent` above), so activating it opens THIS component's own // floating editor instead -- same door, same gesture, a different cell kind. onCellActivated={(item: Item) => { + if (readOnly) return; const addr = addrAt(item[0], item[1]); const cell = cellAt(tab, addr); const chrome = viewChromeRefusal(cell); @@ -8262,7 +8315,7 @@ export default function SheetGrid({ // ⭐ R5 (W52-T09): `editIntent`'s "edit" vs. "convert" distinction is retired, and this // was always `true` regardless of it - the Edit button opens the binding panel for // every kind of cell, bound or not. - canEdit + canEdit={!readOnly} busy={busy} onEdit={() => onEdit(selected.addr, selected.cell)} onViewRows={() => onViewRows(selected.addr, selected.cell)} diff --git a/web/src/sheet/SheetPage.tsx b/web/src/sheet/SheetPage.tsx index a4a4c5962ed2701b6bd627087e5f053b46ceb485..7759f8337240bab8a7b7b6400d8c0c39f94dabc9 100644 --- a/web/src/sheet/SheetPage.tsx +++ b/web/src/sheet/SheetPage.tsx @@ -56,6 +56,7 @@ import type { DeletedItem } from "../starred/starredApi"; import type { SheetAddress } from "./index"; import { parseRef } from "./a1"; import BindingEditor from "./BindingEditor"; +import PivotTab from "./PivotTab"; import DrillModal from "./DrillModal"; import type { DrillCellScope } from "./DrillModal"; import FormulaBar from "./FormulaBar"; @@ -94,7 +95,7 @@ import { } from "./sheetState"; import type { MovedBlock, SheetHistory, StructureOp } from "./sheetState"; import { bindingDisplay, emptyIndex, ROOT_FOLDER_ID, sheetExtent, withBindingDisplay } from "./types"; -import type { BindingDisplay, Cell, IndexRepair, SheetChartSpec, SheetPrintSettings, SheetRule, SheetRowGroup, SheetTab, SheetTabView, Workbook } from "./types"; +import type { BindingDisplay, Cell, IndexRepair, PivotSpec, SheetChartSpec, SheetPrintSettings, SheetRule, SheetRowGroup, SheetTab, SheetTabView, Workbook } from "./types"; import { ChartWorkbookContext } from "./ChartDialog"; import { SheetExportContext } from "./Ribbon"; @@ -390,6 +391,7 @@ export default function SheetPage() { const [lastWorkbookId] = useState(() => readLastWorkbook()); const [sheetHash, setSheetHash] = useState(() => window.location.hash); const [addressSelectionKey, setAddressSelectionKey] = useState(0); + const [cellAddressRequest, setCellAddressRequest] = useState(0); const [tabId, setTabId] = useState(""); const [unreadableAddressTab, setUnreadableAddressTab] = useState(""); const [pendingTab, setPendingTab] = useState<{ @@ -548,6 +550,8 @@ export default function SheetPage() { const dirtyRef = useRef>>(new Map()); /** Set by anything a cell PATCH cannot express: a row, a column, a tab, a workbook name. */ const structureDirtyRef = useRef(false); + /** A refused whole snapshot cannot save again until a full read replaces its cells. */ + const workbookConflictRef = useRef(null); /** * The probe's answer, remembered for the session. C3: *"The client probes PATCH once per * session and falls back to PUT on 404, saying so in the status line."* @@ -1463,14 +1467,32 @@ export default function SheetPage() { [applyLocal, markAllLoaded, markDirty, reloadTab], ); + /** Discard the refused tree; a failed read keeps every later save behind this read fence. */ + const reloadWholeWorkbookAfterConflict = useCallback(async (workbookId: string) => { + workbookConflictRef.current = workbookId; + savedCleanRef.current = false; + dirtyRef.current = new Map(); + structureDirtyRef.current = false; + const fresh = await readWorkbook(workbookId); + if (bookRef.current?.id !== workbookId) return; + if (!fresh.ok) { + setNotice("Your edit was not saved. Reload the workbook before editing again."); + return; + } + historyRef.current = emptyHistory(); + editSeqRef.current += 1; + // Edits made during this read belong to the refused tree too, including shifted addresses. + dirtyRef.current = new Map(); + structureDirtyRef.current = false; + markAllLoaded(fresh.value); + applyLocal(fresh.value); + workbookConflictRef.current = null; + setNotice("This workbook changed. Your edit was not saved and the workbook was reloaded."); + }, [applyLocal, markAllLoaded]); + /** * Send the workbook the ref currently holds. Structure changes and the PATCH fallback only. - * - * ⚠ THE SERVER'S ANSWER IS ADOPTED ONLY WHEN NOTHING MOVED WHILE IT WAS AWAY. If more - * keystrokes landed during the round trip, taking `saved.value` wholesale would paint the - * workbook as it was BEFORE those keystrokes and they would vanish from the screen while - * staying in the ref. In that case only `rev` and `updated_at` are taken, which is the one - * thing the client could not have known. [[a-rebuilt-value-drops-its-new-field]] + * Adopt the server's full answer only if no keystrokes landed during the round trip. */ const sendWholeWorkbook = useCallback(async (structural = false) => { const opening = bookRef.current; @@ -1506,47 +1528,19 @@ export default function SheetPage() { namedRanges: current.namedRanges, structureChanged: structural, }; - let retried = false; - let saved = await writeWorkbook(current.id, payload); - if (bookRef.current?.id !== current.id) return; - if (!saved.ok && saved.code === "workbook_revision_stale" - && typeof saved.rev_current === "number") { - applyLocal({ ...bookRef.current, rev: saved.rev_current }); - retried = true; - saved = await writeWorkbook(current.id, { ...payload, rev: bookRef.current.rev }); - } + const saved = await writeWorkbook(current.id, payload); if (bookRef.current?.id !== current.id) return; if (!saved.ok) { - // ⛔ A STALE REV IS NOT A DEAD SESSION. Leaving the local `rev` behind would make every - // later save stale too, so editing would be over until someone reloaded the page. The - // workbook is re-read instead, and the reader is told the edit did not land. - if (saved.code === "sheet_structure_stale" || saved.code === "workbook_revision_stale" && retried) { - if (saved.code !== "sheet_structure_stale" && typeof saved.rev_current === "number") - applyLocal({ ...bookRef.current, rev: saved.rev_current }); - dirtyRef.current = new Map(); - structureDirtyRef.current = false; - const fresh = await readWorkbook(current.id); - if (fresh.ok && bookRef.current?.id === current.id) { - historyRef.current = emptyHistory(); - editSeqRef.current += 1; - markAllLoaded(fresh.value); - applyLocal(fresh.value); - } - if (bookRef.current?.id !== current.id) return; - // ⚠ AND HERE THE FLAG STAYS OFF, deliberately. The re-read REPLACED the tree the - // structure change was made in, so there is nothing left to re-send; the sentence above - // is what tells the reader their change did not land. - savedCleanRef.current = false; - setNotice("This workbook changed again while saving, so your edit was not saved and the workbook was reloaded."); + // A current revision does not make these old cells current. Never re-send this snapshot. + if (saved.code === "sheet_structure_stale" || saved.code === "workbook_revision_stale") { + await reloadWholeWorkbookAfterConflict(current.id); return; } // A refusal that is not a revision conflict leaves the local tree intact, so the pending // structure change is still pending and the next drain must still carry it. structureDirtyRef.current = true; savedCleanRef.current = false; - setNotice(saved.code === "workbook_revision_stale" - ? "The current workbook revision was not returned, so reload the workbook and try again." - : saved.message); + setNotice(saved.message); return; } if (bookRef.current?.id !== current.id) return; @@ -1556,7 +1550,7 @@ export default function SheetPage() { } else if (bookRef.current?.id === current.id) { applyLocal({ ...bookRef.current, rev: saved.value.rev, updated_at: saved.value.updated_at }); } - }, [applyLocal, ensureTabs, markAllLoaded]); + }, [applyLocal, ensureTabs, markAllLoaded, reloadWholeWorkbookAfterConflict]); /** * ⭐ ONE DRAIN, BEHIND WHATEVER SAVE IS ALREADY IN FLIGHT. @@ -1568,6 +1562,10 @@ export default function SheetPage() { const drainSave = useCallback(() => { saveChainRef.current = saveChainRef.current.then(async () => { if (!bookRef.current) return; + if (workbookConflictRef.current === bookRef.current.id) { + await reloadWholeWorkbookAfterConflict(bookRef.current.id); + return; + } const structural = structureDirtyRef.current; structureDirtyRef.current = false; const hasPendingCells = dirtyRef.current.size > 0; @@ -1593,7 +1591,7 @@ export default function SheetPage() { const after = bookRef.current; if (structural && after && tabIdRef.current) refreshOpenedWorkbook(after.id, tabIdRef.current); }); - }, [sendDirtyCells, sendWholeWorkbook, refreshOpenedWorkbook]); + }, [sendDirtyCells, sendWholeWorkbook, refreshOpenedWorkbook, reloadWholeWorkbookAfterConflict]); useEffect(() => { drainRef.current = drainSave; }, [drainSave]); @@ -2018,6 +2016,7 @@ export default function SheetPage() { // ── the tab bar (wave-49 R7) ─────────────────────────────────────────────────────────────── const onAddTab = useCallback(() => { + if (!canEditOutline) return; const base = bookRef.current; if (!base) return; const made = addTab(base); @@ -2026,16 +2025,41 @@ export default function SheetPage() { // and must never be fetched: a lazy read of it would 404 or answer an empty tab. tabLoadedRef.current.add(tabKeyOf(base.id, made.id)); setTabId(made.id); - }, [commitSheets]); + }, [commitSheets, canEditOutline]); + + const onCreatePivot = useCallback(() => { + if (!canEditOutline) return; + const base = bookRef.current; + if (!base) return; + const made = addTab(base, { kind: "pivot", pivot: { + database: "", rows: [], columns: [], values: [], filters: [], + } }); + commitSheets(made.sheets); + tabLoadedRef.current.add(tabKeyOf(base.id, made.id)); + setEditAt(""); + setDrillAt(""); + setTabId(made.id); + }, [commitSheets, canEditOutline]); + + const onPivotChange = useCallback((pivot: PivotSpec) => { + if (!canEditOutline) return; + const base = bookRef.current; + if (!base) return; + const target = openTabId(base); + commitSheets(base.sheets.map(t => t.id === target && t.kind === "pivot" + ? { ...t, pivot } : t)); + }, [commitSheets, canEditOutline, openTabId]); const onMoveTab = useCallback((id: string, step: number) => { + if (!canEditOutline) return; const base = bookRef.current; setTabMenu(null); if (base) commitSheets(moveTab(base, id, step)); - }, [commitSheets]); + }, [commitSheets, canEditOutline]); /** Capture the whole tab, preserving the exact point inside it that the pointer grabbed. */ const startTabDrag = useCallback((id: string, event: ReactPointerEvent) => { + if (!canEditOutline) return; if (event.button !== 0 || !bookRef.current || tabDragRef.current) return; const element = event.currentTarget; const rect = element.getBoundingClientRect(); @@ -2056,7 +2080,7 @@ export default function SheetPage() { targetIndex: startIndex, moved: false, }; - }, []); + }, [canEditOutline]); /** * Gesture frames write only a transform and a target index. React state would re-layout the @@ -2130,6 +2154,7 @@ export default function SheetPage() { }, [commitSheets]); const onRenameTab = useCallback(() => { + if (!canEditOutline) return; const pending = tabRename; const base = bookRef.current; if (!pending || !base) return; @@ -2140,9 +2165,10 @@ export default function SheetPage() { } setTabRename(null); commitSheets(renameTab(base, pending.id, pending.name)); - }, [commitSheets, tabRename]); + }, [commitSheets, tabRename, canEditOutline]); const onRemoveTab = useCallback((id: string) => { + if (!canEditOutline) return; const base = bookRef.current; setTabMenu(null); if (!base) return; @@ -2159,7 +2185,7 @@ export default function SheetPage() { } commitSheets(removeTab(base, id)); if (tabIdRef.current === id) setTabId(base.sheets.find((t) => t.id !== id)?.id ?? ""); - }, [commitSheets]); + }, [commitSheets, canEditOutline]); const confirmRemoveTab = useCallback(() => { const pending = tabDelete; @@ -3170,6 +3196,13 @@ export default function SheetPage() { {opening || addressMismatch || tabPending ? ( ) : book && tab ? ( + tab.kind === "pivot" ? : { + openAtRef.current = { workbookId: book.id, tabId: tab.id, addr }; + setBarAddr(addr); + setCellAddressRequest((request) => request + 1); + }} onCommit={commitCells} draft={cellDraft} />} @@ -3295,7 +3333,7 @@ export default function SheetPage() { } openTab(t.id); }} - onDoubleClick={() => setTabRename({ id: t.id, name: t.name })} + onDoubleClick={() => { if (canEditOutline) setTabRename({ id: t.id, name: t.name }); }} onPointerDown={(event) => startTabDrag(t.id, event)} onPointerMove={moveTabDrag} onPointerUp={(event) => finishTabDrag(event, true)} @@ -3308,6 +3346,7 @@ export default function SheetPage() { e.preventDefault(); const el = e.currentTarget; openTab(t.id); + if (!canEditOutline) return; setTabMenu({ id: t.id, el }); }} title="Click to open. Drag to reorder. Double click to rename. Right click for more." @@ -3319,6 +3358,7 @@ export default function SheetPage() { @@ -1415,6 +1425,13 @@ export default function SheetRail({ setImporting(false)} + onSummary={(summary) => { + setImportSummary(summary); + if (summary) { + setRailShut(false); + if (narrow) setNarrowOpen(true); + } + }} onOpened={(workbookId) => { setImporting(false); onOpen(workbookId); }} /> ) : null} diff --git a/web/src/sheet/formulaEval.ts b/web/src/sheet/formulaEval.ts index e1ccea81cdc451d4ab665c21a60e0e6200ba04e0..06a20c22a7b63db274b5dbb312812bc32f11a06c 100644 --- a/web/src/sheet/formulaEval.ts +++ b/web/src/sheet/formulaEval.ts @@ -2803,7 +2803,7 @@ const cyclicComponents = (nodes: Set, reads: Map>): } return result; }; -const sameFormat = (a: unknown, b: unknown): boolean => { +export const sameFormat = (a: unknown, b: unknown): boolean => { const stable = (v: unknown): string => v && typeof v === "object" && !Array.isArray(v) ? JSON.stringify(Object.keys(v).sort().map((k) => [k, stable((v as Record)[k])])) : JSON.stringify(v); return stable(a) === stable(b); diff --git a/web/src/sheet/ribbonInsert.tsx b/web/src/sheet/ribbonInsert.tsx index 3566484e9b24187eeb1f4cff849fd441f75dd33d..4187fb22d24c3720a8cc51e28395a7524c5dac15 100644 --- a/web/src/sheet/ribbonInsert.tsx +++ b/web/src/sheet/ribbonInsert.tsx @@ -4,7 +4,7 @@ // ⭐⭐ REGISTERED NOW, AND IT WAS NOT BEFORE ON PURPOSE. The base kit shipped this module exporting // its descriptor with an EMPTY group list, and `Ribbon.tsx`'s `TABS` carried Home alone, because a // registered empty tab paints a clickable tab that opens a blank toolbar row. W54-T10 gives the tab -// its four groups and adds its descriptor to that array in the same change, which is the whole of +// its groups and adds its descriptor to that array in the same change, which is the whole of // the wiring this module owes: the shell reads `keys` for the roving order, `groups` for the row, // and the rest of the descriptor for the tab button. // @@ -16,11 +16,8 @@ // `insertGroups` the groups, as a function of the shell's `RibbonTabContext`. // `INSERT_TAB` the descriptor. // -// ⛔⛔ RULING R3 — FOUR GROUPS, AND THE ABSENCES ARE THE RULING. Excel's own Insert tab in -// `reference/EXCEL 2.png` also carries Tables, PivotTable, Recommended PivotTables, Illustrations, -// Slicer, Timeline, Text and Symbols. NONE of them is here and none of them is coming: the DATA -// module exists exactly for tables and pivots, and R3 names the rest out. Ours is Charts, -// Sparklines, Links and Comments. +// S70 R14 revokes wave 54 R3's PivotTable exclusion. PivotTable now creates its own worksheet; +// it uses a workbook callback alongside the cell/chart doors, leaving RibbonActionKind stable. // // ⛔⛔ THESE CONTROLS DO NOT ADD A NAMED ACT, AND THAT IS DELIBERATE. `RibbonActionKind` is closed // by an exhaustive switch in `SheetGrid.tsx`, a file this lane does not own, so a new member would @@ -112,6 +109,7 @@ const SPARKLINES = ["sparkline-line", "sparkline-column", "sparkline-winloss"] a const LINKS = ["link"] as const; const COMMENTS = ["comment"] as const; const CELL_DATA = ["cell-data"] as const; +const PIVOTS = ["pivot-table"] as const; /** * ⭐⭐ THE INSERT TAB'S ROVING ORDER, WHICH IS ALSO ITS TYPE. The shell spreads this into the union @@ -121,7 +119,7 @@ const CELL_DATA = ["cell-data"] as const; * call rendering it makes ArrowRight land on nothing, with every gate still green, so the two are * changed together or not at all. */ -export const INSERT_KEYS = [...CHARTS, ...SPARKLINES, ...LINKS, ...COMMENTS, ...CELL_DATA] as const; +export const INSERT_KEYS = [...PIVOTS, ...CHARTS, ...SPARKLINES, ...LINKS, ...COMMENTS, ...CELL_DATA] as const; /** * ⭐ THE EIGHT KINDS AND THE FALLBACK RECTANGLE BOTH MOVED TO `ChartDialog.tsx` (W54-T11), which @@ -528,6 +526,15 @@ export const insertGroups = (ctx: RibbonTabContext) => { growing a ninth argument this lane may not give it. The rules that strip its own box live in `styles/wave54/ribbon-insert.css`. */
+ {group("Tables", large(<> + {control("pivot-table", "PivotTable", <> + + {word("PivotTable")} + , () => { insert?.onCreatePivot?.(); closePop(); }, + false, undefined, "T", undefined, !insert?.onCreatePivot)} + ))} {group("Charts", ( <> {large(<> diff --git a/web/src/sheet/sheetState.ts b/web/src/sheet/sheetState.ts index 466f4b0abda93bf2e1f4c6e6ba170b2f6f16a9d5..059e0b6688a7c640096ca33c3334943759271d12 100644 --- a/web/src/sheet/sheetState.ts +++ b/web/src/sheet/sheetState.ts @@ -12,7 +12,7 @@ import type { BindingOnDrag, BindingPeriod, Cell, CellFormat, CellMap, CellState, - PeriodUnit, Workbook, SheetTab, SheetTabView, SheetChartSpec, SheetRule, SheetRowGroup, StructureOp, RowHeightsPatch, + PeriodUnit, PivotSpec, Workbook, SheetTab, SheetTabView, SheetChartSpec, SheetRule, SheetRowGroup, StructureOp, RowHeightsPatch, } from "./types"; import { asNumber, bindingOnDrag, bindingPeriod, BINDING_PERIOD_STEP, BINDING_ENDING_STEP, formatCellText, chartDocumentGeometry, chartRangeAfterStructure, @@ -42,7 +42,7 @@ import type { SavedViewShadow } from "./bindingRefs"; // one door that turns this file's `Cell` into that shape, so the evaluator itself never has to. import { compileSheetFormula, CYCLE_REFUSAL, ERROR_REF, evaluateCompiledSheetFormula, evaluateFormula, - formulaReferenceKey, WorkbookCalculation as FormulaCalculation, workbookFormulaGetter, + formulaReferenceKey, sameFormat, WorkbookCalculation as FormulaCalculation, workbookFormulaGetter, } from "./formulaEval"; import type { CellValue, QualifiedFormula, SpillChildMarker, FormulaErrorCode } from "./formulaEval"; import { copyCell, replaceCellStyle } from "./cellStyleOps"; @@ -266,7 +266,8 @@ export const cellToValue = (cell: Cell | null | undefined): CellValue => { if (cell.value == null) { // ⛔ A BLANK LITERAL IS EMPTY; A BOUND OR FORMULA CELL WITH NOTHING COMPUTED IS PENDING. The // difference is a confident zero versus an honest "not yet", and it is the whole of the trap. - return cell.kind === "literal" ? { kind: "empty", ...metadata } : { kind: "pending", ...metadata }; + return cell.kind === "literal" || cell.state === "fresh" + ? { kind: "empty", ...metadata } : { kind: "pending", ...metadata }; } const pending = cell.kind !== "literal" && cell.state === "stale"; if (typeof cell.value === "boolean") return { kind: "boolean", value: cell.value, pending, ...metadata }; @@ -377,11 +378,12 @@ export const setFormula = ( delete rest.spill; delete rest.spillParent; delete rest.error; - if (rest.formulaMeta) { - rest.formulaMeta = { ...rest.formulaMeta }; - delete rest.formulaMeta.spillChild; - delete rest.formulaMeta.spillClear; + const inferred = rest.formulaMeta?.inferredFormat as { version?: number; format?: unknown } | undefined; + if (inferred?.version === 1 && sameFormat(inferred.format, rest.style?.format)) { + rest.style = { ...rest.style }; + delete rest.style.format; } + delete rest.formulaMeta; delete rest.binding; delete rest.source; delete rest.refusal; @@ -3042,11 +3044,12 @@ export const nextTabName = (sheets: readonly SheetTab[]): string => { return `Sheet${sheets.length + 1}`; }; -export const addTab = (book: Workbook): { sheets: SheetTab[]; id: string } => { +export const addTab = (book: Workbook, options?: { kind: "pivot"; pivot: PivotSpec }): { sheets: SheetTab[]; id: string } => { const id = newTabId(); return { id, - sheets: [...book.sheets, { id, name: nextTabName(book.sheets), cells: {} }], + sheets: [...book.sheets, { id, name: nextTabName(book.sheets), cells: {}, + ...(options ? { kind: options.kind, pivot: structuredClone(options.pivot) } : {}) }], }; }; diff --git a/web/src/sheet/sparkline.ts b/web/src/sheet/sparkline.ts index 20ba19dad30e813c2c395438674124c9c883561c..8877cd261d138103c834e906f5acbafc034a447f 100644 --- a/web/src/sheet/sparkline.ts +++ b/web/src/sheet/sparkline.ts @@ -1,37 +1,14 @@ // --------------------------------------------------------------------------------------------- -// sheet/sparkline.ts — AN IN-CELL PICTURE OF A RANGE. Wave 54 lane B (W54-T13, instruction 5). -// -// ⭐ IT WAS WIRED BEFORE IT DID ANYTHING, WHICH IS WHY THIS FILE IS THE ONLY ONE THAT MOVED. The -// call site in `SheetGrid.tsx::paintCell` has existed since the base kit and answered `false`; the -// only change that file takes for this ticket is the fourth argument below, because a sparkline -// READS A RANGE and a paint callback is only ever handed its own cell. -// -// ⛔ THE PARAMETER NAMES CARRY A LEADING UNDERSCORE ONLY WHILE THEY ARE UNREAD. -// `tsconfig.app.json` sets `noUnusedParameters: true`, so a parameter a body never touches must -// wear one or the file does not compile. All four are read now, so none of them wears one; the -// note stays because the next parameter added here inherits the rule. -// -// ⛔⛔ TWO ANSWERS, AND THE CELL'S OWN TEXT IS WHAT PICKS BETWEEN THEM. The seam reads a `true` as -// "this cell's WHOLE paint belonged to the sparkline" and returns before the default renderer: -// • a cell with NO text of its own — the ordinary case, `kind: "empty"` carrying only the spec — -// is drawn in full and answers `true`; -// • a cell that ALSO holds text draws the sparkline as an UNDERLAY, quietened, and answers -// `false`, so `paintCell` goes on to lay that cell's value over the top of it. Owning the -// paint there would mean re-implementing the styled text renderer, alignment, wrap, number -// format and all, in a file whose job is to draw three marks. -// ⚠ Driven in a browser rather than reasoned about: glide 6.0.3 fills a cell's background BEFORE -// `drawCell`, and `drawContent()` paints the value alone, so an underlay survives into the frame. -// The one case it does not survive is a cell carrying an explicit `style.fill`, which `paintCell` -// lays down itself after this function has returned — see the handoff's PENDING row. -// -// ⚠ A `true` ALSO SKIPS `ruleStyleAt` AND ALL FOUR OVERLAY CLOSURES (reference border, copy -// marquee, shimmer, corner mark), which the seam's own comment warns about. That is accepted for -// the no-text case: those four all describe a cell's VALUE, and a cell drawn by this function has -// none. The moment a cell has both, the `false` branch hands every one of them back. +// sheet/sparkline.ts — an in-cell picture of a range. +// SheetGrid paints the resolved background first, these marks next, and the value and selection +// overlays last. A sparkline never takes over the cell's conditional rules, text or overlays. +// Source readings use the underlying numeric value; presentation such as `(370)` cannot change +// the sign. Gaps remain gaps. Geometry scales with the grid zoom, including its minimum size. // --------------------------------------------------------------------------------------------- import { tokenColour } from "./cellBorders"; import { addressOf } from "./sheetState"; +import { asNumber } from "./types"; import type { Cell, CellMap } from "./types"; interface Rect { @@ -75,9 +52,8 @@ const valuesIn = ( const out: (number | null)[] = []; for (let r = r0; r <= r1; r += 1) { for (let c = c0; c <= c1; c += 1) { - const text = textOf(cells[addressOf(c, r)]).trim(); - const n = text === "" ? Number.NaN : Number(text.replace(/,/g, "")); - out.push(Number.isFinite(n) ? n : null); + const cell = cells[addressOf(c, r)]; + out.push(cell?.state === "refused" ? null : asNumber(cell?.value)); } } return out; @@ -91,7 +67,7 @@ const centreX = (box: Rect, i: number, n: number): number => * A POLYLINE, BROKEN AT EVERY GAP. Scaled to the readings that are present, so a flat run still * reads as flat rather than being stretched into noise: an empty span is drawn on the middle line. */ -const drawLine = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | null)[]): void => { +const drawLine = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | null)[], zoom: number): void => { const present = values.filter((v): v is number => v !== null); if (!present.length) return; const lo = Math.min(...present); @@ -102,7 +78,7 @@ const drawLine = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | nu ctx.save(); ctx.strokeStyle = tokenColour("--lp-blue-deep", "#768fb6"); - ctx.lineWidth = 1.25; + ctx.lineWidth = Math.max(0.5, 1.25 * zoom); ctx.lineJoin = "round"; ctx.lineCap = "round"; ctx.beginPath(); @@ -125,7 +101,7 @@ const drawLine = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | nu const i = values.findIndex((v) => v !== null); ctx.fillStyle = tokenColour("--lp-blue-deep", "#768fb6"); ctx.beginPath(); - ctx.arc(centreX(box, i, values.length), yOf(present[0]), 1.4, 0, Math.PI * 2); + ctx.arc(centreX(box, i, values.length), yOf(present[0]), 1.4 * zoom, 0, Math.PI * 2); ctx.fill(); } ctx.restore(); @@ -139,7 +115,7 @@ const drawLine = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | nu * zero gets the line drawn where zero actually falls. A bar chart whose axis starts at the * smallest reading is the classic lie, and it is one line of arithmetic to avoid. */ -const drawColumn = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | null)[]): void => { +const drawColumn = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | null)[], zoom: number): void => { const present = values.filter((v): v is number => v !== null); if (!present.length) return; const lo = Math.min(0, ...present); @@ -149,7 +125,7 @@ const drawColumn = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | const zero = yOf(0); const slot = box.width / values.length; - const barW = Math.max(1, Math.min(slot - 1, slot * 0.72)); + const barW = Math.max(zoom, Math.min(slot - zoom, slot * 0.72)); const up = tokenColour("--lp-blue-solid", "#5b8fd9"); const down = tokenColour("--lp-red-deep", "#a3453c"); @@ -159,7 +135,7 @@ const drawColumn = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | const x = box.x + slot * i + (slot - barW) / 2; const y = yOf(v); const top = Math.min(y, zero); - const height = Math.max(1, Math.abs(zero - y)); + const height = Math.max(zoom, Math.abs(zero - y)); ctx.fillStyle = v < 0 ? down : up; ctx.fillRect(x, top, barW, height); }); @@ -167,10 +143,10 @@ const drawColumn = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | // The baseline only earns its pixel when readings actually sit on both sides of it. if (lo < 0 && hi > 0) { ctx.strokeStyle = tokenColour("--lp-line", "#e6e9ef"); - ctx.lineWidth = 1; + ctx.lineWidth = Math.max(0.5, zoom); ctx.beginPath(); - ctx.moveTo(box.x, Math.round(zero) + 0.5); - ctx.lineTo(box.x + box.width, Math.round(zero) + 0.5); + ctx.moveTo(box.x, zero); + ctx.lineTo(box.x + box.width, zero); ctx.stroke(); } ctx.restore(); @@ -183,20 +159,20 @@ const drawColumn = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | * make this a second, quieter column chart and delete the only kind that answers "how often". * Zero is neither, and so is a gap: both draw nothing. */ -const drawWinLoss = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | null)[]): void => { +const drawWinLoss = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | null)[], zoom: number): void => { const mid = box.y + box.height / 2; - const markH = Math.max(2, box.height * 0.34); + const markH = Math.max(2 * zoom, box.height * 0.34); const slot = box.width / values.length; - const barW = Math.max(1, Math.min(slot - 1, slot * 0.72)); + const barW = Math.max(zoom, Math.min(slot - zoom, slot * 0.72)); const up = tokenColour("--lp-blue-deep", "#768fb6"); const down = tokenColour("--lp-red-deep", "#a3453c"); ctx.save(); ctx.strokeStyle = tokenColour("--lp-line", "#e6e9ef"); - ctx.lineWidth = 1; + ctx.lineWidth = Math.max(0.5, zoom); ctx.beginPath(); - ctx.moveTo(box.x, Math.round(mid) + 0.5); - ctx.lineTo(box.x + box.width, Math.round(mid) + 0.5); + ctx.moveTo(box.x, mid); + ctx.lineTo(box.x + box.width, mid); ctx.stroke(); values.forEach((v, i) => { @@ -211,26 +187,26 @@ const drawWinLoss = (ctx: CanvasRenderingContext2D, box: Rect, values: (number | /** * Draw an in-cell sparkline over the range its spec names. * - * @returns `true` when this cell's whole paint belonged to the sparkline, `false` when the caller - * must go on painting — which is both "there was nothing to draw" and "the mark is an - * underlay and the cell's own text still has to land on top of it". + * @returns whether the mark has no value text above it. Callers always finish painting the cell; + * this compatibility result does not transfer ownership of the paint pass. */ export function drawSparkline( ctx: CanvasRenderingContext2D, rect: Rect, cell: Cell, cells: CellMap, + zoom = 1, ): boolean { const spec = cell.sparkline; - if (!spec) return false; + if (!spec || !Number.isFinite(zoom) || zoom <= 0) return false; const box: Rect = { - x: rect.x + INSET, - y: rect.y + INSET, - width: rect.width - INSET * 2, - height: rect.height - INSET * 2, + x: rect.x + INSET * zoom, + y: rect.y + INSET * zoom, + width: rect.width - INSET * zoom * 2, + height: rect.height - INSET * zoom * 2, }; - if (box.width < MIN_SIDE || box.height < MIN_SIDE) return false; + if (box.width < MIN_SIDE * zoom || box.height < MIN_SIDE * zoom) return false; const values = valuesIn(cells, spec.range); if (!values.length) return false; @@ -241,14 +217,14 @@ export function drawSparkline( // The mark is clipped to its own cell for the same reason the text is: a wide range drawn in a // narrow column must stop at the column edge rather than reach into its neighbour. ctx.beginPath(); - ctx.rect(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2); + ctx.rect(rect.x + zoom, rect.y + zoom, rect.width - 2 * zoom, rect.height - 2 * zoom); ctx.clip(); // ⭐ QUIETER UNDER TEXT, FULL STRENGTH ALONE. The underlay has to stay a background: at full // opacity a column chart competes with the digits sitting on it and neither one wins. if (hasText) ctx.globalAlpha = 0.45; - if (spec.kind === "line") drawLine(ctx, box, values); - else if (spec.kind === "column") drawColumn(ctx, box, values); - else drawWinLoss(ctx, box, values); + if (spec.kind === "line") drawLine(ctx, box, values, zoom); + else if (spec.kind === "column") drawColumn(ctx, box, values, zoom); + else drawWinLoss(ctx, box, values, zoom); ctx.globalAlpha = 1; ctx.restore(); diff --git a/web/src/sheet/types.ts b/web/src/sheet/types.ts index f8bb14edd16a42adb8a4d24d458382c62b9d8cec..c880d6a3b3bbc67cf38056a62df92815947e7d4c 100644 --- a/web/src/sheet/types.ts +++ b/web/src/sheet/types.ts @@ -176,6 +176,8 @@ export interface BindingFilterClause { value: string; /** Set operands stay separate, including singleton Step drivers. */ values?: string[]; + /** Preserve a compared field for related-offer readiness. */ + rhs?: { colId: string }; /** True when the clause holds a SET rather than one value (the `values` operand). */ multiValued: boolean; /** True on a MEASURE condition (a leaf carrying a window), which is not a column clause. */ @@ -207,11 +209,13 @@ export const bindingFilterClauses = (tree: unknown): BindingFilterClause[] => { // ⚠ First occurrence wins because drag selects a DRIVER COLUMN and rewrites every clause // on that column; a second entry would offer the same drag twice. seen.add(colId); + const rhs = asRecord(node.rhs); clauses.push({ colId, op: typeof node.op === "string" ? node.op : "", value: typeof node.value === "string" ? node.value : "", ...(Array.isArray(node.values) ? { values: node.values.map(String) } : {}), + ...(rhs && typeof rhs.colId === "string" ? { rhs: { colId: rhs.colId } } : {}), // ⛔ A non-empty `values` operand is a SET, while `window` marks a MEASURE condition; // preserving both facts lets the panel explain why neither is one ordered option. multiValued: Array.isArray(node.values) && node.values.length > 0, @@ -1392,9 +1396,53 @@ export interface SheetRowGroup { /** A tab-level dimension patch. Null removes one override; it never enters SheetTabView. */ export type RowHeightsPatch = Record; +/** S70 C6: the aggregate reads every matching row on the server. */ +export interface PivotSpec { + database: string; + rows: string[]; + columns: string[]; + values: { field: string; agg: "sum" | "count" | "avg" | "min" | "max" }[]; + filters: import("../customer-grid/types").FilterNode[]; +} + +export type PivotAxis = string | number | boolean | null | (string | number | boolean | null)[]; +export interface PivotAnswer { + ok: true; + rows: PivotAxis[]; + columns: PivotAxis[]; + cells: (number | null)[][]; + total_rows: number; + limits: unknown; +} + +/** C6 leaves axis labels open: support scalar and tuple entries without deriving buckets. */ +export const pivotAxisLabel = (axis: PivotAxis): string => { + const parts = Array.isArray(axis) ? axis : [axis]; + return parts.length ? parts.map(part => part === null || part === "" ? "Empty" : String(part)).join(" / ") : "Total"; +}; + +/** Refuse a malformed matrix instead of painting a partial or invented total. */ +export const readPivotAnswer = (raw: unknown): PivotAnswer | null => { + const answer = asRecord(raw); + const scalar = (part: unknown) => part === null || typeof part === "string" + || typeof part === "boolean" || (typeof part === "number" && Number.isFinite(part)); + const axis = (entry: unknown) => Array.isArray(entry) ? entry.every(scalar) : scalar(entry); + if (!answer || answer.ok !== true || !Array.isArray(answer.rows) || !answer.rows.every(axis) + || !Array.isArray(answer.columns) || !answer.columns.every(axis) + || !Array.isArray(answer.cells) || answer.cells.length !== answer.rows.length + || !answer.cells.every(row => Array.isArray(row) && row.length === (answer.columns as unknown[]).length + && row.every(cell => cell === null || (typeof cell === "number" && Number.isFinite(cell)))) + || typeof answer.total_rows !== "number" || !Number.isSafeInteger(answer.total_rows) + || answer.total_rows < 0 || !("limits" in answer)) return null; + return answer as unknown as PivotAnswer; +}; + export interface SheetTab { id: string; name: string; + /** Absent on legacy workbooks means grid. Server round trip is S70-T22. */ + kind?: "grid" | "pivot"; + pivot?: PivotSpec; cells: CellMap; usedExtent?: { col0: number; row0: number; col1: number; row1: number } | null; /** Exact document pixels, keyed by 1-based row number; overrides the base and wrap floor. */ diff --git a/web/src/shell/Brand.tsx b/web/src/shell/Brand.tsx index 8d75d0f45f4e5de5b2c6b2b4c3fa7992df60d3b5..903f288a5f86eff938dd02e5ce891f043a94fab2 100644 --- a/web/src/shell/Brand.tsx +++ b/web/src/shell/Brand.tsx @@ -1,22 +1,12 @@ // --------------------------------------------------------------------------- // shell/Brand.tsx — the Loopable lockup: the mark, plus the wordmark on request. // -// ⚠ THIS COMPONENT DRAWS NOTHING. It POINTS at the generated artifact, and that -// is the entire point of the file. -// -// The mark's one source of truth is `_brandmark.loopable_svg()` in -// platform (wave-10 I24: an open loop of continuously tapering weight, -// monotone #6E56CF). `platform/static/loopable-mark.svg` and -// `web/public/favicon.svg` are GENERATED from it, and that file's own header -// says: "Never redraw a third copy." -// -// It was redrawn anyway. Shell.tsx carried a hand-typed TWO-CONCENTRIC-RINGS -// mark (the wave-9 geometry, blue + green) under a comment asserting it was -// "identical to app.py `_loopable_svg` and public/favicon.svg — one mark, three -// painters". I24 replaced the geometry upstream and the copy did not follow, so -// the standalone shell has been painting LAST WAVE'S BRAND while claiming -// parity in a comment. A comment claiming parity is not parity; consuming the -// artifact is. Hence , not . +// Mark consumes favicon.svg as an ; it does not draw a third copy. +// `web/public/favicon.svg` and `platform/static/loopable-mark.svg` are the +// byte-identical sources. There is no geometry generator. Owner 2026-10-05 +// selects the heart knot: one purple rope on a transparent ground, a heart +// turned 45 degrees so its straight sides are the stem and foot of an L. It +// supersedes SDLC 70 R27's white loop knot on a purple disc. // // BASE_URL rather than a leading slash: `vite.config.ts` sets `base: "./"` so // the bundle works served from any sub-path, and an absolute `/favicon.svg` @@ -42,8 +32,7 @@ export function Mark({ size }: { size: number }) { ); } -/** Mark + wordmark, side by side — the wave-9 I5 lockup, the same object on the - * login card and in the nav so the two doors read as one product. */ +/** Shared mark and wordmark for sign-in and navigation. */ export function Brand({ size, className }: { size: number; className?: string }) { return (
diff --git a/web/src/shell/FreezeNotice.tsx b/web/src/shell/FreezeNotice.tsx new file mode 100644 index 0000000000000000000000000000000000000000..ae9b3c566580721bce859265a095ca367dff0975 --- /dev/null +++ b/web/src/shell/FreezeNotice.tsx @@ -0,0 +1,46 @@ +import { useEffect, useState } from "react"; +import { getFreezeNotice } from "../settings/settingsApi"; +import type { FreezeNoticeState } from "../settings/settingsApi"; + +export function useFreezeNotice(): FreezeNoticeState { + const [notice, setNotice] = useState({ enabled: false, text: "" }); + useEffect(() => { + let live = true; + let request: AbortController | null = null; + const refresh = () => { + if (document.hidden) return; + request?.abort(); + const controller = new AbortController(); + request = controller; + void getFreezeNotice(controller.signal).then((result) => { + if (!live || controller.signal.aborted || !result.ok) return; + const next = result.data; + if (typeof next?.enabled === "boolean" && typeof next.text === "string") { + setNotice((current) => current.enabled === next.enabled && current.text === next.text + ? current : { enabled: next.enabled, text: next.text }); + } + }); + }; + refresh(); + const timer = window.setInterval(refresh, 30_000); + window.addEventListener("focus", refresh); + document.addEventListener("visibilitychange", refresh); + return () => { + live = false; + request?.abort(); + window.clearInterval(timer); + window.removeEventListener("focus", refresh); + document.removeEventListener("visibilitychange", refresh); + }; + }, []); + return notice; +} + +export default function FreezeNotice({ notice, standalone = false }: { + notice?: FreezeNoticeState; + standalone?: boolean; +}) { + if (!notice?.enabled) return null; + const message = {notice.text}; + return standalone ?
{message}
: message; +} diff --git a/web/src/shell/LoginPage.tsx b/web/src/shell/LoginPage.tsx index 998e9cf367f83bf64cc05d75133c6028ab435cc6..f478ade755c6d8f36c9be2d7e6f36a3cd1a4b5c5 100644 --- a/web/src/shell/LoginPage.tsx +++ b/web/src/shell/LoginPage.tsx @@ -1,18 +1,8 @@ // --------------------------------------------------------------------------- // shell/LoginPage.tsx — X5: the branded door. // -// THE CARD LANGUAGE IS `gate()`'s, DELIBERATELY (app.py:1228). Same lockup -// (mark + "Loopable" wordmark, side by side — wave-9 I5), same 410px card with -// ONE hairline closing all four sides (wave-10 I25a removed the gold top-rule: -// "there is an orange line at the top of the sign-in place… it's ugly" — do not -// reintroduce an accent edge here), same two fields and one primary button. -// Two doors into one product must not look like two products, and this is the -// door that outlives the other: `gate()` dies with app.py at EXIT-6. -// -// WHAT IT DOES NOT INHERIT: `gate()`'s `st.empty()` slot dance is a workaround -// for Streamlit replacing elements POSITIONALLY, labelled in its own docstring -// as something "the successor shell deletes". This is that successor. React -// unmounts what it unmounts; there is no ghost card to chase. +// DESIGN.md §4.5 / C1: the shared Loopable lockup and form sit directly on +// the page surface. Every sign-in step keeps the same width and brand anchor. // // NO CLIENT-SIDE BACKOFF (X5: the server owns it). The only local rule is // single-flight — a second submit while one is in the air is dropped, which is @@ -22,6 +12,8 @@ import { useEffect, useRef, useState } from "react"; import type { FormEvent } from "react"; import { Brand } from "./Brand"; +import FreezeNotice from "./FreezeNotice"; +import type { FreezeNoticeState } from "../settings/settingsApi"; import { changePassword, login, passwordPolicy } from "./session"; import type { LoginOutcome, PasswordPolicy, SessionUser } from "./session"; import SecurityPane from "../settings/SecurityPane"; @@ -41,7 +33,10 @@ function EyeIcon({ off }: { off: boolean }) { ); } -export default function LoginPage({ onSignedIn }: { onSignedIn: (user: SessionUser) => void }) { +export default function LoginPage({ onSignedIn, freezeNotice }: { + onSignedIn: (user: SessionUser) => void; + freezeNotice?: FreezeNoticeState; +}) { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); @@ -161,26 +156,11 @@ export default function LoginPage({ onSignedIn }: { onSignedIn: (user: SessionUs return (
+
- {/* ⭐ WAVE 34 R9 — THE ONE LINE OF SELL ON THIS SCREEN. Owner, 2026-08-16: - *"Come up with selling points: Automate all business workflows, add it to the login - page. Don't make Royal Imports think im selling to other businesses though, so just - add a catchy slogan."* - - ⛔ THE SECOND SENTENCE IS THE HARD CONSTRAINT AND IT IS EASY TO MISS. Royal Imports is - tenant #0 and their team signs in here every day; a line that reads like marketing - copy aimed at OTHER companies tells them, correctly, that they are looking at a demo - of a product being sold elsewhere. So this is a PROMISE ABOUT THEIR OWN TOOL, phrased - with no audience in it: no "for teams", no "businesses", no "your company", no plan - names, no call to action. "Every workflow, automated." is a claim the app makes about - itself, which happens to also be the selling point. - - ⚠ No dash of any kind (R6). A slogan is exactly where an em dash wants to appear. */} -

Every workflow, automated.

- - {/* One card and one anchor across every step. The extra content + {/* One form column and one anchor across every step. The extra content grows downward; no alternate width, position or brand lockup. */} {step === "expired" || step === "mfa" ? (

@@ -303,7 +283,7 @@ export default function LoginPage({ onSignedIn }: { onSignedIn: (user: SessionUs {/* The message line is ALWAYS in the layout (min-height: 1lh) so an - arriving error does not shove the card. `role="status"` rather than + arriving error does not move the form. `role="status"` rather than "alert": polite announcement, no interruption. */}

{error} diff --git a/web/src/shell/Shell.tsx b/web/src/shell/Shell.tsx index d4b7080f9fe6e83b73bc4a6f1ffeecc02a9aa0b4..c1b1ceec0e7bbdef64dc22017d9e5eea218591f1 100644 --- a/web/src/shell/Shell.tsx +++ b/web/src/shell/Shell.tsx @@ -95,6 +95,7 @@ import type { DeletedItem } from "../starred/starredApi"; import { Brand } from "./Brand"; import { ErrorBoundary } from "./ErrorBoundary"; import LoginPage from "./LoginPage"; +import FreezeNotice, { useFreezeNotice } from "./FreezeNotice"; import { APP_ROUTE, ARTIFACTS_ROUTE, ASSISTANT_ROUTE, CONNECTORS_ROUTE, DATA_ROUTE, EMPTY_NAV_PREFS, ENVELOPE_KEYS, FEEDBACK_ROUTE, HOME_ROUTE, INBOX_ROUTE, NO_SUBSCRIPTION_TENANT, SUBSCRIPTION_ROUTE, USAGE_ROUTE, NAV_ATTEMPTS, NAV_MALFORMED_STATUS, NAV_RETRY_DELAYS_MS, NAV_STAYS_EXPANDED_ROUTES, NAV_TIMEOUT_STATUS, agentAddressHash, appLink, canonicalRoute, databaseEntries, defaultRoute, deleteTable, fetchNav, fetchNavPrefs, fetchSchema, fetchTableFootprint, postOpened, dataAddressHash, readAgentAddress, readDataAddress, reportNavTimeout, resolveRoute, rewrittenHash, routeKeyOf, saveNavMeta, shapeNav, splitChrome } from "./nav"; import type { NavEntry, NavMetaPatch, NavPage, NavPrefs, RailSlot, Recent, SchemaField, SchemaPayload } from "./nav"; // ⭐ 2026-08-14 — the database frame, extracted so the Query module can wear the SAME one. @@ -124,7 +125,6 @@ const AgentsGalleryPage = lazy(() => const BACKGROUND_SYNC_SYSTEMS = new Set(["odoo_sync"]); const TemplatePicker = lazy(() => import("../home/TemplatePicker")); const ConnectorsPage = lazy(() => import("../connectors/ConnectorsPage")); -const ExternalAccessPane = lazy(() => import("../settings/ExternalAccessPane")); const FinanceWorkspace = lazy(() => import("../app/FinanceWorkspace")); // W63-T06 — AI API is chrome, like Connectors and Sheet. It deliberately stays local to the // shell because this lane cannot widen nav.ts's file fence; unknown hash routes already pass @@ -1147,7 +1147,13 @@ function useHashRoute(): { route: string; hash: string } { if (!window.location.hash && window.location.pathname.startsWith("/finance/")) { window.history.replaceState(window.history.state, "", `/#${window.location.pathname}${window.location.search}`); } - const hash = rewrittenHash(window.location.hash) ?? window.location.hash; + let hash = rewrittenHash(window.location.hash) ?? window.location.hash; + // Retired bookmarks and Slack callbacks keep their query and open Contact's existing door. + if (canonicalRoute(routeKeyOf(hash)) === AI_API_ROUTE) { + const params = new URLSearchParams(hash.split("?", 2)[1] ?? ""); + params.set("section", "contact"); + hash = `#/${CONNECTORS_ROUTE}?${params.toString()}`; + } if (hash !== window.location.hash) window.history.replaceState(window.history.state, "", hash); return { route: canonicalRoute(routeKeyOf(hash)), hash }; }; @@ -1459,17 +1465,6 @@ function PlugIcon() { ); } -/** W63-T06 — braces around two endpoint dots: an API, distinct from Connectors' physical plug. */ -function ApiIcon() { - return ( - - ); -} - /** ⭐⭐ WAVE 49 · T01 (R1/R2/R30) — SHEET. A page ruled into cells: one outlined sheet with two * rules across and two down, which is the shortest mark that reads as a spreadsheet rather than * as a table or a window at 16px. Same 16x16 / `currentColor` / 1.35 vocabulary as every other @@ -1617,6 +1612,8 @@ function AccountMenu({ className="shell-menu-item" role="menuitem" onClick={() => { + // Settings outlives this menu item; its opener must stay mounted. + wrap.current?.querySelector(".shell-account")?.focus(); setOpen(false); onSettings("account"); }} @@ -1787,6 +1784,7 @@ function readNavFold(): NavFold { } function ShellFrame() { + const freezeNotice = useFreezeNotice(); const [session, setSession] = useState({ phase: "checking" }); const [nav, setNav] = useState