diff --git a/RELEASES.json b/RELEASES.json index 411b1ae95c2ebd3f79454585a32d8ddd81b46acc..a958f057875fcef4a89bd1207cb8bf3131f2a00a 100644 --- a/RELEASES.json +++ b/RELEASES.json @@ -1,443 +1,4 @@ { - "current": "0576185", - "releases": [ - { - "version": "v74", - "sha": "40dceba", - "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": "bb94de5", - "date": "2026-09-13", - "subject": "release v73" - }, - { - "version": "v71", - "sha": "96143dc", - "date": "2026-09-04", - "subject": "release v71 - wave 51, QA'd 24 PASS / 6 FAIL / 5 UNPROVEN" - }, - { - "version": "v70", - "sha": "eedfb6a", - "date": "2026-09-03", - "subject": "release v70 - wave 50 (grid analytics, the Sheet as a spreadsheet, view trash and audit)" - }, - { - "version": "v69", - "sha": "8598f56", - "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": "62b065a", - "date": "2026-09-01", - "subject": "release v68: unified holiday date offsets" - }, - { - "version": "v67", - "sha": "3b22419", - "date": "2026-09-01", - "subject": "release v67" - }, - { - "version": "v66", - "sha": "73c94f0", - "date": "2026-08-31", - "subject": "release v66" - }, - { - "version": "v65", - "sha": "9f6cfed", - "date": "2026-08-31", - "subject": "release v65" - }, - { - "version": "v64", - "sha": "bb8985c", - "date": "2026-08-31", - "subject": "release v64" - }, - { - "version": "v63", - "sha": "ec75cb8", - "date": "2026-08-31", - "subject": "release v63" - }, - { - "version": "v62", - "sha": "2c8f702", - "date": "2026-08-31", - "subject": "release v62" - }, - { - "version": "v61", - "sha": "bbc4c07", - "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": "dd1b062", - "date": "2026-08-30", - "subject": "v60 - wave 46" - }, - { - "version": "v59", - "sha": "3cb73ce", - "date": "2026-08-29", - "subject": "release v59" - }, - { - "version": "v58", - "sha": "c526ced", - "date": "2026-08-29", - "subject": "release v58" - }, - { - "version": "v57", - "sha": "4859c03", - "date": "2026-08-29", - "subject": "release v57" - }, - { - "version": "v56", - "sha": "2970b72", - "date": "2026-08-28", - "subject": "release v56" - }, - { - "version": "v55", - "sha": "b766b90", - "date": "2026-08-28", - "subject": "release v55" - }, - { - "version": "v54", - "sha": "6a8e99a", - "date": "2026-08-27", - "subject": "release v54" - }, - { - "version": "v53", - "sha": "89ed2cb", - "date": "2026-08-24", - "subject": "release v53" - }, - { - "version": "v52", - "sha": "8263493", - "date": "2026-08-23", - "subject": "release v52" - }, - { - "version": "v51", - "sha": "5740d20", - "date": "2026-08-23", - "subject": "release v51" - }, - { - "version": "v50", - "sha": "8027c83", - "date": "2026-08-23", - "subject": "release v50" - }, - { - "version": "v49", - "sha": "9a14c01", - "date": "2026-08-23", - "subject": "release v49" - }, - { - "version": "v48", - "sha": "3c69fd9", - "date": "2026-08-23", - "subject": "release v48" - }, - { - "version": "v47", - "sha": "917f9a0", - "date": "2026-08-23", - "subject": "release v47" - }, - { - "version": "v46", - "sha": "a8dc531", - "date": "2026-08-23", - "subject": "release v46" - }, - { - "version": "v45", - "sha": "0b2b501", - "date": "2026-08-23", - "subject": "release v45" - }, - { - "version": "v44", - "sha": "a7877aa", - "date": "2026-08-23", - "subject": "Hotfix: allow renaming the active Map view" - }, - { - "version": "v43", - "sha": "e2ba461", - "date": "2026-08-23", - "subject": "release v43" - }, - { - "version": "v42", - "sha": "25b885e", - "date": "2026-08-23", - "subject": "release v42" - }, - { - "version": "v41", - "sha": "1d6adcc", - "date": "2026-08-22", - "subject": "v41 vertical route navigation" - }, - { - "version": "v40", - "sha": "1451837", - "date": "2026-08-22", - "subject": "release v40 route UI correction" - }, - { - "version": "v39", - "sha": "a78bdf0", - "date": "2026-08-21", - "subject": "W39 clean stale migration targets" - }, - { - "version": "v38", - "sha": "a556631", - "date": "2026-08-21", - "subject": "W39 reversible Live store cutover" - }, - { - "version": "v37", - "sha": "1ddd8cf", - "date": "2026-08-21", - "subject": "W39: coalesce derived workspace reads" - }, - { - "version": "v36", - "sha": "51db3b7", - "date": "2026-08-21", - "subject": "W39 share-field oid repair" - }, - { - "version": "v35", - "sha": "0f64212", - "date": "2026-08-21", - "subject": "W39 visual QA fixture" - }, - { - "version": "v34", - "sha": "cb4caa1", - "date": "2026-08-21", - "subject": "W39 Staging PostgreSQL egress controls" - }, - { - "version": "v33", - "sha": "2b115e5", - "date": "2026-08-21", - "subject": "W39: stop nonproduction Postgres background egress" - }, - { - "version": "v32", - "sha": "b47b27c", - "date": "2026-08-21", - "subject": "W39: repair Staging Postgres projection" - }, - { - "version": "v31", - "sha": "8880a49", - "date": "2026-08-21", - "subject": "v31: Wave 39 staging candidate" - }, - { - "version": "v30", - "sha": "71776ca", - "date": "2026-08-20", - "subject": "v30: W39-T29 Member-owned View first-share hotfix" - }, - { - "version": "v29", - "sha": "55ced50", - "date": "2026-08-18", - "subject": "v29: the build staging has been running since 2026-08-18 (55ced50)." - }, - { - "version": "v28", - "sha": "2793e4b", - "date": "2026-08-18", - "subject": "release v28" - }, - { - "version": "v27", - "sha": "c05a794", - "date": "2026-08-18", - "subject": "release v27" - }, - { - "version": "v25", - "sha": "bc24829", - "date": "2026-08-14", - "subject": "v25 - wave 32's LIVE build (deployed 2026-08-13, bc24829)" - }, - { - "version": "v26", - "sha": "6a642e1", - "date": "2026-08-14", - "subject": "v26 - wave 33's build (deployed 2026-08-14, 6a642e1)" - }, - { - "version": "v24", - "sha": "514d829", - "date": "2026-08-09", - "subject": "release v24" - }, - { - "version": "v23", - "sha": "44cc283", - "date": "2026-08-09", - "subject": "release v23" - }, - { - "version": "v22", - "sha": "f14e199", - "date": "2026-08-09", - "subject": "release v22" - }, - { - "version": "v21", - "sha": "5fdffce", - "date": "2026-08-09", - "subject": "release v21" - }, - { - "version": "v20", - "sha": "442e4a1", - "date": "2026-08-09", - "subject": "release v20" - }, - { - "version": "v19", - "sha": "94cb4a4", - "date": "2026-08-09", - "subject": "release v19" - }, - { - "version": "v18", - "sha": "0106ef9", - "date": "2026-08-09", - "subject": "release v18" - }, - { - "version": "v17", - "sha": "308240c", - "date": "2026-08-09", - "subject": "release v17" - }, - { - "version": "v16", - "sha": "a804df5", - "date": "2026-08-09", - "subject": "release v16" - }, - { - "version": "v15", - "sha": "6b84d72", - "date": "2026-08-09", - "subject": "release v15" - }, - { - "version": "v14", - "sha": "bee5eaf", - "date": "2026-08-07", - "subject": "release v14" - }, - { - "version": "v13", - "sha": "314c0e3", - "date": "2026-08-06", - "subject": "release v13" - }, - { - "version": "v12", - "sha": "4908f39", - "date": "2026-08-06", - "subject": "release v12" - }, - { - "version": "v11", - "sha": "3126617", - "date": "2026-08-05", - "subject": "release v11" - }, - { - "version": "v10", - "sha": "43f8042", - "date": "2026-08-05", - "subject": "release v10" - }, - { - "version": "v9", - "sha": "2e26f57", - "date": "2026-08-05", - "subject": "release v9" - }, - { - "version": "v8", - "sha": "8073b3e", - "date": "2026-08-05", - "subject": "release v8" - }, - { - "version": "v7", - "sha": "b1c5a7e", - "date": "2026-08-05", - "subject": "release v7" - }, - { - "version": "v6", - "sha": "a344020", - "date": "2026-08-05", - "subject": "release v6" - }, - { - "version": "v5", - "sha": "c0b0fbb", - "date": "2026-08-05", - "subject": "release v5" - }, - { - "version": "v4", - "sha": "de5037f", - "date": "2026-08-05", - "subject": "release v4" - }, - { - "version": "v3", - "sha": "6b9fa62", - "date": "2026-08-05", - "subject": "release v3" - }, - { - "version": "v2", - "sha": "fd05861", - "date": "2026-08-04", - "subject": "release v2" - }, - { - "version": "v1", - "sha": "5b4e2c4", - "date": "2026-08-04", - "subject": "release v1" - } - ] + "current": "83594e0", + "releases": [] } \ No newline at end of file diff --git a/VERSION b/VERSION index fb5b19868a39f46d31ad70c163e191a28450e536..4bd82a83d69ee854ae7a0d2b651aabeed5184d33 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0576185 +83594e0 diff --git a/api/routes_automation.py b/api/routes_automation.py index d3b8d4eef9c804fcb78a7922a20bd6d8b80d2343..37a2fe43754480705da8bcaa46e7ff4bf37428b7 100644 --- a/api/routes_automation.py +++ b/api/routes_automation.py @@ -126,10 +126,27 @@ def _voice_options(session): tables.append(option) except Exception: # noqa: BLE001 - permission/absence. pass - # User tables are permissioned by their own object wall inside automation_tables(). - for table in automation_tables(session).get("tables") or []: - option = _voice_option(table.get("key"), table.get("label") or table.get("key"), - table.get("fields"), table.get("views")) + # User tables are permissioned by their own object wall inside _automation_table_rows(). + # + # ⭐⭐ W62-T17 — THE VIEW LIST IS BOUGHT PER DATABASE AND `_voice_option` THROWS MOST OF IT + # AWAY. That helper returns None unless a database has BOTH a phone-type field AND a saved + # View, so a database with no phone field is dropped whatever its Views say — and resolving + # them cost a `shares.grants` read plus a whole workspace-document read EACH. MEASURED on + # staging: `_table_views` x24 = 6,810 ms of this pass + # (`AutoWiki/waves/wave62/proto/T17-profile.md`). The phone test is free (the field list is + # already in hand), so it is asked FIRST and the view read is spent only where it can matter. + # + # ⚠ THE ANSWER IS IDENTICAL, not merely similar: the skipped databases are exactly the ones + # `_voice_option` returned None for, and `_table_views` is still the single source of the + # view list for every database that survives — ABSENT (None) still reads as "nobody looked" + # and still drops the option, exactly as it did when the list rode on the row. + for table in _automation_table_rows(session, with_views=False): + if not any(isinstance(field, dict) and field.get("key") and field.get("type") == "phone" + for field in (table.get("fields") or [])): + continue + key = table.get("key") + option = _voice_option(key, table.get("label") or key, + table.get("fields"), _table_views(session, key)) if option: tables.append(option) return {"tables": tables} @@ -210,8 +227,23 @@ def _voice_view_input(session, audience): @router.get("/voice-agents") def list_voice_agents(session: Session = Depends(_GATE)): - options = _voice_options(session) + """Every voice agent, each carrying the audience choices its editor offers. + + ⭐⭐ W62-T17 (R12, D-739) — **THE OPTIONS ARE RESOLVED ONLY IF A ROW WILL CARRY THEM**, and on + the store that produced this ticket that is the whole fix. `_voice_options` is the most + expensive read pass in this file (MEASURED at 14,705 ms of this route's 14,748 ms on staging's + Royal Imports, `AutoWiki/waves/wave62/proto/T17-profile.md`), and Royal Imports has NO voice + agents, so every one of those seconds was assembled and then thrown away with the empty list. + The payload is unchanged by construction: with no rows the loop below never ran, so the + options never reached a caller. + + ⛔ THE ORDER IS THE FIX, not a tidy-up — read the rows first, then decide. Reinstating the + eager call "because it is simpler" restores a 14 second answer to an 18 byte payload. This is + the same shape this file already carries twice (W29-T01 and W30-T12, both on `list_automations` + below): work whose result is discarded is still paid for. + """ rows = voice.list_browser_dtos(session.runtime) + options = _voice_options(session) if rows else None for row in rows: row["audienceOptions"] = options return {"voiceAgents": rows} @@ -1081,6 +1113,49 @@ def _discover_default(session, tables): lambda: engine.discover_default_table(session.runtime)) +def automation_ids(session): + """Every automation id `GET /automations` would list, WITHOUT wiring a single DTO. + + ⭐⭐ W62-T17 — THE ID-ONLY ACCESSOR `routes_module_index._readable_ids` ASKED FOR IN WRITING: + *"all_definitions omits synthetic field/Odoo agents. Keep the complete automation list until it + has an id-only accessor."* That route needs the id SET and nothing else — it uses it to drop a + folder placement whose item the caller cannot read — and it was building the whole Agents rail + payload to get it. MEASURED on staging: `GET /api/v1/module-index/agents` spent 1,101 ms of its + 1,180 ms inside `list_automations`, whose `_wire` reads `agent_contracts` and + `automation_agent_actions` once PER AUTOMATION (`AutoWiki/waves/wave62/proto/T17-profile.md`). + + ⛔⛔ THE SET MUST BE `list_automations`' SET EXACTLY, BECAUSE IT IS A WALL. An id this omits is + a tile that silently stops rendering — the worst failure shape on a folder home, because the + placement is still stored and the page simply looks emptier. So all three strata stay: the + stored definitions, the synthetic field agents (C3), and the derived Odoo sync row (C4). Add a + stratum to `list_automations` and it belongs here in the same commit. + + ⛔ AND THE STATEMENTS SEED STAYS, for a reason that is not obvious: `seed_statements_agent` + MINTS a row that `all_definitions` then lists (W35-T37 / R10), once per tenant per process. If + only `list_automations` seeded it, this set would depend on WHICH ROUTE the process happened to + serve first — the Agents home would hide the statements agent's placement until somebody + opened the rail. Same guard, same idempotency key, same never-take-the-rail-down posture. + + ⚠ WHAT IT DELIBERATELY DOES NOT DO is the board-retirement scan. That is hygiene over stored + state (`all_definitions` strips retired board state on every read anyway), it changes no id, + and `list_automations` still runs it on its own first call per tenant. + """ + if session.tenant not in _STATEMENTS_SEEDED: + try: + engine.seed_statements_agent(session.runtime, username=session.uname or "system") + except Exception: # noqa: BLE001 + pass + _STATEMENTS_SEEDED.add(session.tenant) + ids = {str(defn.get("id")) for defn in engine.all_definitions(session.runtime).values() + if isinstance(defn, dict) and defn.get("id")} + ids |= {str(row.get("id")) for row in _field_agent_rows(session) + if isinstance(row, dict) and row.get("id")} + odoo = _odoo_sync_row(session) + if isinstance(odoo, dict) and odoo.get("id"): + ids.add(str(odoo["id"])) + return ids + + def warm_default(rt): """⭐⭐ WAVE 30 · T12, THE COLD HALF. Elect the discovery picker's default at BOOT. @@ -1396,7 +1471,22 @@ def _table_views(session, key): @router.get("/automations/tables") def automation_tables(session: Session = Depends(_GATE)): - """The blank databases an automation can target, with their fields. + """The blank databases an automation can target, with their fields. See the helper below.""" + return {"tables": _automation_table_rows(session)} + + +def _automation_table_rows(session, with_views=True): + """The blank databases an automation can target, with their fields — the rows behind + `GET /automations/tables`, as a plain list. + + ⭐⭐ W62-T17 — SPLIT OUT OF THE ROUTE SO ONE IN-PROCESS CALLER CAN DECLINE THE EXPENSIVE HALF. + The route's payload is unchanged (`with_views=True` is the default and the route passes + nothing else); `_voice_options` above is the caller that asks for `with_views=False`, because + it filters on phone fields first and most databases never reach the view list at all. See that + call site for the measurement. + + ⛔ A PLAIN FUNCTION, NOT A SECOND ROUTE. The wall, the C8 migration and the lent document all + stay exactly where they were; only the `views` read became optional. ⚠ A READ-ONLY MIRROR of C3-UT's `GET /api/v1/tables`, under this router's own prefix so the two can never collide. It exists because the automation editor needs the table+field list to @@ -1442,11 +1532,12 @@ def automation_tables(session: Session = Depends(_GATE)): "type": f.get("type") or "text", "automation": f.get("automation") or None} for f in (t.get("fields") or [])]} - views = _table_views(session, key) - if views is not None: - row["views"] = views # absent when the bucket did not answer — never [] + if with_views: + views = _table_views(session, key) + if views is not None: + row["views"] = views # absent when the bucket did not answer — never [] out.append(row) - return {"tables": out} + return out @router.get("/automations/presets") diff --git a/api/routes_external.py b/api/routes_external.py index e4174af315d8b8ba193cb7cbe05b2b6ac02820f2..7eb5fe13b6c922f46fe9de3cb2b230f6a69048df 100644 --- a/api/routes_external.py +++ b/api/routes_external.py @@ -1,5 +1,6 @@ """Per-user credentials, governed reads and explicitly allowed bearer build tools.""" +import copy import json import uuid from urllib.parse import quote @@ -17,6 +18,53 @@ router = APIRouter(prefix="/api/v1/external", tags=["external"]) _CREDENTIAL_PREFIX = "lpb1." +def _key_scope(session): + """(access, databases) for THIS request. A session without the claims is an in-app human.""" + claims = session.claims or {} + return external_access.scope_of({ + "access": claims.get("external_access"), + "databases": claims.get("external_databases"), + }) + + +def _assert_database_scope(session, database): + if external_access.database_allowed(_key_scope(session)[1], database): + return + # A binding can carry no database at all. Name what was asked for when there is a name, and + # never interpolate a bare None into a sentence a caller reads. + named = str(database or "").strip() + raise err(403, "key_database_out_of_scope", + f"Database {named} is outside this credential's scope." if named + else "This credential is limited to named databases, and none was named.") + + +def _assert_write_scope(session, tool_name): + if not external_access.may_write(_key_scope(session)[0]): + raise err(403, "key_read_only", f"{tool_name} writes, and this credential is read only.") + + +def _databases_in_arguments(value): + """Find database references, including bound cell documents, without unbounded recursion.""" + found = [] + + def scan(node, depth): + if depth > 12: + return + if isinstance(node, dict): + for key, item in node.items(): + if key in ("database", "sourceDatabase") and isinstance(item, str): + found.append(item) + elif key == "compatibleDatabases" and isinstance(item, list): + found.extend(database for database in item if isinstance(database, str)) + scan(item, depth + 1) + elif isinstance(node, (list, tuple)): + for item in node: + scan(item, depth + 1) + + scan(value, 0) + return found + + def _auth_refusal(): raise err(401, external_access.AUTH_ERROR, "The credential is invalid or revoked.") @@ -42,6 +90,10 @@ def require_external_session( "u": verified.user["username"], "external": True, "external_key_id": verified.key_id, + "external_access": verified.access, + "external_databases": (external_access.ALL_DATABASES + if verified.databases == external_access.ALL_DATABASES + else list(verified.databases)), }, runtime=verified.runtime, ) @@ -66,13 +118,17 @@ def _can_use_analyst(session: Session) -> bool: def _management_refusal(exc: external_access.ExternalAccessError): + if exc.code == "external_access_scope_invalid": + raise err(400, exc.code, "Choose Read only or Read and write.") + if exc.code == "external_databases_invalid": + raise err(400, exc.code, "Choose all databases, or name at least one database.") if exc.code == "external_label_invalid": raise err(400, exc.code, "Enter a label between 1 and 80 characters.") if exc.code == "external_key_not_found": raise err(404, exc.code, "That credential was not found.") if exc.code in {"external_identity_invalid", "external_account_unavailable"}: raise err(409, exc.code, "This account cannot create an external credential.") - raise err(503, "external_access_unavailable", "External access is unavailable right now.") + raise err(503, "external_access_unavailable", "The AI API is unavailable right now.") def _resolve_external_scope( @@ -130,6 +186,8 @@ def _resolve_external_scope( else: row["label"] = fallback_label(database) row["fields"] = [] + else: + row["label"] = fallback_label(database) resolved.append(row) return resolved @@ -326,7 +384,9 @@ def _portable_skill_document(session: Session, base_url: str) -> str: "## Limits", "", ("This surface writes only through the named tools, as the key's maker with the maker's " - "own grants. Workbook roles and database restrictions still apply. It returns no data " + "own grants. Workbook roles and database restrictions still apply. Each credential also " + "carries its own access level and database scope, set when it is created and shown in " + "Loopable Settings. It returns no data " "the maker cannot already see in Loopable. Sheet cell history records the maker and " "key label. Saved Views and Apps keep the UI's username attribution; their writers " "have no author display-name field. No tool writes to Odoo."), @@ -343,8 +403,11 @@ def create_key(body=Body(default=None), session: Session = Depends(require_sessi """Mint a credential for the signed-in human. Bearer auth is not accepted here.""" session.require("analyst") label = body.get("label") if isinstance(body, dict) else None + access = body.get("access") if isinstance(body, dict) else None + databases = body.get("databases") if isinstance(body, dict) else None try: - key, token = external_access.mint(session.runtime, session.uname, session.tenant, label) + key, token = external_access.mint(session.runtime, session.uname, session.tenant, label, + access=access, databases=databases) except external_access.ExternalAccessError as exc: _management_refusal(exc) return {"key": key, "secret": token} @@ -385,20 +448,38 @@ def portable_skill(request: Request, session: Session = Depends(require_session) @router.get("/whoami") def whoami(session: Session = Depends(require_external_analyst)): """Name the authenticated maker and the explicit limits on bearer writes.""" + access, databases = _key_scope(session) return { "username": session.uname, "name": session.user.get("name") or session.uname, "tenant": session.tenant, "key_id": session.claims["external_key_id"], + "access": access, + "databases": (external_access.ALL_DATABASES + if databases == external_access.ALL_DATABASES else list(databases)), "write_scope": "Named tools only, as the key's maker with the maker's own grants.", } +@router.get("/scope/databases") +def scope_databases(session: Session = Depends(require_session)): + """List the databases this signed-in maker could put inside a new credential's scope.""" + session.require("analyst") + return {"databases": [ + {key: row[key] for key in ("database", "label", "permitted", "answerable")} + for row in _resolve_external_scope(session) + if row.get("permitted") or row.get("visible") + ]} + + @router.get("/databases") def list_databases(session: Session = Depends(require_external_analyst)): """List only sources this user may read or already sees elsewhere in the product.""" databases = [] + key_databases = _key_scope(session)[1] for row in _resolve_external_scope(session): + if not external_access.database_allowed(key_databases, row["database"]): + continue if not (row.get("permitted") or row.get("visible")): continue public = { @@ -416,6 +497,7 @@ def list_databases(session: Session = Depends(require_external_analyst)): @router.get("/databases/{database}/schema") def database_schema(database: str, session: Session = Depends(require_external_analyst)): """Return the field contract after resolving the ordinary strict Assistant read wall.""" + _assert_database_scope(session, database) resolved = _resolve_external_scope( session, databases=[database], include_schema=True, strict_schema=True, ) @@ -429,6 +511,7 @@ def query_database( session: Session = Depends(require_external_analyst), ): """Read one detached Assistant snapshot; FastAPI rejects non-object request bodies.""" + _assert_database_scope(session, database) payload = body or {} return deps.assistant_read_scope( session, @@ -504,6 +587,7 @@ def _view_event(session, kind, args): from core import grid_events database = args["database"] + _assert_database_scope(session, database) event = {"id": uuid.uuid4().hex, "type": kind} if kind == "view_upsert": raw = args["view"] @@ -556,6 +640,10 @@ def _application_upsert(session, args): import application_registry as applications import routes_assets + for database in args.get("compatibleDatabases") or []: + _assert_database_scope(session, database) + if args.get("sourceDatabase") is not None: + _assert_database_scope(session, args["sourceDatabase"]) payload = {key: value for key, value in args.items() if key != "app_id"} app_id = args.get("app_id") if "app_id" in args and not str(app_id or "").strip(): @@ -595,9 +683,27 @@ def _tool_context(session: Session) -> external_tools.ToolContext: def project(book): served = routes_sheet._read_for(session, book) + databases = _key_scope(session)[1] + if databases != external_access.ALL_DATABASES: + # The owner projection can share cells with its input; key scope is response-only. + served = copy.deepcopy(served) + for tab in served.get("sheets", []): + for cell in tab.get("cells", {}).values(): + if not isinstance(cell, dict) or cell.get("kind") != "bound": + continue + binding = cell.get("binding") + database = binding.get("database") if isinstance(binding, dict) else None + if not external_access.database_allowed(databases, database): + cell["value"] = None + cell["display"] = "" routes_sheet._recompute_formulas(served, formula_runtime, sheet_mod) return served + def resolve_binding(binding, book, tab): + if isinstance(binding, dict): + _assert_database_scope(session, binding.get("database")) + return routes_sheet._resolve_cell_binding(session, binding, book, tab) + def cache_binding(workbook_id, tab, addr, binding, resolved): # Same best-effort cache fill as the UI's compute_cell. A fresh value must replace # this scope's old slot before an editor's read projection can use that slot. @@ -618,7 +724,7 @@ def _tool_context(session: Session) -> external_tools.ToolContext: require_reader=lambda book: routes_sheet._require_reader(session, book), require_editor=lambda book: routes_sheet._require_editor(session, book), project_book=project, - resolve_binding=lambda binding, book, tab: routes_sheet._resolve_cell_binding(session, binding, book, tab), + resolve_binding=resolve_binding, cache_binding=cache_binding, list_databases=lambda: list_databases(session), describe_database=lambda database: database_schema(database, session), @@ -687,6 +793,10 @@ def _handle_mcp(req, session: Session): for key, tool in external_tools.TOOLS.items()]}} if method != "tools/call": return protocol_error(rid, -32601, "The requested MCP method was not found.") + if isinstance(name, str) and name in _WRITE_TOOLS: + _assert_write_scope(session, name) + for database in _databases_in_arguments(params.get("arguments", {})): + _assert_database_scope(session, database) data = external_tools.dispatch(_tool_context(session), name, params.get("arguments", {})) if isinstance(data, dict) and data.get("refused"): first = data["refused"][0] @@ -697,7 +807,7 @@ def _handle_mcp(req, session: Session): except external_access.ExternalAuthError: _auth_refusal() except external_access.ExternalAccessError: - out = _tool_refusal(name, "external_access_unavailable", "External access is unavailable right now.", 503) + out = _tool_refusal(name, "external_access_unavailable", "The AI API is unavailable right now.", 503) except sheets.SheetRefusal as exc: import routes_sheet translated = routes_sheet._refuse(exc) diff --git a/api/routes_module_index.py b/api/routes_module_index.py index fe58ffbf8416b32aaf61b4a8432c6ce4e0bf3e79..fe9ea335582f43f40d172eba42c92c634275fd64 100644 --- a/api/routes_module_index.py +++ b/api/routes_module_index.py @@ -52,10 +52,15 @@ def _readable_ids(module, session): # records, never audience-option assembly, just to verify placement ids. if module == "app": return {row["id"] for row in routes_assets.application_index(session=session)["applications"]} - # all_definitions omits synthetic field/Odoo agents. Keep the complete automation - # list until it has an id-only accessor; callers skip this work for empty placements. - return ({"agent:" + row["id"] for row in - routes_automation.list_automations(session=session)["automations"]} + # ⭐⭐ W62-T17 — THE ID-ONLY ACCESSOR THIS COMMENT USED TO ASK FOR NOW EXISTS. It read + # *"all_definitions omits synthetic field/Odoo agents. Keep the complete automation list until + # it has an id-only accessor"*, and the whole Agents rail payload was being built to answer a + # membership question. `routes_automation.automation_ids` keeps all three strata (stored + # definitions, field agents, the derived Odoo sync row) and the statements seed, so the SET is + # unchanged; what it drops is `_wire` per automation, which read `agent_contracts` and + # `automation_agent_actions` once EACH TIME (MEASURED: 1,101 ms of this route's 1,180 ms on + # staging, `AutoWiki/waves/wave62/proto/T17-profile.md`). + return ({"agent:" + aid for aid in routes_automation.automation_ids(session)} | {"voice:" + row["id"] for row in voice._store(session.runtime)["voice_agent"].values() if isinstance(row, dict)}) diff --git a/api/routes_odoo_tables.py b/api/routes_odoo_tables.py index c5fd5694b9a8dc2b70970d33ab5a02a55423a6ab..92dc799bf74a3966c53b880b3da1084a5ef2ce9e 100644 --- a/api/routes_odoo_tables.py +++ b/api/routes_odoo_tables.py @@ -1522,18 +1522,37 @@ def _materialised_table(table_key): return module_cache_source(table_key) is not None or table_key not in _sources() -def _relation_rows(session, table, columns, *, ids=None, match=None, where=None): +def _traversal_defn_or_refuse(session, table_key): + """R50 / C4: admit an intermediate after the caller has established both end grants. + + The session may already open both ends of a declared two-hop link. Its intermediate + is read only to find target ids, and only join values leave this traversal. The compiled + read still applies the row wall, hidden-field closure, BU scope and mirror guard. + """ + ut = _ut() + st = ut.lend_defs(session.runtime) + defn = ut.get(table_key, st=st) + if not defn: + raise err(404, "unknown_table", "that database does not exist") + return defn + + +def _relation_rows(session, table, columns, *, ids=None, match=None, where=None, + traversal_of=None): """Internal join values, after the ordinary row wall, with no row-count ceiling. Join columns may be hidden on the wire. Only identities leave the resolver; the native payload builders still apply their complete hidden-field closure before returning cells. + With traversal_of, the caller must already have established both ends of the declared + edge named by that key. Only join values leave this intermediate read, never a cell. """ import core.perm_scope as perm_scope import core.relations as relations columns = set(columns) | {relations.IDENTITY_COL} if not _materialised_table(table): - read = _compiled_read(table, session) + read = (_compiled_read(table, session, traversal_intermediate=True) + if traversal_of is not None else _compiled_read(table, session)) spec = read["spec"] bindings = dict(spec["cols"], pid=(spec["id"], int)) if columns - set(bindings): @@ -1556,7 +1575,7 @@ def _relation_rows(session, table, columns, *, ids=None, match=None, where=None) # This is an internal join, never a cell response. Apply the same permanent wall on the # complete contract before taking any join value, including a join on a hidden column. - if not perm_scope.may_read(session.user, table, st=session.runtime): + if traversal_of is None and not perm_scope.may_read(session.user, table, st=session.runtime): raise err(403, "forbidden_table", "This account cannot open the linked database.") if module_cache_source(table) is not None: session.require(table) @@ -1582,6 +1601,8 @@ def _relation_cell_ids(value): def _relation_leg_ids(session, source, source_ids, entry): """Follow one declared leg, including manual, inverse and through links.""" + import core.relations as relations + target = str(entry["target"]) via = entry.get("via") or {} link = entry.get("link") or {} @@ -1601,11 +1622,12 @@ def _relation_leg_ids(session, source, source_ids, entry): if (manual and not reverse) or (inverse and reverse): values = set().union(*(_relation_cell_ids(row.get(source_col)) for row in source_rows)) target_col = "pid" - through = link.get("through") if not manual else None + through = relations.declared_through(link) if not manual else None if through: left = str(link["throughTo"] if reverse else link["throughOn"]) right = str(link["throughOn"] if reverse else link["throughTo"]) - middle = _relation_rows(session, str(through), [left, right], match=(left, values)) + middle = _relation_rows(session, str(through), [left, right], match=(left, values), + traversal_of=str(entry.get("key") or via.get("key") or "")) values = {str(row[right]) for row in middle if row.get(right) not in (None, "")} if (manual and reverse) or (inverse and not reverse): rows = _relation_rows(session, target, [target_col]) @@ -1616,6 +1638,15 @@ def _relation_leg_ids(session, source, source_ids, entry): return {int(row["pid"]) for row in rows} +def _relation_label(key): + """Prefer the database's display label in relation refusals.""" + source = module_cache_source(key) + if source and source.get("label"): + return source["label"] + return next((label for _bucket, table, label, _fields in _rel().TABLES + if table == key and label), str(key)) + + def _relation_target_ids(session, target, related): """One pin resolver for SQL, pool and tenant rows, selected from the scoped path list.""" import core.relations as relations @@ -1631,7 +1662,8 @@ def _relation_target_ids(session, target, related): if path and len(path) > relations.MAX_DEPTH: raise err(400, "over_depth", relations.over_depth_refusal(source, path)) if not _may_open(session, source): - raise err(403, "forbidden_table", "This account cannot open the source database.") + raise err(403, "forbidden_table", + f"the relation starts at '{_relation_label(source)}', which your account cannot open") entries = related_path_entries(session, source, len(path) if path else 1) entry = relations.pick_path(entries, path) if path else relations.pick(entries, via) if entry is None or str(entry.get("target")) != str(target): @@ -1646,7 +1678,8 @@ def _relation_target_ids(session, target, related): legs = [(source, first), (middle, last)] for table, leg in legs: if not _may_open(session, leg["target"]): - raise err(403, "forbidden_table", "This account cannot open the linked database.") + raise err(403, "forbidden_table", + f"the relation ends at '{_relation_label(leg['target'])}', which your account cannot open") ids = _relation_leg_ids(session, table, ids, leg) return ids @@ -1700,7 +1733,7 @@ def _cache_relation_plan(session, source, via, target): if not isinstance(link, dict) or str(link.get("table") or "") != target: raise err(404, "relation_target_missing", f"the relation '{via}' no longer has a current target in this workspace") - through = str(link.get("through") or "") + through = relations.declared_through(link) through_on = str(link.get("throughOn") or "") through_to = str(link.get("throughTo") or "") on = str(link.get("on") or relations.IDENTITY_COL) @@ -1710,10 +1743,8 @@ def _cache_relation_plan(session, source, via, target): if relations.is_retired_table(through): raise err(404, "relation_target_retired", f"'{through}' is retired, so the relation to '{target}' is no longer available") - if not _may_open(session, through): - raise err(403, "forbidden_table", - f"the relation to '{target}' crosses '{through}', which your account cannot " - "open") + # R50: both end grants suffice. Traversal admission in _compiled_read replaces the + # intermediate grant check and still applies the intermediate's row wall. if not _may_open(session, target): raise err(403, "forbidden_table", f"the relation ends at '{target}', which your account cannot open") @@ -1755,7 +1786,10 @@ def _cache_relation_target_ids(session, source, record, via, target): raise err(400, "unknown_relation", f"record {record} has no readable source value for relation '{via}'") - compiled = _compiled_read(through, session) + # R50 / C4: the intermediate of a DECLARED two-hop link is read to find target ids + # only, so it is admitted on both ends' grants. The compiler still applies its row + # wall, hidden-field closure and mirror guard to it. + compiled = _compiled_read(through, session, traversal_intermediate=True) spec = compiled["spec"] left = (spec.get("cols") or {}).get(through_on) right = (spec.get("cols") or {}).get(through_to) @@ -2163,7 +2197,7 @@ def _may_see_view(session, vid, store): def _compiled_read(table_key, session, *, filters=None, filterConj="and", sorts=None, - search=None, tree=_TREE_UNSET, related=None): + search=None, tree=_TREE_UNSET, related=None, traversal_intermediate=False): """EVERYTHING THAT DECIDES **WHICH ROWS** A CONNECTED GRID MAY ANSWER OVER, compiled once. Returns `{defn, fields, cur, spec, cols, base_where, wall_params, where, params, limits, @@ -2223,7 +2257,10 @@ def _compiled_read(table_key, session, *, filters=None, filterConj="and", sorts= # tenant #0's eight databases is read-through. Left un-applied, an administrator could set a # filter in Manage user, see it saved, and the scoped user would meet a 409 where their rows # used to be. So the wall is APPLIED here, in SQL, three paragraphs down. - defn = _defn_or_refuse(session, table_key, defs_only=True, scope_applied=True) + if traversal_intermediate: + defn = _traversal_defn_or_refuse(session, table_key) + else: + defn = _defn_or_refuse(session, table_key, defs_only=True, scope_applied=True) fields = None for _bucket, key, _label, mk_fields in rel.TABLES: if key == table_key: diff --git a/api/routes_query.py b/api/routes_query.py index 1a79e8bd97531be78b4f7efcce166a98fc906ea9..75dc5d74852681e27f9d9a58907514ea35331e28 100644 --- a/api/routes_query.py +++ b/api/routes_query.py @@ -1447,6 +1447,7 @@ def _submit(body, session, chat=None): assistant_message = { "id": assistant_message_id, "threadId": thread_id, "role": "assistant", "createdAt": now, "content": (said or _said_fallback(artifact)) if artifact else (refusal or "the assistant could not answer"), + # D-745: the Assistant restores routing from targetDatabase; keep it equal to routed["database"]. "targetDatabase": target, "requestedModel": selected_model, "model": provider, "routedTo": routed, **_model_identity(provider), diff --git a/api/routes_sheet.py b/api/routes_sheet.py index a473d2f03efce3834d1a267ba7a3450ffdbc82c5..4c4e94c32b1c41f146877d79d10e711796a59eac 100644 --- a/api/routes_sheet.py +++ b/api/routes_sheet.py @@ -221,6 +221,11 @@ def _registry_assembly(session: Session, table_key: str): if table_key in memo: return memo[table_key] 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] if table_key == "product_data": from routes_products import product_assembly memo[table_key] = product_assembly(session, scope="product", @@ -1042,11 +1047,7 @@ def _reresolved_for_reader(session: Session, book, *, endings_only=False): seen_db[db] = exc failure = seen_db.get(db) if failure is not None: - cell["state"] = "refused" - cell["value"] = None - cell["display"] = "" - cell["refusal"] = {"code": "database_unreadable", - "message": f"You cannot read {db}."} + cell.update(_binding_refusal("database_unreadable", f"You cannot read {db}.")) cell.pop("limits", None) continue @@ -1075,13 +1076,10 @@ def _reresolved_for_reader(session: Session, book, *, endings_only=False): except HTTPException as exc: detail = getattr(exc, "detail", None) or {} info = detail.get("error") if isinstance(detail, dict) else None - cell["state"] = "refused" - cell["value"] = None - cell["display"] = "" - cell["refusal"] = { - "code": (info or {}).get("code", "sheet_unresolved"), - "message": (info or {}).get("message", "This cell could not be refreshed."), - } + cell.update(_binding_refusal( + (info or {}).get("code", "sheet_unresolved"), + (info or {}).get("message", "This cell could not be refreshed."), + )) cell.pop("limits", None) continue cell["state"] = "fresh" @@ -1219,7 +1217,7 @@ def read_sheet_index(session: Session = Depends(require_session)): @router.put("/sheets/index") def write_sheet_index(body: dict = Body(default=None), session: Session = Depends(require_session)): - """Replace the folder, placement and star strata wholesale, or refuse by name. + """D-639 / W62-T25: replace the rail wholesale or merge a placements-only delta. ⭐ `repairs` RIDES BACK OUT. `core.sheets.save_folders` records every change it made to the payload, and the rail says so in a sentence rather than letting a workbook reappear at the @@ -1235,9 +1233,16 @@ def write_sheet_index(body: dict = Body(default=None), from core import sheets raw = dict(body or {}) try: - out = sheets.save_folders(_store(session), raw.get("folders"), - raw.get("itemFolders"), starred=raw.get("starred"), - expect_rev=raw.get("rev")) + if "placements" in raw: + if set(raw) != {"placements"}: + raise sheets.SheetRefusal( + "index_request_mixed", + "Send placements alone, or send the full Sheet rail with its revision.") + out = sheets.merge_placements(_store(session), raw["placements"]) + else: + out = sheets.save_folders(_store(session), raw.get("folders"), + raw.get("itemFolders"), starred=raw.get("starred"), + expect_rev=raw.get("rev")) except sheets.SheetRefusal as exc: raise _refuse(exc) return {"index": _wire_index({**_index_for(session), "repairs": out.get("repairs", [])})} @@ -1339,7 +1344,8 @@ def _run_import(job_id, path, filename, runtime, uname): made = sheets.create_workbook(runtime, name=book.get("name") or "", created_by=uname, on_duplicate="suffix") saved = sheets.put_workbook(runtime, made["id"], - dict(book, refreshed_at=stamp, rev=made["rev"]), actor=uname) + dict(book, refreshed_at=stamp, rev=made["rev"]), actor=uname, + lossless=True) _job_set(job_id, state="done", progress=1.0, workbook_id=saved["id"], sentence=_import_sentence(report)) except sheet_import.ImportRefusal as exc: @@ -1617,9 +1623,10 @@ def _recomputed_tab_cells(session: Session, workbook_id: str, tab_ref: str, *, c cell.pop("refusal", None) except HTTPException as exc: info = ((exc.detail or {}).get("error") or {}) - cell.update(value=None, display="", state="refused", refusal={ - "code": info.get("code", "ending_cell_unreadable"), - "message": info.get("message", "The Ending date could not be read. Choose a valid date.")}) + cell.update(_binding_refusal( + info.get("code", "ending_cell_unreadable"), + info.get("message", "The Ending date could not be read. Choose a valid date."), + )) ending_cells.add((source.get("id"), addr)) if ending_cells: _recompute_formulas(book, fr, sheet_mod) @@ -1839,6 +1846,8 @@ def refresh_sheet(workbook_id: str, session: Session = Depends(require_session)) for addr, cell in (tab.get("cells") or {}).items(): if not isinstance(cell, dict) or cell.get("kind") != "bound": continue + if _is_view_binding(cell): + continue # C2: a placed view refreshes through its own door binding = cell.get("binding") or {} db = str(binding.get("database") or "") if db not in seen_db: @@ -1849,11 +1858,7 @@ def refresh_sheet(workbook_id: str, session: Session = Depends(require_session)) seen_db[db] = exc failure = seen_db.get(db) if failure is not None: - cell["state"] = "refused" - cell["value"] = None - cell["display"] = "" - cell["refusal"] = {"code": "database_unreadable", - "message": f"You cannot read {db}."} + cell.update(_binding_refusal("database_unreadable", f"You cannot read {db}.")) cell.pop("limits", None) continue try: @@ -1861,13 +1866,10 @@ def refresh_sheet(workbook_id: str, session: Session = Depends(require_session)) except HTTPException as exc: detail = getattr(exc, "detail", None) or {} info = detail.get("error") if isinstance(detail, dict) else None - cell["state"] = "refused" - cell["value"] = None - cell["display"] = "" - cell["refusal"] = { - "code": (info or {}).get("code", "sheet_unresolved"), - "message": (info or {}).get("message", "This cell could not be refreshed."), - } + cell.update(_binding_refusal( + (info or {}).get("code", "sheet_unresolved"), + (info or {}).get("message", "This cell could not be refreshed."), + )) cell.pop("limits", None) continue cell["state"] = "fresh" @@ -1894,11 +1896,13 @@ def refresh_sheet(workbook_id: str, session: Session = Depends(require_session)) _recompute_formulas(book, fr, sheet_mod) recalc_ms = round((perf_counter() - started) * 1000.0, 1) try: + # R19: book came from get_workbook; clamping on refresh would destroy imported widths. saved = sheets.put_workbook(_store(session), workbook_id, {"name": book.get("name"), "sheets": book.get("sheets"), "refreshed_at": stamp, "rev": book.get("rev")}, actor=session.uname, - actor_name=session.user.get("name") or session.uname) + actor_name=session.user.get("name") or session.uname, + lossless=True) except sheets.SheetRefusal as exc: raise _refuse(exc) # ⭐⭐ R3 / W52-T12: REFRESH IS THE ONE EVENT ALLOWED TO INVALIDATE A READER'S CACHED SCOPE @@ -2265,6 +2269,36 @@ def _filter_cell_values(session, nodes, *, book=None, tab=None, today=None, seen raise err(400, exc.code, exc.message) +# ⛔⛔ W62-T12 / C2 / R29 — A `mode: "view"` BINDING IS SKIPPED BY EVERY METRIC RESOLVER, AND +# SKIPPING IS NOT THE SAME AS REFUSING. A placed view has no scalar value: its rows are read through +# `POST /sheets/{id}/view-rows` and PLACED by the client, because the placement is a structure op +# only the client can take. Before this guard the refresh door handed a view binding to +# `modules/sheet.py::clean_binding`, which REQUIRES an `aggregate`, so `Refresh All` marked every +# placed view `refused` and painted "That cell's source is not set up correctly." over a view that +# was perfectly well formed. Caught by this lane's own screenshot, not by a gate. +def _is_view_binding(cell) -> bool: + """True for a C2 placed view, which the metric resolvers must leave exactly as they found it.""" + binding = (cell or {}).get("binding") if isinstance(cell, dict) else None + return isinstance(binding, dict) and binding.get("mode") == "view" + + +# R48: the persisted code is short; the original cause stays intact in the document. +_BINDING_REFUSAL_DISPLAY = { + "database_unreadable": "#NO ACCESS", + "field_unknown": "#FIELD?", +} + + +def _binding_refusal(code, message): + """One refusal shape for compute, refresh and caller-scoped reads of metric cells.""" + return { + "state": "refused", + "value": None, + "display": _BINDING_REFUSAL_DISPLAY.get(code, "#ERROR!"), + "refusal": {"code": code, "message": message}, + } + + def _resolve_cell_binding(session, binding, book, tab, **kwargs): """Keep the legacy resolver calling convention for bindings without workbook inputs.""" if _has_cell_inputs(binding): @@ -2568,6 +2602,11 @@ def compute_cell(workbook_id: str, addr: str, body: dict = Body(default=None), raise err(404, "cell_missing", f"Cell {key} is not filled in.") if cell.get("kind") != "bound": raise err(409, "cell_not_bound", f"Cell {key} is not bound to a database.") + if _is_view_binding(cell): + # C2: a placed view has rows, not a value. Refusing BY NAME beats resolving it as a metric, + # which is what painted "That cell's source is not set up correctly." over a good view. + raise err(409, "cell_is_a_view", + f"Cell {key} holds a view, so it is read through its own rows door.") tab_ref = str((tab or {}).get("id") or (tab or {}).get("name") or "") binding = cell.get("binding") or {} @@ -2582,13 +2621,10 @@ def compute_cell(workbook_id: str, addr: str, body: dict = Body(default=None), #: a cell left `stale` would shimmer forever, and the reason belongs on the cell. detail = getattr(exc, "detail", None) or {} info = detail.get("error") if isinstance(detail, dict) else None - saved["state"] = "refused" - saved["value"] = None - saved["display"] = "" - saved["refusal"] = { - "code": (info or {}).get("code", "sheet_unresolved"), - "message": (info or {}).get("message", "This cell could not be refreshed."), - } + saved.update(_binding_refusal( + (info or {}).get("code", "sheet_unresolved"), + (info or {}).get("message", "This cell could not be refreshed."), + )) else: limits = resolved["limits"] saved["state"] = "fresh" @@ -2624,6 +2660,104 @@ def compute_cell(workbook_id: str, addr: str, body: dict = Body(default=None), return {"cell": wire, "rev": landed.get("rev"), "tab": tab_ref, "addr": key} +@router.post("/sheets/{workbook_id}/view-rows") +def view_rows(workbook_id: str, body: object = Body(default=None), + session: Session = Depends(require_session)): + """W62 C2 / R29: read the live rows of a placed view under this caller's walls.""" + from datetime import date + from fastapi.responses import JSONResponse + from core import perm_scope, sheets, user_tables + from modules import sheet as sheet_mod + + try: + binding = sheets.clean_view_binding(body.get("binding") if isinstance(body, dict) else None) + except sheets.SheetRefusal as exc: + return JSONResponse(status_code=400, content={"error": exc.code, "message": exc.message}) + try: + book = sheets.get_workbook(_store(session), workbook_id) + except sheets.SheetRefusal as exc: + raise _refuse(exc) + _require_reader(session, book) + database = _require_database(session, binding["database"]) + fields = list(_registry_assembly(session, database).get("fields") or []) + hidden = perm_scope.hidden_keys(session.user, database, fields, st=session.runtime) + visible = {field["key"]: field for field in fields if field.get("key") not in hidden} + requested = binding["fields"] + sorted_keys = {item["field"] for item in binding["sort"]} + for key in dict.fromkeys([*requested, *sorted(sorted_keys)]): + if key not in visible: + return JSONResponse(status_code=400, content={ + "error": "view_field_unknown", "message": f"Field {key} is not available in this database."}) + + # W62 C2: "Filter and sort apply to ALL rows BEFORE limit. total counts every matching row." + wall_tree = ((perm_scope.entry(session.user, database) or {}).get("filter") + if perm_scope.row_scope_applies(session.user, database) else None) + wall_nodes, wall_conj = _fe_parts(wall_tree) + scope_nodes, scope_conj = _fe_parts(binding.get("filter")) + # W62 C2: a filter-only field obeys the same grants; linked fields have their own resolver wall. + for key in sorted(sheet_mod.filter_columns_named(scope_nodes)): + if not key.startswith(sheet_mod.LINKED_FILTER_PREFIX) and key not in visible: + return JSONResponse(status_code=400, content={ + "error": "view_field_unknown", "message": f"Field {key} is not available in this database."}) + today = date.today() + try: + scope_nodes = _filter_cell_values(session, scope_nodes, book=book, today=today) + window = _ending_window(session, binding, book=book, today=today) + needed = (set(requested) | sorted_keys | sheet_mod.filter_columns_named(scope_nodes) + | sheet_mod.filter_columns_named(wall_nodes)) + cur, spec, cols = _mirror(session)(database, metric_window=window, needed=needed) + cols = dict(cols) + scope_nodes = _linked_filter_scope(session, database, scope_nodes, cur, spec, cols, today=today) + for key in dict.fromkeys([*requested, *sorted(sorted_keys | sheet_mod.filter_columns_named(scope_nodes))]): + if key in hidden or not (cols.get(key) or {}).get("sql"): + return JSONResponse(status_code=400, content={ + "error": "view_field_unknown", "message": f"Field {key} is not available in this database."}) + # W62 C2: an unanswerable row wall must refuse before either SQL read can widen it. + wall_compiled = sheet_mod.compile_scope(wall_nodes, wall_conj, cols, today=today) + if sheet_mod.widen_limits(wall_nodes, cols, wall_compiled): + raise ValueError("unapplied row wall") + count_sql, params, limits = sheet_mod.build_aggregate_query( + spec, {"aggregate": {"fn": "count"}}, cols, scope_nodes=scope_nodes, + scope_conj=scope_conj, wall_nodes=wall_nodes, wall_conj=wall_conj, today=today) + if limits: + raise ValueError("unapplied view filter") + except sheet_mod.SheetRefusal as exc: + return JSONResponse(status_code=400, content={"error": exc.code, "message": exc.message}) + except ValueError: + raise err(409, "scope_not_applicable", + "This view uses a restriction that cannot be applied to this database's live connection.") + + # W62 C2 / standing rule 1: "limit caps what is PLACED"; the matching count is unlimited. + total = (cur.execute(count_sql, params).fetchone() or [0])[0] + # ⭐⭐ W62-T14 — EVERY ROW CARRIES ITS RECORD ID, ALWAYS, AND IT IS NOT A PROJECTED FIELD. + # A placed Grid over one of our own tables writes a record back, and the write door is Data's + # own `PATCH /api/v1/tables/{database}/rows/{pid}`: without the id the client can only guess + # from a row's POSITION in the sheet, which is wrong the moment anyone sorts, filters or + # refreshes. So the id rides beside the values under the key `pid` while `fields` still names + # only the columns the sheet paints. ⛔ It is the SAME `spec["id"]` expression `build_ids_query` + # uses, so the identity here and the identity the drill lists can never be two different things. + id_expr = str(spec.get("id") or "").strip() + projection = ", ".join([*(cols[key]["sql"] for key in requested), + *([id_expr] if id_expr else [])]) + rows_sql = count_sql.replace("SELECT COUNT(*) FROM", f"SELECT {projection} FROM", 1) + if binding["sort"]: + # W62 C2 / R30: expressions come only from resolved columns; directions are validated. + order = ", ".join(cols[item["field"]]["sql"] + (" ASC" if item["dir"] == "asc" else " DESC") + for item in binding["sort"]) + rows_sql += " ORDER BY " + order + rows_sql += f" LIMIT {binding['limit']}" + keys = [*requested, *(["pid"] if id_expr else [])] + rows = [dict(zip(keys, row)) for row in cur.execute(rows_sql, params).fetchall()] + # W62 C2 / R34: Data's _records_or_refuse uses database access (checked above) plus + # W62 R34: records_mutable, with no separate edit-role grant; its row/field walls still apply. + editable = user_tables.records_mutable(database, st=session.runtime) + return {"fields": [{"key": key, "label": visible[key].get("label") or key, + "kind": visible[key].get("type") or visible[key].get("kind") or "text"} + for key in requested], + "rows": rows, "total": int(total or 0), "limit": binding["limit"], + "viewType": binding["viewType"], "editable": bool(editable)} + + @router.get("/sheets/{workbook_id}/cells/{addr}/drill") def drill_cell(workbook_id: str, addr: str, session: Session = Depends(require_session)): """`{database, view_id?, filter?, record_ids, count}` for `LinkGridModal` (R6, R13, R17). @@ -2659,6 +2793,9 @@ def drill_cell(workbook_id: str, addr: str, session: Session = Depends(require_s if cell.get("kind") != "bound": raise err(409, "literal_no_drill", "A hand typed value has no rows behind it.") + if _is_view_binding(cell): + raise err(409, "cell_is_a_view", + f"Cell {key} holds a view, so its rows are already on the sheet.") try: binding = sheet_mod.clean_binding(cell.get("binding")) @@ -2772,7 +2909,7 @@ def _asked_key(asked, home, fr): @router.post("/sheets/{workbook_id}/resolve-bindings") def resolve_bindings(workbook_id: str, body: dict = Body(default=None), session: Session = Depends(require_session)): - """`{addr: value}` for the NAMED bound cells only (contract C1, ruling R2). + """Values or named refusals for the NAMED bound cells only (contract C1, rulings R2 and R48). ⭐⭐ THIS IS THE DOOR THE COMMIT PATH USES, AND ITS WHOLE POINT IS THAT IT IS SMALL. Under R2 a formula evaluates in the browser the instant it is committed, so the only thing the @@ -2788,20 +2925,13 @@ def resolve_bindings(workbook_id: str, body: dict = Body(default=None), by THE EXACT STRING THE CALLER ASKED WITH, so a client looks its answer up under the reference it already holds and never has to re-derive this file's key spelling. - ⛔⛔ A KEY IS ABSENT WHEN NO BOUND CELL LIVES AT THAT ADDRESS, AND THAT IS THE ONE - DISTINCTION THE SHAPE CAN CARRY. An address that is empty, holds a typed literal or holds a - formula is not this door's business - lane A's evaluator already has those values in hand - - so asking for one costs nothing and answers nothing. - - ⛔⛔ AND HERE IS WHAT THE SHAPE CANNOT CARRY, STATED RATHER THAN HIDDEN: a bound cell whose - binding was REFUSED just now (a database wall, a row wall, a scope condition this database - cannot compile) comes back as `null`, which is also an empty AVG/MIN/MAX result. An empty - SUM returns zero. The cause is real and this door knows it; `{addr: value}` has nowhere to put it. - `POST /sheets/{id}/refresh` and `GET /sheets/{id}` both still carry the full - `{code, message}` refusal on the cell, so nothing is lost from the document - what is lost is - the client's ability to paint THIS cell's refusal at commit time. Raised as a dated PRD - amendment rather than fixed by widening the shape on one lane's authority, because lane A is - the consumer and a second shape agreed by nobody is worse than a known gap. + A KEY IS ABSENT WHEN NO BOUND CELL LIVES AT THAT ADDRESS. An address that is empty, holds a + typed literal or holds a formula is not this door's business - lane A's evaluator already + has those values in hand - so asking for one costs nothing and answers nothing. + + R48: a refused binding answers `{value: null, refusal: {code, message}}`, so it cannot be + mistaken for a legitimate empty aggregate. The flat shape is kept for every address that + has an answer, including a genuine null, so no existing reader of those answers changes. ⛔ ONE BAD CELL NEVER FAILS THE BATCH, exactly as in `refresh_sheet`'s bound-cell loop: a refusal is caught per cell and the siblings still answer. @@ -2869,11 +2999,15 @@ def resolve_bindings(workbook_id: str, body: dict = Body(default=None), try: by_binding[slot] = _resolve_cell_binding(session, binding, book, own_tab, view_memo=view_memo)["value"] - except HTTPException: - # ⛔ SWALLOWED HERE AND NOWHERE ELSE. See the docstring: the flat body has no - # room for a cause, and failing the whole batch on one walled cell would take - # every sibling's number down with it. The same refusal still travels in full on - # the refresh and read doors, which is where the cell's own sentence comes from. - by_binding[slot] = None + except HTTPException as exc: + detail = getattr(exc, "detail", None) or {} + info = detail.get("error") if isinstance(detail, dict) else None + by_binding[slot] = { + "value": None, + "refusal": { + "code": (info or {}).get("code", "sheet_unresolved"), + "message": (info or {}).get("message", "This cell could not be refreshed."), + }, + } answer[label] = by_binding[slot] return answer diff --git a/api/routes_starred.py b/api/routes_starred.py index e2a1e9df918c9c1d10829566af403e979cb6e51e..fa40e882bfaf7deff2ac9de57672f293aeca4917 100644 --- a/api/routes_starred.py +++ b/api/routes_starred.py @@ -208,7 +208,29 @@ def _scan_starred_views(session, keys=None, cache=None): import core.view_templates as view_templates granted = routes_nav._granted_view_ids(session) - out = [] + # ⭐⭐ W62-T17 (D-756) — EVERY GRANTED VIEW ID WAS RESOLVED ONCE PER DATABASE, and that loop is + # where `/starred`'s budget went. MEASURED on staging's Royal Imports, 26 databases + # (`AutoWiki/waves/wave62/proto/T17-profile.md`): 4,844 ms total, of which `_visible_views` was + # 3,075 ms — `object_shares` read 28 times (1,117 ms) and `TableStore.find_view` 26 times + # (2,072 ms), for ONE granted id. Neither figure is about a slow read: the grant registry is + # 8 KB and each read is a single ~40 ms round trip. They are about the same question being + # asked once per database. + # + # ⭐ A VIEW LIVES IN EXACTLY ONE DATABASE. Once a database has resolved a granted id, every + # LATER database in the scan is being asked a question whose answer cannot be yes — and each + # of those "no"s costs a `shares.may_see` read plus a full workspace-document read through + # `find_view`. Dropping resolved ids from the set handed on narrows only the LOOKUPS, never + # the answer: `_visible_views` still returns this caller's own views for every database, and a + # granted view is still admitted by `shares.may_see` before it is looked up at all. + # + # ⛔ AND THE OBVIOUS SECOND SAVING IS NOT AVAILABLE: lending `object_shares` for the pass + # (`user_tables.lend`) would collapse those 28 reads to one, but `TableStore.find_view` reaches + # `migrate_legacy_shared_views`, which WRITES grants and then READS THEM BACK to verify them. A + # lent snapshot would serve the pre-write value to that readback and the migration would raise + # `LegacySharedViewMigrationError` on any tenant that still has legacy shared views — a 500 on + # `/starred` in exchange for a few hundred milliseconds. `_Lent`'s own contract says it may + # only serve code "proven not to mutate" the lent key; this path is not that code. + resolved, out = set(), [] for key in order: doc = docs.get(key) if doc is None: @@ -217,8 +239,10 @@ def _scan_starred_views(session, keys=None, cache=None): if not ws_key: continue views = table_store.make(ws_key, st=session.runtime) - for vid, view in routes_nav._visible_views(doc, session.uname, session.admin, - granted, views).items(): + pending = granted - resolved + found = routes_nav._visible_views(doc, session.uname, session.admin, pending, views) + resolved |= pending & set(found) + for vid, view in found.items(): cfg = view.get("config") or {} if cfg.get("important") is True: out.append({"id": vid, "database": key, diff --git a/platform/core/external_access.py b/platform/core/external_access.py index cf646f9842d19559dabd3ee5acd1869f180a1b36..2312af7687425a10c923e79c08c44f4b4ff69e87 100644 --- a/platform/core/external_access.py +++ b/platform/core/external_access.py @@ -1,8 +1,9 @@ -"""Per-user credentials for the governed external read and build surface. +"""Per-user credentials carrying an access level and database subset for governed reads and +builds, with the maker's live grants still applying on top. -The credential is authentication only. Verification reconstructs the same PUBLIC user record -the normal API session carries, so every later database read still goes through ``perm_scope`` -and the existing Assistant mirror. This module never reads product rows and never caches a +Verification reconstructs the same PUBLIC user record the normal API session carries, so every +later database read still goes through ``perm_scope`` and the existing Assistant mirror. +This module never reads product rows and never caches a credential, user, bucket, or tenant runtime. """ import calendar @@ -17,6 +18,11 @@ from harness import runtime BUCKET = "external_access" +ACCESS_READ = "read" +ACCESS_READWRITE = "readwrite" +ACCESS_LEVELS = (ACCESS_READ, ACCESS_READWRITE) +ALL_DATABASES = "all" +MAX_SCOPE_DATABASES = 200 ALGORITHM = "sha256-salt-v1" AUTH_ERROR = "invalid_external_credential" _DUMMY_SALT = bytes(32) @@ -48,6 +54,50 @@ class ExternalAuthError(ExternalAccessError): """A uniform authentication refusal that reveals no credential or tenant detail.""" +def normalise_access(value) -> str: + if value is None or value == "": + return ACCESS_READWRITE + if value in ACCESS_LEVELS: + return value + raise ExternalAccessError("external_access_scope_invalid") + + +def normalise_databases(value): + if value is None or (isinstance(value, str) and value.strip().lower() == ALL_DATABASES): + return ALL_DATABASES + if not isinstance(value, (list, tuple)) or not 1 <= len(value) <= MAX_SCOPE_DATABASES: + raise ExternalAccessError("external_databases_invalid") + if any(not isinstance(item, str) or not item.strip() or len(item.strip()) > 200 + for item in value): + raise ExternalAccessError("external_databases_invalid") + return tuple(dict.fromkeys(item.strip() for item in value)) + + +def scope_of(record): + """Read legacy defaults, but preserve a stored empty subset as no database access.""" + record = record if isinstance(record, dict) else {} + access = record.get("access") + if access not in ACCESS_LEVELS: + access = ACCESS_READWRITE + databases = record.get("databases") + if isinstance(databases, (list, tuple)): + databases = tuple(item.strip() for item in databases + if isinstance(item, str) and item.strip()) + else: + databases = ALL_DATABASES + return access, databases + + +def may_write(access) -> bool: + return access == ACCESS_READWRITE + + +def database_allowed(databases, database) -> bool: + if databases == ALL_DATABASES: + return True + return isinstance(database, str) and bool(database.strip()) and database.strip() in databases + + @dataclass(frozen=True) class VerifiedCredential: """The authenticated public principal plus the request-scoped tenant runtime.""" @@ -56,6 +106,8 @@ class VerifiedCredential: key_id: str user: dict runtime: object + access: str = ACCESS_READWRITE + databases: object = ALL_DATABASES def _now() -> str: @@ -130,12 +182,15 @@ def _active_public_user(username: str, tenant: str) -> dict: def _metadata(record: dict) -> dict: """Return list-safe metadata. Salt, digest, algorithm and owner never cross this seam.""" + access, databases = scope_of(record) return { "id": record.get("id"), "label": record.get("label"), "created": record.get("created"), "last_used": record.get("last_used"), "revoked_at": record.get("revoked_at"), + "access": access, + "databases": ALL_DATABASES if databases == ALL_DATABASES else list(databases), } @@ -150,8 +205,10 @@ def label_for(st, key_id: str) -> str: return label -def mint(st, username: str, tenant: str, label: str) -> tuple[dict, str]: +def mint(st, username: str, tenant: str, label: str, access=None, databases=None) -> tuple[dict, str]: """Create one credential and return ``(metadata, token)``; the token is never persisted.""" + access = normalise_access(access) + databases = normalise_databases(databases) owner, tenant_key = _normalise_owner(username, tenant) _assert_bound_runtime(st, tenant_key) _active_public_user(owner, tenant_key) @@ -179,6 +236,8 @@ def mint(st, username: str, tenant: str, label: str) -> tuple[dict, str]: "username": owner, "tenant": tenant_key, "label": clean_label, + "access": access, + "databases": ALL_DATABASES if databases == ALL_DATABASES else list(databases), "salt": salt.hex(), "hash": _digest(salt, secret), "algo": ALGORITHM, @@ -306,8 +365,10 @@ def verify(token: str) -> VerifiedCredential: except ExternalAccessError as exc: raise ExternalAuthError(AUTH_ERROR) from exc + access, databases = scope_of(record) if not _stamp_is_due(record.get("last_used"), time.time()): - return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st) + return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st, + access=access, databases=databases) used_at = _now() @@ -332,7 +393,8 @@ def verify(token: str) -> VerifiedCredential: raise except Exception as exc: raise ExternalAuthError(AUTH_ERROR) from exc - return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st) + return VerifiedCredential(tenant=tenant_key, key_id=key_id, user=public, runtime=st, + access=access, databases=databases) except ExternalAuthError: raise except Exception as exc: diff --git a/platform/core/relations.py b/platform/core/relations.py index f07cc091b4b258506c338ebb321c6697b4a1d41a..a8c95a4b9ec8bae8ce24a52dd10aa78a3faa8727 100644 --- a/platform/core/relations.py +++ b/platform/core/relations.py @@ -265,6 +265,20 @@ def _link_bag(field): return bag if isinstance(bag, dict) else None +def declared_through(link): + """R50 / C4: return the intermediate of a complete declared two-hop link, or ''. + + The intermediate is a join, not a destination. R50 says grants on both ends suffice; + intermediate fields are never projected, returned or rendered. An incomplete through + bag is not a declared two-hop link and earns no traversal admission. Manual links do + not declare a traversal either. + """ + if (isinstance(link, dict) and link.get('through') and link.get('throughOn') + and link.get('throughTo') and link.get('selectionMode') != 'manual'): + return str(link['through']) + return '' + + def _entry(key, label, target, direction, field=None, on=None, frm=None, served=True, where=None, cardinality=None): """ONE `/related` entry, in C6's shape. diff --git a/platform/core/sheet_structure.py b/platform/core/sheet_structure.py index 01e2ad034f539abf15c2db3b539dc7ac1d52fe94..102ae0bc304a82b55a749e6d809746f34d6e5b7f 100644 --- a/platform/core/sheet_structure.py +++ b/platform/core/sheet_structure.py @@ -51,8 +51,10 @@ def parse_ref(text: str) -> dict | None: "col": col.upper(), "rowAbs": row_abs == "$", "row": int(row)} -def format_ref(ref: dict) -> str: +def format_ref(ref: dict | str) -> str: """Format a CellRef, preserving dollar marks and quoting non-bare sheet names.""" + if isinstance(ref, str): + return ref body = (f"{'$' if ref['colAbs'] else ''}{ref['col']}" f"{'$' if ref['rowAbs'] else ''}{ref['row']}") sheet = ref["sheet"] @@ -62,7 +64,8 @@ def format_ref(ref: dict) -> str: return name + "!" + body -def rewrite_refs(text: str, rewrite: Callable[[dict], dict]) -> str: +def rewrite_refs(text: str, rewrite: Callable[[dict], dict | str], + rewrite_range: Callable[[dict, dict, str], str] | None = None) -> str: """Walk text exactly as a1.ts::rewriteRefs; strings and function names survive.""" src = str(text or "") out = [] @@ -90,6 +93,15 @@ def rewrite_refs(text: str, rewrite: Callable[[dict], dict]) -> str: if match and not _FUNCTION.match(src, match.end()): ref = parse_ref(match[0]) if ref: + separator = re.match(r"\s*:\s*", src[match.end():]) if rewrite_range else None + if separator: + end = _REF.match(src, match.end() + separator.end()) + if end and not _FUNCTION.match(src, end.end()): + last = parse_ref(end[0]) + if last: + out.append(rewrite_range(ref, last, separator[0])) + i = end.end() + continue out.append(format_ref(rewrite(ref))) i = match.end() continue @@ -98,14 +110,13 @@ def rewrite_refs(text: str, rewrite: Callable[[dict], dict]) -> str: return "".join(out) -def shift_index(value: int, at: int, delta: int) -> int: - """References in a deleted band point at its deletion point, never at #REF!.""" - if value < at: - return value - if delta > 0: - return value + delta - past = at - delta - return max(1, at if value < past else value + delta) +def shift_index(value: int, at: int, delta: int) -> int | None: + """R49 retires the clamp: both grammars already parse #REF! as an error token. + + The feared bad_formula cannot occur. Deleted indices return None; only a range + with surviving cells may clamp its deleted endpoint and shrink. + """ + return _moved_index(value, at, delta) def _same_name(left: str, right: str) -> bool: @@ -121,15 +132,38 @@ def shift_for_structure(text: str, shift: dict, own_sheet: str, *, bare: bool = if not shift["delta"] or (bare and parse_ref(text) is None): return str(text or "") + def index(ref): + return ref["row"] if shift["axis"] == "row" else col_to_index(ref["col"]) + + def at_index(ref, value): + return {**ref, shift["axis"]: value if shift["axis"] == "row" else index_to_col(value)} + def rewrite(ref): if not _same_name(ref["sheet"] or own_sheet, shift["sheet"]): return ref - if shift["axis"] == "row": - return {**ref, "row": shift_index(ref["row"], shift["at"], shift["delta"])} - return {**ref, "col": index_to_col(shift_index( - col_to_index(ref["col"]), shift["at"], shift["delta"]))} + moved = shift_index(index(ref), shift["at"], shift["delta"]) + return "#REF!" if moved is None else at_index(ref, moved) + + def rewrite_range(first, last, separator): + sheet = first["sheet"] or own_sheet + if last["sheet"] and not _same_name(sheet, last["sheet"]): + return format_ref(rewrite(first)) + separator + format_ref(rewrite(last)) + # A bare right endpoint inherits the left endpoint's sheet, not the formula's tab. + if not _same_name(sheet, shift["sheet"]): + return format_ref(first) + separator + format_ref(last) + start, end = index(first), index(last) + low, high = min(start, end), max(start, end) + if low >= shift["at"] and high < shift["at"] - shift["delta"]: + return "#REF!" + + def move(value): + moved = shift_index(value, shift["at"], shift["delta"]) + return moved if moved is not None else shift["at"] if value == low else shift["at"] - 1 + + return format_ref(at_index(first, move(start))) + separator + format_ref(at_index(last, move(end))) - return rewrite_refs(text, rewrite) + # Insertions keep the existing per-reference scan; range handling is deletion-only. + return rewrite_refs(text, rewrite, rewrite_range if shift["delta"] < 0 else None) def _moved_index(value: int, at: int, delta: int) -> int | None: @@ -246,8 +280,8 @@ def _move_charts(tab: dict, tab_id: str, op: dict) -> None: for key in ("geometry", "anchor"): geometry = chart.get(key) if isinstance(geometry, dict) and op["axis"] in geometry: - geometry[op["axis"]] = shift_index( - geometry[op["axis"]] + 1, op["at"], op["delta"]) - 1 + moved = shift_index(geometry[op["axis"]] + 1, op["at"], op["delta"]) + geometry[op["axis"]] = (op["at"] if moved is None else moved) - 1 def apply_structure(book: dict, tab_id: str, op: dict) -> dict: diff --git a/platform/core/sheets.py b/platform/core/sheets.py index 3049596d6fa4dcb1b5c5854484ea943b797539c0..71c1bf848f6407c7ce67aadde0c0512dd3329555 100644 --- a/platform/core/sheets.py +++ b/platform/core/sheets.py @@ -93,6 +93,11 @@ WRITE_LOG_REVS = 200 CELL_KINDS = ("literal", "formula", "bound", "empty") CELL_STATES = ("fresh", "stale", "typed", "refused") +# W62 C2 / R31: "every view type can be placed" inside the binding. +VIEW_BINDING_TYPES = ("grid", "kanban", "map", "gallery", "timeline", "list", "calendar", "catalog", "swipe") +VIEW_BINDING_LIMIT_DEFAULT = 100 +VIEW_BINDING_LIMIT_MAX = 10000 + #: A literal is ALWAYS this state (R19). Stated once, here, so no caller has to remember it. #: ⚠ AN `empty` CELL TAKES THE SAME STATE, deliberately: it holds no binding, so `stale` would say #: a refresh is owed for a value that does not exist, and a reader counting stale cells would @@ -261,6 +266,7 @@ _MAX_DECIMALS = 10 #: constants, which is why nothing could grow a sheet or remember a dragged column: there was #: nowhere to put the answer. These are the DEFAULTS a sheet is created with and the ceiling it may #: grow to, and `web/src/sheet/types.ts` mirrors the three defaults. +#: W62 C1 / R14 also mirrors the width bounds there; the client half lands in W62-T05. #: #: ⚠ THE CEILING IS A RENDERING BUDGET, NOT A GRAMMAR LIMIT. `_ADDR` below already admits three #: column letters and seven row digits, and `a1.ts` agrees with it; 702 is the last TWO letter @@ -273,7 +279,7 @@ MIN_SHEET_COLS = 1 MIN_SHEET_ROWS = 1 MAX_SHEET_COLS = 702 MAX_SHEET_ROWS = 100000 -MIN_COL_WIDTH = 40 +MIN_COL_WIDTH = 0 MAX_COL_WIDTH = 1000 #: A column key in `colWidths`. Letters only: a WIDTH belongs to a column, and a row number there @@ -690,6 +696,55 @@ def _clean_sparkline(raw): return {"kind": kind, "range": rect} +def clean_view_binding(raw): + """W62-T10 / C2: normalize a PLACED VIEW binding, or raise `SheetRefusal` with a named code. + + `{database, mode: "view", viewType, fields: [key], filter?, sort: [{field, dir}], period?,` + `limit (1..10000, default 100), onDrag?, block?: {cols, rows}}`. + + ⛔ IT IS A SEPARATE VALIDATOR FROM `modules/sheet.py::clean_binding` RATHER THAN A WIDENING OF + IT. That one REQUIRES an `aggregate`, because a metric binding IS an aggregate; a view has + none. Widening it would make `aggregate` optional for every caller, and the day a metric + binding arrives without one it would resolve to nothing instead of refusing by name. + """ + if not isinstance(raw, dict) or not isinstance(raw.get("database"), str) or not raw["database"].strip(): + raise SheetRefusal("view_binding_invalid", "A view binding must be an object naming a database.") + if raw.get("mode", raw.get("kind")) != "view": + raise SheetRefusal("view_binding_mode_unknown", "A view binding must choose view mode.") + if raw.get("viewType") not in VIEW_BINDING_TYPES: + raise SheetRefusal("view_binding_type_unknown", "That view display type is not supported.") + fields = raw.get("fields") + if not isinstance(fields, list) or not fields or any( + not isinstance(key, str) or not key.strip() for key in fields): + raise SheetRefusal("view_binding_fields_invalid", "A view needs a non-empty list of field keys.") + sort = raw.get("sort", []) + if not isinstance(sort, list) or any( + not isinstance(item, dict) or not isinstance(item.get("field"), str) + or not item["field"].strip() or item.get("dir") not in ("asc", "desc") for item in sort): + raise SheetRefusal("view_binding_sort_invalid", "A view sort needs a field key and asc or desc direction.") + limit = raw.get("limit", VIEW_BINDING_LIMIT_DEFAULT) + if isinstance(limit, bool) or not isinstance(limit, int) or not 1 <= limit <= VIEW_BINDING_LIMIT_MAX: + raise SheetRefusal("view_binding_limit_invalid", "A view limit must be a whole number from 1 to 10000.") + out = {"database": raw["database"].strip(), "mode": "view", "viewType": raw["viewType"], + "fields": list(fields), "sort": [{"field": item["field"], "dir": item["dir"]} for item in sort], + "limit": limit} + for key in ("filter", "block"): + if key in raw: + out[key] = _document_object(raw[key], "view_binding_" + key) + # W62 C2: reuse only the existing pure metadata cleaners, never the metric binding validator. + if "period" in raw or "onDrag" in raw: + from modules import sheet as sheet_mod + for key, cleaner in (("period", sheet_mod.clean_period), ("onDrag", sheet_mod.clean_on_drag)): + if key in raw: + try: + value = cleaner(_json_safe(copy.deepcopy(raw[key]), "view binding " + key)) + except sheet_mod.SheetRefusal as exc: + raise SheetRefusal(exc.code, exc.message) from exc + if value is not None: + out[key] = value + return out + + def clean_cell(raw, addr=""): """Normalize ONE cell, or raise `SheetRefusal` with a named code. @@ -735,7 +790,17 @@ def clean_cell(raw, addr=""): if meta and meta.get("kind", "normal") not in ("normal", "array", "shared", "dataTable"): raise SheetRefusal("cell_formula_kind_unknown", "That formula representation is not supported.") binding = raw.get("binding") - if isinstance(binding, dict): + # ⭐⭐ W62-T10 / C2 — A PLACED VIEW LIVES INSIDE `kind: "bound"`, AND `CELL_KINDS` STAYS + # FROZEN. Wave 49's cell-kind contract is the reason: the vocabulary is pinned here and two + # gates prove the client agrees with it, so a fifth kind would be a change to a frozen + # contract for a feature that does not need one. The discriminator is `binding.mode == "view"`, + # and a view binding carries no `aggregate` at all, which is why it cannot go through the + # metric validator below. + if isinstance(binding, dict) and (binding.get("mode") == "view" or binding.get("kind") == "view"): + if kind != "bound": + raise SheetRefusal("view_binding_invalid", "A view binding needs a bound cell.") + binding = clean_view_binding(binding) + elif isinstance(binding, dict): for tag in ("kind", "mode"): if tag in binding and binding[tag] not in ("number", "column"): raise SheetRefusal("cell_binding_kind_unknown", "A cell binding must choose Number or Column.") @@ -785,7 +850,7 @@ def clean_cell(raw, addr=""): cell["value"] = _json_safe(raw.get("value"), "cell value") for key in ("binding", "source", "refusal"): - blob = raw.get(key) + blob = binding if key == "binding" else raw.get(key) if blob is None: continue if not isinstance(blob, dict): @@ -888,12 +953,19 @@ def _clean_col_widths(raw, cols, *, lossless=False): "Column " + (letters or str(key)) + " is not a column letter.") if isinstance(value, bool) or not isinstance(value, (int, float)): if lossless: - raise SheetRefusal("col_width_invalid", "A column width must be a finite positive size.") + raise SheetRefusal("col_width_invalid", "A column width must be a finite size.") continue if not math.isfinite(value): raise SheetRefusal("col_width_invalid", "A column width must be a finite size.") - if lossless and value <= 0: - raise SheetRefusal("col_width_invalid", "A column width must be a finite positive size.") + # ⭐ W62-T04 / R14 — A ZERO IS NOW A WIDTH, AND A NEGATIVE ONE STILL IS NOT. Before this + # ticket the lossless door refused every `value <= 0`, which is the same test for both; R14 + # made 0 Excel's own hidden column, so an xlsx carrying one must import rather than refuse. + # A negative width is still nothing a file or a person can mean, so it keeps its refusal on + # the lossless door and is clamped to the floor on a live save. + if lossless and value < 0: + raise SheetRefusal("col_width_invalid", "A column width cannot be negative.") + #: W62 C1 / R14 / R19: reads retain hidden zero widths and exact imported values; + #: only a live save applies the bounds a person can set. width = value if lossless else max(MIN_COL_WIDTH, min(MAX_COL_WIDTH, int(value))) if lossless or width != DEFAULT_COL_WIDTH: out[letters] = width @@ -1204,15 +1276,18 @@ def _clean_base_row_height(raw): return raw -def _clean_row_heights(raw): +def _clean_row_heights(raw, *, lossless=False): if raw is None: return {} out = _document_object(raw, "sheet_row_heights") for row, height in out.items(): if not re.fullmatch(r"[1-9][0-9]*", str(row)) or int(row) > DOCUMENT_MAX_ROWS \ or isinstance(height, bool) or not isinstance(height, (int, float)) \ - or not math.isfinite(height) or height <= 0: + or not math.isfinite(height) or (lossless and height <= 0): raise SheetRefusal("sheet_row_height_invalid", "A row height needs a row number and a positive size.") + #: W62 C1 / R19: a live size is clamped, while imported positive sizes stay exact. + if not lossless: + out[row] = max(18, min(200, height)) return out @@ -1329,7 +1404,7 @@ def _clean_sheet(raw, index=0, *, lossless=False): "charts": _clean_charts(raw.get("charts")), "imported": _clean_imported(raw.get("imported")), "rowHeight": _clean_base_row_height(raw.get("rowHeight")), - "rowHeights": _clean_row_heights(raw.get("rowHeights")), + "rowHeights": _clean_row_heights(raw.get("rowHeights"), lossless=lossless), "visibility": visibility, "view": _clean_view(raw.get("view")), "print": _clean_print(raw.get("print")), @@ -1371,7 +1446,7 @@ def _clean_sheets(raw, *, lossless=False): # ── the workbook ───────────────────────────────────────────────────────────────────────────── -def migrate_workbook(raw): +def migrate_workbook(raw, *, lossless=True): """Pure unversioned/v0 -> v1 migration; validate current documents without recalculating. A read never writes or changes `rev`, timestamps or cached results. Unknown versions @@ -1395,7 +1470,7 @@ def migrate_workbook(raw): if book.get("print") is not None: book["print"] = _clean_print(book["print"]) book["schemaVersion"] = SHEET_SCHEMA_VERSION - book["sheets"] = _clean_sheets(raw.get("sheets"), lossless=True) + book["sheets"] = _clean_sheets(raw.get("sheets"), lossless=lossless) return _json_safe(book, "workbook") @@ -1564,7 +1639,7 @@ def get_workbook(st, workbook_id): return _carry_write_logs(migrate_workbook(doc), doc) -def put_workbook(st, workbook_id, raw, expect_rev=None, *, actor="", actor_name=""): +def put_workbook(st, workbook_id, raw, expect_rev=None, *, actor="", actor_name="", lossless=False): """Replace a workbook's name and sheets, bumping `rev`. ⛔ `id`, `created_by`, `access` and `created_at` are PRIOR-WINS: a client may not reassign a workbook by @@ -1586,7 +1661,8 @@ def put_workbook(st, workbook_id, raw, expect_rev=None, *, actor="", actor_name= if expect_rev is False: expect_rev = None - incoming = migrate_workbook(raw) + #: W62 C1 / R19: app and AI saves share bounded defaults; import and refresh opt out. + incoming = migrate_workbook(raw, lossless=lossless) sheets = incoming["sheets"] name = _text(raw.get("name"), _MAX_NAME) refreshed_at = raw.get("refreshed_at") @@ -1977,6 +2053,7 @@ def _index_row(book): "id": book.get("id") or "", "name": book.get("name") or "", "created_by": book.get("created_by") or "", + "has_access": "access" in book, "created_at": book.get("created_at") or "", "updated_at": book.get("updated_at") or "", "rev": int(book.get("rev") or 0), @@ -2036,9 +2113,11 @@ def narrow_workbook(st, workbook_id): if not state.get("id"): raise SheetRefusal("workbook_missing", "That workbook no longer exists.") state.setdefault("access", "private") - return state, None + return state, state - _persist(st, workbook_key(wid), _apply) + # R28 / W62-T25: explicit sharing changes the fact used by the index's visibility filter. + book = _persist(st, workbook_key(wid), _apply) + _index_upsert(st, book) def workbook_shares(st, workbook_id): @@ -2094,8 +2173,8 @@ 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. - Creation and explicit sharing stamp access. Read the document every time: - _index_upsert rebuilds its row after every save, and PUT must preserve marker absence too. + 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. """ return bool(role) or "access" not in book or not book.get("created_by") @@ -2104,7 +2183,7 @@ def may_read_workbook(book, role): def project_shares(st, rows, viewer=None, *, is_admin=False, role_on_workbook=None): """Join `shares` onto a list of index rows. Returns NEW rows; the stored ones are untouched. - A named viewer filters rows against each live workbook document and receives their role. + 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. `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 @@ -2121,22 +2200,12 @@ def project_shares(st, rows, viewer=None, *, is_admin=False, role_on_workbook=No 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. That case still reads the document, - because admitting a row wrongly is the failure that matters here. - - ⚠ ONE BEHAVIOUR NARROWS, AND IT IS NOT A SIDE EFFECT. The old read also filtered out a - pointer row whose DOCUMENT IS GONE, as a by-product of needing the document rather than as a - designed guard — the other caller of this function passes no viewer and has never had it. - That filter now applies to every viewer EXCEPT one already holding a role without asking the - document: an admin, the row's own creator, or the holder of a grant. Measured, not reasoned: - a ghost still disappears for a stranger and for a viewer whose grants are on other rows; it - survives only for the admin, who may read everything anyway. - ⛔ AND THE DIRECTION IS THE SAFE ONE. A row can only survive that could not have been REFUSED - on its contents — `access` is consulted for every viewer whose admission actually depends on - it. Opening a ghost answers `workbook_missing` through `_require_reader`, which reads the - document for real, and `_index_drop` removes the row with the document, so a ghost needs a - torn write to exist at all. A stale row in an admin's list until the next index write is a - far smaller defect than 12.5 seconds on every Sheet open. + 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. """ out = [] bulk = {} @@ -2161,9 +2230,16 @@ def project_shares(st, rows, viewer=None, *, is_admin=False, role_on_workbook=No 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"]: - book = _read(st, workbook_key(row["id"])) - if not book or not book.get("id") or not may_read_workbook(book, role): - continue + 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" joined["shares"] = shares_from_record(rec) out.append(joined) @@ -2659,8 +2735,8 @@ def read_index(st, viewer=None, *, is_admin=False, role_on_workbook=None): so a `rev` here without the check there (or the check without this) is a guard nothing can reach ([[a-rev-guard-inert-on-the-events-channel]]). The pair lands together or not at all. - ⚠ WHICH WRITES MOVE IT IS PART OF THE CONTRACT: `save_folders` and `_index_drop`, which are - the two that rewrite the folder, placement and star strata this guard protects. + D-639 / W62-T25: `save_folders`, `merge_placements` and `_index_drop` move the revision + because they write the folder, placement and star strata this guard protects. `_index_upsert` deliberately does NOT, because it is called by `create_workbook`, `put_workbook` AND `patch_cells` — bumping there would make every cell edit 409 a folder drag that touched nothing the guard is about. @@ -2772,3 +2848,52 @@ def save_folders(st, folders, item_folders, starred=None, expect_rev=None): result["workbooks"] = project_shares(st, result.get("workbooks")) result["repairs"] = repairs return result + + +def merge_placements(st, placements, *, starred=None): + """D-639 / W62-T25: apply only named moves to the current rail under the store's lock. + + None unfiles a workbook. Disjoint moves need no expected revision; every accepted merge + advances it so a stale wholesale save still refuses. Stars change only if explicitly supplied. + """ + if not isinstance(placements, dict): + raise SheetRefusal("placements_not_a_map", "Sheet placements must be a map of workbook ids.") + + def _apply(state): + rows = [r for r in (state.get("workbooks") or []) + if isinstance(r, dict) and r.get("id")] + ids = {r["id"] for r in rows} + merged = dict(state.get("itemFolders") or {}) + for wid, fid in placements.items(): + if wid not in ids: + raise SheetRefusal("placement_missing_workbook", + 'The workbook "' + str(wid) + '" no longer exists in the Sheet rail.') + if fid is None: + merged.pop(wid, None) + else: + merged[wid] = fid + # D-639: validate the merged tree inside the same update, before changing any stratum. + # D-639: use wholesale repair names, but refuse a malformed delta instead of losing it. + _tree, placed, repairs = validate_folder_tree(state.get("folders"), merged) + if repairs: + repair = repairs[0] + raise SheetFolderRefusal( + repair["reason"], + 'The placement for workbook "' + str(repair.get("item_id") or "") + + '" cannot be saved. Use an existing folder id or null to unfile it.', + folder_id=str(repair.get("folder_id") or ""), reason=repair["reason"]) + for wid, fid in placements.items(): + if fid is not None: + merged[wid] = placed[wid] + state["itemFolders"] = merged + if starred is not None: + state["starred"] = _clean_starred(starred, ids, repairs) + state["rev"] = _index_rev(state) + 1 + return state, {"workbooks": rows, "folders": state.get("folders") or [], + "itemFolders": merged, "starred": state.get("starred") or [], + "rev": _index_rev(state), "repairs": repairs} + + result = _persist(st, INDEX_KEY, _apply, seed={"workbooks": []}) + # D-639: grant reads happen after the store releases its update lock, as in save_folders. + result["workbooks"] = project_shares(st, result["workbooks"]) + return result diff --git a/platform/core/user_tables.py b/platform/core/user_tables.py index 02c9f7296ae4f4c8c2b74f186e196d3bf20d5bd0..a0bff373698b395df94534c9e9399351324247e2 100644 --- a/platform/core/user_tables.py +++ b/platform/core/user_tables.py @@ -218,7 +218,20 @@ def is_connected(table_key, st=None): key = str(table_key or '').strip() if key in _CONNECTED: return True - table = get(key, st) or {} + # ⭐⭐ W62-T17 (D-739) — `all_defs`, NOT `get`, AND THE THREE KEYS BELOW ARE THE WHOLE ARGUMENT. + # This function asks `connected`, `source` and `recordMode`. Every one of them is a DEFINITION + # key; not one of them is a row. `get` reaches `all_tables`, which is the whole tenant + # document — MEASURED at 97,859,052 bytes and 2,274-2,816 ms PER CALL on staging's Royal + # Imports (`AutoWiki/waves/wave62/proto/T17-profile.md`), against 106,779 bytes for the + # rows-free projection that answers identically. D-739 called this "a platform-wide hazard, + # not a templates one: anything that calls it in a loop has this defect", and the loop callers + # were never the only victims — one call was already over the whole 2,000 ms module budget. + # + # ⚠ THE PROJECTION IS SAFE HERE FOR THE SAME REASON IT IS SAFE IN `/nav`: `all_defs` drops + # exactly `rows`, raises loudly (or falls back to the whole read) rather than answering empty, + # and a lent handle already serves whichever document the pass is holding. A caller that lends + # the WHOLE document gets MORE than asked for, which is the one direction this may be wrong in. + table = all_defs(st).get(key) or {} if not table: return False if table.get('connected') is True: diff --git a/web/package-lock.json b/web/package-lock.json index c5d6675a9017fa7953dd2a44a3cae2bcc3a1702e..4bc2d28f73eb6adc7b9f8a85106cdb930b3b091f 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -11,6 +11,8 @@ "@glideapps/glide-data-grid": "^6.0.3", "lodash": "^4.18.1", "marked": "^4.3.0", + "overlayscrollbars": "^2.16.0", + "overlayscrollbars-react": "^0.5.6", "react": "^18.3.1", "react-dom": "^18.3.1", "vega": "^6.3.1", @@ -2358,6 +2360,22 @@ "node": ">=0.10.0" } }, + "node_modules/overlayscrollbars": { + "version": "2.16.0", + "resolved": "https://registry.npmjs.org/overlayscrollbars/-/overlayscrollbars-2.16.0.tgz", + "integrity": "sha512-N03oje/q7j93D0aLZtoCdsDSYLmhheSsv8H7oSLE7HhdV9P/bmCURtLV/KbPye7P/bpfyt/obSfDpGUYoJ0OWg==", + "license": "MIT" + }, + "node_modules/overlayscrollbars-react": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/overlayscrollbars-react/-/overlayscrollbars-react-0.5.6.tgz", + "integrity": "sha512-E5To04bL5brn9GVCZ36SnfGanxa2I2MDkWoa4Cjo5wol7l+diAgi4DBc983V7l2nOk/OLJ6Feg4kySspQEGDBw==", + "license": "MIT", + "peerDependencies": { + "overlayscrollbars": "^2.0.0", + "react": ">=16.8.0" + } + }, "node_modules/oxlint": { "version": "1.75.0", "resolved": "https://registry.npmjs.org/oxlint/-/oxlint-1.75.0.tgz", diff --git a/web/package-lock.linux.sha256 b/web/package-lock.linux.sha256 index 64d84248f51f9fed45e676358e7ec8e49ec7d42f..36d50c8749e3210ba58746dd4b0bd447334df181 100644 --- a/web/package-lock.linux.sha256 +++ b/web/package-lock.linux.sha256 @@ -1,2 +1,2 @@ -3875e749397966dc170a4f83ae04cddc47a2989828cd5c46970cad69dedb255a package-lock.json -# resolved on linux/x64 by HF job 6aa6bba021047bf1b0384360 on 2026-09-13, npm ci passed there before upload. Regenerate ONLY with lockfile_linux.py (W-170). +e3e18054f50ef4119f7d9f38fdb8c712c1277df2dfa2ba5418bb683f56afb7af package-lock.json +# resolved on linux/x64 natively in the SDLC 62 wave container (lane H, W62-T33) on 2026-09-16; `npm ci` passed there before this receipt was written. `lockfile_linux.py` submits an HF job to reach a Linux box; this container IS one, so it ran `npm install --save` and `npm ci` here instead. Regenerate ONLY on linux/x64 (W-170). diff --git a/web/package.json b/web/package.json index 29d65db9918a22c1afee6ca71598b6567eaefaf2..016ca63d7dac1d3b76fbd30603574024f2d351f0 100644 --- a/web/package.json +++ b/web/package.json @@ -18,6 +18,8 @@ "@glideapps/glide-data-grid": "^6.0.3", "lodash": "^4.18.1", "marked": "^4.3.0", + "overlayscrollbars": "^2.16.0", + "overlayscrollbars-react": "^0.5.6", "react": "^18.3.1", "react-dom": "^18.3.1", "vega": "^6.3.1", diff --git a/web/src/assistant/AssistantPage.tsx b/web/src/assistant/AssistantPage.tsx index 74f1656b0d8e7f0b70785ab2180229ace51415c8..aaceb74e563852320cdb5f19d96f258accbd8a3f 100644 --- a/web/src/assistant/AssistantPage.tsx +++ b/web/src/assistant/AssistantPage.tsx @@ -31,6 +31,12 @@ export interface AssistantPageProps { granted: NavEntry[]; } * the disclosure and refusal extension local and read it through the single cast in ask(). */ type RoutedTo = { database: string; label: string; reason: string; auto: boolean; alternatives: Array<{ database: string; label: string }> }; +type RoutingDisclosure = { + database: string; + label: string; + alternatives: Array<{ database: string; label: string }>; + answered: boolean; +}; // W61-T30: the drafting extension stays local while the shared API union belongs to another lane. type SemanticDraft = { database: string; label: string; canApprove: boolean; sentToAdmins: boolean; @@ -56,16 +62,43 @@ async function draftRequest(database: string, fields?: string[], question?: stri return response.json(); } -function RoutingLine({ routed, onSwitch, busy }: { - routed?: RoutedTo; onSwitch?: (database: string) => void; busy?: boolean; +/** + * R47 / R6 — the answer names its own source, and the override lives here rather than in front of + * the question. One line: "Answered from · Change". + * + * ⛔ CHANGE IS A SELECT, NOT A ROW OF BUTTONS, AND THAT WAS MEASURED ON A RUNNING TENANT. This + * first shipped as one quiet button per alternative; tenant #0 can read 28 databases, so opening + * Change pushed the answer six rows down the screen behind a wall of names, eleven of which read + * "Formula contract". A select collapses the whole override into the one word the ruling names, + * hands the browser the scrolling, the keyboard and the screen reader, and is the same control + * the model picker one line below already is. Its value is held at "" so the word Change survives + * every switch, and `onChange` ignores that empty value. + * + * ⚠ A refusal renders the control ALONE: the refusal sentence under it already names the database + * it could not answer from, so "Answered from" would be the one thing on the surface that is not + * true. The override still has to be reachable there, which is the state that needs it most. + */ +function RoutingLine({ disclosure, onSwitch, busy }: { + disclosure?: RoutingDisclosure | null; onSwitch?: (database: string) => void; busy?: boolean; }) { - if (!routed?.auto || !routed.database) return null; + if (!disclosure?.database || !disclosure.alternatives.length) return null; return
- {routed.reason} - {onSwitch ? routed.alternatives.map((alternative) => ( - - )) : null} + {disclosure.answered ? <> + Answered from {disclosure.label} + {onSwitch ? : null} + : null} + {onSwitch ? ( + + ) : null}
; } @@ -217,7 +250,7 @@ export function AssistantMessage({ onRate, identity, governedArtifacts, - routed, + disclosure, onSwitch, busy, }: { @@ -226,7 +259,7 @@ export function AssistantMessage({ onRate?: (id: string, rating: "up" | "down" | null, reason?: string) => void; identity?: QueryModelIdentity; governedArtifacts?: QueryToolArtifacts; - routed?: RoutedTo; + disclosure?: RoutingDisclosure | null; onSwitch?: (database: string) => void; busy?: boolean; }) { @@ -260,7 +293,7 @@ export function AssistantMessage({ const numeric = message.numeric; return (
- +

{message.content}

Provider {identity?.provider || message.model || "Not reported"} @@ -436,9 +469,6 @@ export default function AssistantPage({ granted }: AssistantPageProps) { const [queryRefresh, setQueryRefresh] = useState(0); const [modelCatalog, setModelCatalog] = useState([]); const [artifactByView, setArtifactByView] = useState>({}); - const [routingByMessage, setRoutingByMessage] = useState>({}); const [alignment, setAlignment] = useState; maxSelections: number; @@ -466,6 +496,12 @@ export default function AssistantPage({ granted }: AssistantPageProps) { const databases = useMemo(() => databaseEntries(granted).filter((entry) => entry.kind !== "group"), [granted]); const labels = useMemo(() => new Map(databases.map((entry) => [entry.key, entry.label])), [databases]); + const disclosureFor = useCallback((database: string, answered: boolean): RoutingDisclosure | null => { + if (!labels.has(database)) return null; + return { database, label: labels.get(database)!, answered, + alternatives: databases.filter((entry) => entry.key !== database) + .map((entry) => ({ database: entry.key, label: entry.label })) }; + }, [databases, labels]); /** * ⭐⭐ INSTRUCTION 12 / R11 — NO SOURCE PICKER AT ALL. Owner: *"the Assistant should always have * access to all database that the user have access to. So it doesn't need the database clicks @@ -709,8 +745,6 @@ export default function AssistantPage({ granted }: AssistantPageProps) { return; } setActiveThread(result.value.thread.id); - if (routed) setRoutingByMessage((current) => ({ ...current, - [result.value.message.id]: { routed, prompt: text } })); if (result.value.view && governed.status === "answered") { setArtifactByView((current) => ({ ...current, [result.value.view!.id]: governed.artifacts })); } @@ -805,12 +839,32 @@ export default function AssistantPage({ granted }: AssistantPageProps) { selectQuery(qid); }, [pickMode, selectQuery]); + /** + * The question Change re-asks for a past answer. + * + * ⛔ NOT "the nearest message above it". `_submit` stamps the question and the answer with ONE + * `createdAt`, and the index sorts on that key, so a turn's two rows arrive in whichever order + * the stored map held them: a thread whose answer sits ABOVE its question left Change with no + * question to re-ask and therefore unrendered. The shared timestamp IS the pairing, so it is + * what this matches on; the scan above is the fallback for a row that predates that shape. + */ + const questionFor = useCallback((position: number) => { + const row = messages[position]; + if (!row) return ""; + const turn = messages.find((item) => item.role === "user" && item.createdAt === row.createdAt); + if (turn) return turn.content; + for (let index = position - 1; index >= 0; index -= 1) { + if (messages[index].role === "user") return messages[index].content; + } + return ""; + }, [messages]); + const empty = messages.length === 0; const composer = (

{approvalLine ?

{approvalLine}

: null} {alignment ?
- switchDatabase(database, alignment.prompt)} />

{alignment.draft ? "Database fields" : "Clarify"}

@@ -848,7 +902,7 @@ export default function AssistantPage({ granted }: AssistantPageProps) { className="as-prompt" value={question} rows={1} - placeholder={target ? `Ask about ${labels.get(target) || target}…` : "Ask about your data…"} + placeholder={targetTouched ? `Ask about ${labels.get(target) || target}…` : "Ask about your data…"} aria-label="Ask the assistant" onChange={(event) => setQuestion(event.currentTarget.value)} onKeyDown={(event) => { @@ -874,22 +928,8 @@ export default function AssistantPage({ granted }: AssistantPageProps) { })} - {/* ⚠ ONLY WHEN THE ANSWER IS AMBIGUOUS. Every generated view names ONE database (R3); - with a single database reachable that database is not a question, so asking it would - be chrome for its own sake. This is now the ONLY database control on the surface. */} - {databases.length > 1 ? ( - - ) : null} + {/* R6 / R47: routing needs no pre-question picker; its override survives as Change + on each answer's disclosure line. */} + +

+ )} ))} - {/* ⭐⭐ W50-T02 (owner instruction 2, ruling R16) — "the user can click Collapse - all and the opposite (Expand all)". Inside the NON-windowed branch on - purpose: grouping is refused outright on a windowed table one branch up, so - a fold control there could only ever refuse, and a control that can only - refuse is the fake affordance this repo bans. - ⚠ And only once something IS grouped. Ungrouped there are no groups to fold, - and the same rule applies one level down. */} {/* ⭐⭐ W50-T04 (owner ruling R3) — "a Group Summary mode in the Group menu that collapses the grid to ONE row per group, sortable by any aggregate and - exportable". In the NON-windowed branch, for W50-T02's reason one block down: + exportable". In the NON-windowed branch, for W50-T02's R16 reason at the top + of this same branch, above the field list: grouping is refused outright on a windowed table, so a mode that reshapes a grouped grid could only ever refuse there. ⚠ `cg-check-row`, the checkbox twin of the radio rows above it - the two share @@ -1254,16 +1275,6 @@ export default function Toolbar({ Summary mode )} - {groupBy !== null && onCollapseAllGroups && onExpandAllGroups && ( -
- - -
- )} )} diff --git a/web/src/customer-grid/ViewSidebar.tsx b/web/src/customer-grid/ViewSidebar.tsx index f01149bdf3b5d26dd9bcd4feb34b2d49f9bfb22f..4b8ecd1587b04d1c21f6b23679904e32cca64b4e 100644 --- a/web/src/customer-grid/ViewSidebar.tsx +++ b/web/src/customer-grid/ViewSidebar.tsx @@ -10,6 +10,7 @@ import { useRailSelection } from "../ui/useRailSelection"; import type { ReactNode } from "react"; import type { KeyboardEvent as ReactKeyboardEvent } from "react"; import { AnchoredOverlay } from "./OverlaySurface"; +import FolderNameDialog, { FOLDER_NAME_MAX } from "../home/FolderNameDialog"; import { EXPORT_FORMATS, EXPORT_LABELS, exportSelectionKey } from "./export"; import type { ExportFormat } from "./export"; import type { @@ -46,6 +47,8 @@ import { FolderMark, LockMark, MenuLabel, ModeIcon, ToneModeIcon } from "./icons // the user has to read, and the frame already listens here for exactly that. import { TOAST_EVENT, WORKSPACE_STALE_EVENT, signal } from "../apiContract"; import { ITEM_DELETED_EVENT, openExistingItem, useItemDeletions } from "../starred/starredApi"; +// C6 / R52 shares only the address's folder stratum with chrome; dataPath.ts still owns grid paths. +import { readDataAddress } from "../shell/nav"; import type { DeletedItem } from "../starred/starredApi"; import { ROOT_FOLDER_ID } from "./folders"; import { @@ -753,12 +756,23 @@ function DataDatabaseNavigator({ parentOfDatabase(database), { kind: "database", key: database.key }, ]; + const levelsForAddress = (): DataNavigatorLevel[] => { + const database = databases.find((candidate) => candidate.key === activeDatabaseKey); + const folder = readDataAddress().folder; + if (folder && folders.some((candidate) => candidate.id === folder)) { + return [ + { kind: "root" }, + { kind: "folder", id: folder }, + ...(database ? [{ kind: "database" as const, key: database.key }] : []), + ]; + } + return database ? levelsForDatabase(database) : [{ kind: "root" }]; + }; const [dataPathState, setDataPathState] = useState<{ levels: DataNavigatorLevel[]; at: number; }>(() => { - const database = databases.find((candidate) => candidate.key === activeDatabaseKey); - const levels = database ? levelsForDatabase(database) : [{ kind: "root" } as const]; + const levels = levelsForAddress(); return { levels, at: levels.length - 1 }; }); const activeDataKey = useRef(activeDatabaseKey); @@ -767,10 +781,33 @@ function DataDatabaseNavigator({ // during render so the newly selected database can never commit under the previous source or // folder for one frame; an effect would run after that stale frame had already painted. activeDataKey.current = activeDatabaseKey; - const database = databases.find((candidate) => candidate.key === activeDatabaseKey); - const levels = database ? levelsForDatabase(database) : [{ kind: "root" } as const]; + const levels = levelsForAddress(); setDataPathState({ levels, at: levels.length - 1 }); } + const seenAddressFolder = useRef(undefined); + useEffect(() => { + const syncAddressFolder = () => { + const levels = levelsForAddress(); + const addressedFolder = levels.find((level) => level.kind === "folder")?.id; + seenAddressFolder.current = addressedFolder; + setDataPathState((current) => { + const standing = current.levels[current.at]; + const standingFolder = standing?.kind === "folder" ? standing.id + : standing?.kind === "database" + ? current.levels.slice(0, current.at).reverse().find((level) => level.kind === "folder")?.id + : undefined; + // A view-only hash change must not reset history when the rail is already in this folder. + if (addressedFolder === standingFolder) return current; + return { levels, at: levels.length - 1 }; + }); + }; + // Folder metadata may arrive after mount. Resolve it once without undoing later rail browsing. + const folder = readDataAddress().folder; + if (folder && folder !== seenAddressFolder.current + && folders.some((candidate) => candidate.id === folder)) syncAddressFolder(); + window.addEventListener("hashchange", syncAddressFolder); + return () => window.removeEventListener("hashchange", syncAddressFolder); + }, [activeDatabaseKey, databases, folders]); const dataLevel = dataPathState.levels[dataPathState.at] ?? { kind: "root" as const }; const canDataBack = dataPathState.at > 0; /** W46-T08: the levels array was ALREADY an index into history and `pushDataLevel` already @@ -1110,7 +1147,7 @@ function DataDatabaseNavigator({ className="cg-input" autoFocus value={folderName} - maxLength={40} + maxLength={FOLDER_NAME_MAX} aria-label="Database folder name" onChange={(event) => setFolderName(event.target.value)} /> @@ -1563,6 +1600,7 @@ export default function ViewSidebar({ const [folderMenu, setFolderMenu] = useState<{ id: string; anchor: HTMLElement } | null>(null); const [renamingFolder, setRenamingFolder] = useState(null); const [creatingFolder, setCreatingFolder] = useState(false); + const [folderAnchor, setFolderAnchor] = useState(null); /** * ⭐⭐ WAVE 41 · T33 (instruction 12, R1) — WHICH FOLDER THE DRAFT FOLDER GOES INSIDE. * @@ -1574,8 +1612,6 @@ export default function ViewSidebar({ * leave its parent behind for the next one. */ const [folderParent, setFolderParent] = useState(null); - /** The controlled folder-name draft is committed only by the explicit Create action. */ - const [folderName, setFolderName] = useState(""); const [confirmDelete, setConfirmDelete] = useState(null); const [addTarget, setAddTarget] = useState(null); /** The element the bulk-add pane hangs off. Anchoring to document.body @@ -2555,8 +2591,8 @@ export default function ViewSidebar({ }; const folderDest = folderDestOf(folderParent ?? atId); - const createFolder = () => { - const value = folderName.trim(); + const createFolder = (name: string) => { + const value = name.trim(); if (!value) return; /* * ⭐⭐ W47-T11 — A REFUSED NESTING IS SAID OUT LOUD AND NOTHING IS WRITTEN. @@ -2593,8 +2629,8 @@ export default function ViewSidebar({ */ if (into) setAt(into); onFolderCreate?.(value, into); - setFolderName(""); setCreatingFolder(false); + setFolderAnchor(null); setFolderParent(null); }; @@ -2606,8 +2642,7 @@ export default function ViewSidebar({ * places, one beside the rail and one inside it, so step 2 read as an unrelated form. * One panel, two contents, one geometry. * - * The FOLDER door still closes it: that path is a single step and has no second panel - * to disagree with. Moving it too would be a change the ruling does not ask for. + * The folder door closes the menu and opens the shared naming popup at that same trigger. */ /** * ⭐ W46-T01 — IT TAKES A PARENT, exactly as its sibling `startFolder` already does, and the @@ -2630,6 +2665,7 @@ export default function ViewSidebar({ * nested folder is made by the control that already makes folders, not by a second form. */ const startFolder = (parent: string | null = null) => { + setFolderAnchor(createMenu ?? folderMenu?.anchor ?? null); setCreateMenu(null); setCreating(null); setFolderParent(parent); @@ -2934,60 +2970,21 @@ export default function ViewSidebar({ second step, inside the same `AnchoredOverlay` (below, next to the type list it follows). This comment stands in for the moved markup because the move IS the item: two steps of one action that used to appear in two different places. */} - {/* The folder form has one controlled name and one explicit commit action. */} {foldersOn && onFolderCreate && creatingFolder && ( -
- {/* ⭐ W41-T33 — the label carries the DESTINATION, because the pane is the same one - for both doors and a form that cannot say where it is about to put something is a - form the user has to remember which menu they came from. One line, the folder's - own name, no explanation of what nesting is (DESIGN.md §4). */} - - setFolderName(event.target.value)} - onKeyDown={(event) => { - if (event.key === "Enter") createFolder(); - if (event.key === "Escape") { - setCreatingFolder(false); - setFolderParent(null); - } - }} - /> -
- - -
-
+ folder.id === folderDest.into) + ? `New folder in ${folders.find((folder) => folder.id === folderDest.into)!.name}` + : "New folder"} + siblingNames={(folders ?? []).filter((folder) => + (nesting.parent[folder.id] ?? null) === folderDest.into).map((folder) => folder.name)} + onSubmit={createFolder} + onDismiss={() => { + setCreatingFolder(false); + setFolderAnchor(null); + setFolderParent(null); + }} + /> )} {/* I14 — the flyout. To the RIGHT (placement right-start), every creatable view type diff --git a/web/src/customer-grid/apiBridge.ts b/web/src/customer-grid/apiBridge.ts index 89f25614a2bf63d64ea4fd4c659771c8b8099c70..9992dfe8078c365fd42eff364a58cdb10dd8b84b 100644 --- a/web/src/customer-grid/apiBridge.ts +++ b/web/src/customer-grid/apiBridge.ts @@ -51,6 +51,8 @@ import { checkTenant, signal, } from "../apiContract"; +import { ROWS_CHUNK } from "./pipelineWorker"; +import type { RowsReply, RowsRequest } from "./pipelineWorker"; import { setStandaloneSink } from "./hostBridge"; import { changedBuckets } from "./liveWorkspace"; import type { ChangeTokens } from "./liveWorkspace"; @@ -163,7 +165,21 @@ function handledUnauthorized(status: number): boolean { * here keeps returning a cheerful 200 full of somebody else's data. `checkTenant` reads the * server's own stamp and reloads the frame; see `apiContract.ts::TENANT_HEADER`. */ -function handledSession(res: Response): boolean { +/** + * ⭐ W62-T18 — WIDENED FROM `Response` TO WHAT IT ACTUALLY READS, and no further. A `Response` + * still satisfies it, so every existing call site is unchanged; what it now also accepts is the + * `{status, headers}` shim `rowsThroughWorker` builds from the worker's reply, because a worker + * that did the fetch holds no `Response` for the main thread to hand over. `checkTenant` was + * already written to this structural type (`apiContract.ts:99`) — this brings its caller into + * line rather than inventing a second session check for the worker path, which is the one way + * a tenant swap could have been missed on the read that matters most. + */ +interface SessionFacts { + status: number; + headers: { get(name: string): string | null }; +} + +function handledSession(res: SessionFacts): boolean { if (checkTenant(res)) return true; return handledUnauthorized(res.status); } @@ -307,11 +323,175 @@ export function refusalMessage(body: unknown, status: number, fallback?: string) return fallback ?? `The server answered ${status}.`; } +/** + * ⭐⭐ W62-T18 / R51 — THE ROWS READ, OFF THE MAIN THREAD, IN BOUNDED PIECES. + * + * ⛔ THE MEASUREMENT IS THE WHOLE ARGUMENT, and it is committed beside this code in + * `AutoWiki/waves/wave62/proto/T18-measure.md`. On staging's `sale.order.line.agent` database + * (137,327 rows, 13 columns) the rows body is **69,944,226 bytes** (5.69 MB gzipped on the wire). + * `await res.json()` over it cost **731 ms** unthrottled and **2,916 ms** at 4x CPU throttling, + * and produced the only main-thread tasks over 200 ms in the whole open: 355 ms unthrottled, + * **1,523 ms and 1,384 ms** throttled. A CPU profile of the same open attributes 870.6 ms of self + * time to V8's `(program)` (the native parse) and 168.6 ms to the collector, against **22.6 ms + * for the largest application frame in the profile**. The freeze is this one line, not the grid. + * + * ⛔ AND IT IS NOT THE PIPELINE, WHICH IS WHERE THE TICKET POINTED FIRST. `runPipeline` over the + * same 137,327 rows costs 20-39 ms on open and 67-101 ms on a filter change; typing five + * characters into the search box produced five tasks, none over 102 ms. Moving THAT to a worker + * would have bought nothing and cost every feature R28 protects its synchronous access to the + * rows. R51 asked for the cost to be measured before it was fixed; this is what the measurement + * changed. + * + * ⛔ CHUNKED, BECAUSE ONE MESSAGE IS ONE TASK. The worker could hand the whole payload back in a + * single `postMessage` (measured: 472 ms of main-thread wall unthrottled, 1,314 ms at 4x), or + * columnar with a rebuild on this side (measured: a 256 ms rebuild at 4x — over budget on its + * own). Ten-thousand-row slices measured 208 ms total unthrottled with NO long task recorded at + * all, and a worst task of **70 ms** at 4x. The bound is the point: each slice is deserialised in + * its own task, so the main thread stays interruptible however far the table grows. + * ⚠ 2,000-row slices measured WORSE at both speeds (293 ms / 1,131 ms, the only long tasks the + * experiment recorded on any chunked leg) — per-message overhead and the garbage it makes. Do not + * "tune" this down without re-running the experiment in that file. + * + * ⭐ NO CAP, ANYWHERE (standing rule 1). Every row the server sends is accumulated and handed to + * the caller; the chunking is a delivery schedule, not a window. There is no paging, no "See + * more" and no second virtualiser — the grid is still glide's canvas. + * + * ⚠ ONE WORKER, REUSED, AND IT MAY REFUSE TO EXIST. Any environment without `Worker` (the gates + * execute parts of this client under node) or any construction failure falls back to the old + * main-thread read, which is exactly today's behaviour. `workerDead` latches so a browser that + * cannot build workers pays the attempt once rather than once per read. + */ +let rowsWorker: Worker | null = null; +let rowsWorkerDead = false; +let rowsRequestId = 0; + +function theRowsWorker(): Worker | null { + if (rowsWorkerDead) return null; + if (rowsWorker) return rowsWorker; + try { + if (typeof Worker === "undefined") { rowsWorkerDead = true; return null; } + rowsWorker = new Worker(new URL("./pipelineWorker.ts", import.meta.url), { type: "module" }); + // An error that reaches here killed the worker, not one read: drop it and let the next + // caller take the main-thread path rather than wait on a worker that cannot answer. + rowsWorker.addEventListener("error", () => { + rowsWorkerDead = true; + try { rowsWorker?.terminate(); } catch { /* already gone */ } + rowsWorker = null; + }); + return rowsWorker; + } catch { + rowsWorkerDead = true; + rowsWorker = null; + return null; + } +} + +/** The worker's header map as something `checkTenant`/`handledSession` can read. Case-insensitive, + * like the `Headers.get` it stands in for. */ +function headerReader(map: Record): SessionFacts["headers"] { + return { get: (name: string) => map[name.toLowerCase()] ?? null }; +} + +/** What `rowsThroughWorker` hands back: the same three outcomes the main-thread read has, kept + * apart so the caller raises exactly what it raises today. */ +type WorkerRead = + | { ok: true; payload: unknown; facts: SessionFacts } + | { ok: false; kind: "refused"; body: unknown; facts: SessionFacts } + | { ok: false; kind: "unavailable" }; + +/** + * ⚠ NO TIMEOUT HERE, AND THAT IS A DECISION RATHER THAN AN OVERSIGHT. A worker that dies loudly + * raises `error` (the listener above retires it) and a fetch that fails posts `failed`, so both + * realistic failures already fall back. What is left uncovered is a worker that hangs silently - + * and the main-thread read this replaces has no timeout either, so a timer here would not close a + * gap, it would only add a way to fire DURING a healthy read: this table's own rows request + * measured 10-48 s, so any bound short enough to be useful is short enough to re-request 66.7 MB + * that was already on its way. + */ +function rowsThroughWorker(url: string): Promise { + const worker = theRowsWorker(); + if (!worker) return Promise.resolve({ ok: false, kind: "unavailable" }); + const id = ++rowsRequestId; + return new Promise((resolve) => { + let head: Record | null = null; + let facts: SessionFacts | null = null; + /* ⭐ PRE-ALLOCATED FROM THE HEAD'S OWN `total`, and it is worth the extra field. Measured at 4x + CPU throttling on the 137,327-row table: growing this array with `push` cost 1,038 ms of + main-thread self time - the single largest application frame in the whole profiled open, + ahead of the pipeline (133 ms) and React's own commit - because a 137k-element array grows + by reallocating and copying about twenty times on the way up. Writing into a right-sized + array at a running offset pays none of that. The bound per slice is unchanged; what goes + away is the copying between slices. */ + let rows: unknown[] = []; + let filled = 0; + const finish = (result: WorkerRead): void => { + worker.removeEventListener("message", onMessage); + resolve(result); + }; + const onMessage = (event: MessageEvent): void => { + const reply = event.data; + // ⚠ ONE WORKER SERVES EVERY READER, so a reply that is not this request's is another + // caller's rows. Ignoring it here is what makes the shared worker safe. + if (!reply || reply.id !== id) return; + if (reply.op === "head") { + head = reply.head; + facts = { status: reply.status, headers: headerReader(reply.headers) }; + rows = new Array(reply.total); + filled = 0; + } else if (reply.op === "slice") { + // A loop, not `push(...slice)`: spreading 10,000 arguments is a stack overflow on some + // engines, and this path exists to be dependable on a big table. + // ⚠ `filled` rather than the array's length, because the array is already at its final + // length. A server that sent more rows than its own `total` still lands them all - the + // assignment grows the array the ordinary way - so this cannot drop a row. + for (let i = 0; i < reply.slice.length; i += 1) rows[filled + i] = reply.slice[i]; + filled += reply.slice.length; + } else if (reply.op === "refused") { + finish({ ok: false, kind: "refused", body: reply.body, + facts: { status: reply.status, headers: headerReader(reply.headers) } }); + } else if (reply.op === "failed") { + // The fetch never happened, or the body was unreadable. Fall back rather than raise: the + // main-thread read owns those two messages and must keep owning them. + finish({ ok: false, kind: "unavailable" }); + } else if (reply.op === "done") { + if (!head || !facts) finish({ ok: false, kind: "unavailable" }); + // A short count means a slice was lost between the two threads. Trimming keeps the array + // honest rather than handing the grid trailing holes that read as blank records. + else finish({ ok: true, payload: { ...head, rows: filled === rows.length ? rows : rows.slice(0, filled) }, facts }); + } + }; + worker.addEventListener("message", onMessage); + try { + worker.postMessage({ id, url, chunk: ROWS_CHUNK } satisfies RowsRequest); + } catch { + finish({ ok: false, kind: "unavailable" }); + } + }); +} + export async function fetchTopicRows(topic: TopicConfig): Promise { const cached = rowsCache.get(topic.rowsPath); if (cached && Date.now() - cached.at < CUSTOMERS_FRESH_MS) { return cached.payload; } + // ⭐ W62-T18 — the worker leg FIRST, the main-thread read below as the fallback. Both end in + // the same three outcomes and the same cache write; what differs is only which thread pays for + // `JSON.parse`. See `rowsThroughWorker` for the measurement that put it here. + const offThread = await rowsThroughWorker(`${API_V1}/${topic.rowsPath}`); + if (offThread.ok || offThread.kind === "refused") { + if (handledSession(offThread.facts)) return null; + if (!offThread.ok) { + signal(DATA_ERROR_EVENT, refusalMessage(offThread.body, offThread.facts.status)); + return null; + } + const carried = offThread.payload as CustomersPayload | null; + if (!carried || !Array.isArray(carried.rows) || !Array.isArray(carried.fields)) { + signal(DATA_ERROR_EVENT, "The server sent an unreadable payload."); + return null; + } + rowsCache.set(topic.rowsPath, { at: Date.now(), payload: carried }); + return carried; + } let res: Response; try { res = await fetch(`${API_V1}/${topic.rowsPath}`, { credentials: CREDENTIALS }); diff --git a/web/src/customer-grid/mapProjection.ts b/web/src/customer-grid/mapProjection.ts index 13bd9509705248b73e859ea66dbaae3176d3cf48..fccd48d34bd6cc326e93e39c26fc693c13c853af 100644 --- a/web/src/customer-grid/mapProjection.ts +++ b/web/src/customer-grid/mapProjection.ts @@ -895,10 +895,23 @@ export function normRect(ax: number, ay: number, bx: number, by: number) { }; } +/** The shared magnitude domain for bubbles and their size key (W62-T21, R24). */ +export function magnitudeDomain(min: number, max: number): { lo: number; hi: number } { + if (min >= 0) return { lo: min, hi: max }; + if (max <= 0) return { lo: -max, hi: -min }; + return { lo: 0, hi: Math.max(-min, max) }; +} + /** - * Bubble radius for a value under a sqrt scale (I5). AREA is proportional to - * the value, which is the only honest way to size a circle — radius-proportional + * Bubble radius for a value under a sqrt scale (I5). AREA is the honest scale: + * it grows linearly with magnitude — radius-proportional * bubbles overstate large values by the square, the classic bubble-chart lie. + * R24 requires one scale over magnitude; colour alone carries the sign, so + * equal positive and negative magnitudes draw the same radius. + * For a non-negative range, magnitude IS the value and the domain is unchanged: + * the same arithmetic preserves today's picture, without a separate size scale. + * Once max > min, hi > lo: the magnitude span is max - min for either one-sign + * range, and max(-min, max) > 0 for a range straddling zero. * `null`/non-finite gets `rNull`: a value-less row is drawn small, never hidden * and never faked (rule 8b). */ @@ -912,7 +925,8 @@ export function bubbleRadius( ): number { if (v == null || !Number.isFinite(v)) return rNull; if (!(max > min)) return (rMin + rMax) / 2; - const t = Math.max(0, Math.min(1, (v - min) / (max - min))); + const { lo, hi } = magnitudeDomain(min, max); + const t = Math.max(0, Math.min(1, (Math.abs(v) - lo) / (hi - lo))); return Math.sqrt(rMin * rMin + t * (rMax * rMax - rMin * rMin)); } diff --git a/web/src/customer-grid/pipelineWorker.ts b/web/src/customer-grid/pipelineWorker.ts new file mode 100644 index 0000000000000000000000000000000000000000..466d2dd09a6c009b94d06f489ea56623e0a42ad2 --- /dev/null +++ b/web/src/customer-grid/pipelineWorker.ts @@ -0,0 +1,155 @@ +// --------------------------------------------------------------------------- +// customer-grid / pipelineWorker.ts +// THE ROWS WORKER: fetch one rows payload and PARSE it off the main thread, +// then hand the rows back in bounded chunks. +// +// ⭐⭐ W62-T18 / R51 — WHY THIS FILE PARSES INSTEAD OF RUNNING THE PIPELINE. +// The ticket's candidate fix was "move the pipeline to a Web Worker". R51 said +// MEASURE FIRST, and the measurement (`AutoWiki/waves/wave62/proto/T18-measure.md`) +// refused that candidate: over the real 137,327-row `sale.order.line.agent` +// payload, `useVisibleRows.runPipeline` costs 20-39 ms on open and 67-101 ms on a +// filter change, while `Response.json()` over the 69,944,226-byte body costs +// 707-731 ms and is what produced BOTH of the >200 ms main-thread tasks in the +// before trace. The pipeline was never the freeze; the parse was. So the worker +// this file names takes the PARSE, and the pipeline stays exactly where it is, +// synchronous and on the main thread, where every feature R28 protects can keep +// reading it. +// +// ⛔ AND IT IS NOT A SECOND VIRTUALISER. Nothing here decides what is painted: +// every row this worker reads is handed to the caller, and the grid is still +// glide's canvas (F-CORR-1). There is no cap, no window and no paging in this +// file - standing rule 1 holds on the wire and holds here. +// +// ⛔ CHUNKED BECAUSE ONE MESSAGE IS ONE TASK. Handing 137,327 rows back in a +// single `postMessage` measured 472 ms of main-thread work; handing them back in +// 10,000-row slices measured 208 ms across FOURTEEN tasks, none of which the long +// task observer recorded at all. The bound is what matters, not the total: a +// chunk is deserialised in its own task, so the main thread stays interruptible +// however large the table grows. +// --------------------------------------------------------------------------- + +/** Rows per `slice` message. Measured: 5,000 / 10,000 / 20,000 all produced ZERO + * long tasks unthrottled; 10,000 was the cheapest overall (208 ms against 216 and + * 226). Smaller is not safer - 2,000 measured WORSE (265 ms, and the one long task + * the whole experiment saw), because per-message overhead starts to dominate. */ +export const ROWS_CHUNK = 10000; + +/** What the main thread asks for. `id` pairs a reply with its request, so one + * worker can serve concurrent readers without interleaving their rows. */ +export interface RowsRequest { + id: number; + url: string; + chunk?: number; +} + +/** The envelope minus its rows, plus what the caller needs to reproduce today's + * session and refusal handling without holding a `Response`. */ +export interface RowsHead { + id: number; + op: "head"; + head: Record; + total: number; + status: number; + headers: Record; +} + +export interface RowsSlice { id: number; op: "slice"; slice: unknown[] } +export interface RowsDone { id: number; op: "done" } +/** A non-2xx answer. The body rides along already parsed - a refusal body is + * small, and `refusalMessage` must see the same object it sees today. */ +export interface RowsRefused { + id: number; + op: "refused"; + status: number; + headers: Record; + body: unknown; +} +/** The fetch itself never happened (offline, DNS, abort). Distinct from a + * refusal, because the two raise different things on the main thread. */ +export interface RowsFailed { id: number; op: "failed"; why: string } + +export type RowsReply = RowsHead | RowsSlice | RowsDone | RowsRefused | RowsFailed; + +/** Headers as a plain object the structured clone can carry. Lower-cased keys, + * so the `get` shim on the other side can be case-insensitive the way + * `Headers.get` is. */ +function headerMap(res: Response): Record { + const out: Record = {}; + try { + res.headers.forEach((value, key) => { out[key.toLowerCase()] = value; }); + } catch { + /* a Headers object that refuses to iterate is not worth failing the read over */ + } + return out; +} + +async function serve(req: RowsRequest): Promise { + const chunk = req.chunk && req.chunk > 0 ? req.chunk : ROWS_CHUNK; + let res: Response; + try { + // ⚠ The literal rather than `apiContract.CREDENTIALS`, deliberately: importing that module + // here would pull the app's event bus and its `window` reads into the worker bundle, where + // there is no window. It is the same value ("same-origin") and `verify_api.py` is not the + // gate that holds it - if that constant ever changes, this line is the second site. + res = await fetch(req.url, { credentials: "same-origin" }); + } catch (err) { + post({ id: req.id, op: "failed", why: String(err) }); + return; + } + const headers = headerMap(res); + if (!res.ok) { + let body: unknown = null; + try { body = await res.json(); } catch { body = null; } + post({ id: req.id, op: "refused", status: res.status, headers, body }); + return; + } + let payload: Record | null = null; + try { + payload = (await res.json()) as Record | null; + } catch (err) { + post({ id: req.id, op: "failed", why: `unreadable payload: ${String(err)}` }); + return; + } + if (!payload || typeof payload !== "object") { + post({ id: req.id, op: "failed", why: "the payload was not an object" }); + return; + } + const rows = Array.isArray(payload.rows) ? (payload.rows as unknown[]) : null; + if (!rows) { + // Not a rows envelope. Hand the whole thing over as the head so the caller's + // own shape check produces the message it produces today, rather than this + // worker inventing a second vocabulary for the same failure. + post({ id: req.id, op: "head", head: payload, total: 0, status: res.status, headers }); + post({ id: req.id, op: "done" }); + return; + } + const head: Record = {}; + for (const key of Object.keys(payload)) if (key !== "rows") head[key] = payload[key]; + post({ id: req.id, op: "head", head, total: rows.length, status: res.status, headers }); + for (let i = 0; i < rows.length; i += chunk) post({ id: req.id, op: "slice", slice: rows.slice(i, i + chunk) }); + post({ id: req.id, op: "done" }); +} + +function post(reply: RowsReply): void { + (self as unknown as { postMessage(m: RowsReply): void }).postMessage(reply); +} + +/** + * ⛔ THE HANDLER INSTALLS IN A WORKER AND NOWHERE ELSE, and the guard is load-bearing rather than + * defensive. `apiBridge.ts` imports `ROWS_CHUNK` and the message types from this same module so + * the two sides cannot drift, which means this file is ALSO evaluated on the main thread — where + * an unguarded `self.onmessage = ...` would install a handler on `window` and swallow every + * `postMessage` the app makes to itself. A worker has `self` and no `window`; the main thread has + * both; node (where the gates execute parts of this client) has neither. + */ +const IN_WORKER = + typeof self !== "undefined" && + typeof (globalThis as { window?: unknown }).window === "undefined"; + +if (IN_WORKER) { + self.onmessage = (event: MessageEvent) => { + const req = event.data; + if (!req || typeof req.url !== "string") return; + void serve(req); + }; +} diff --git a/web/src/home/FolderNameDialog.tsx b/web/src/home/FolderNameDialog.tsx new file mode 100644 index 0000000000000000000000000000000000000000..ded44129ec1021e75d0fa141e2d1e587b26355e1 --- /dev/null +++ b/web/src/home/FolderNameDialog.tsx @@ -0,0 +1,65 @@ +import { useEffect, useState } from "react"; +import { AnchoredOverlay } from "../customer-grid/OverlaySurface"; + +export const FOLDER_NAME_MAX = 40; + +interface FolderNameDialogProps { + anchor: HTMLElement | null; + title: string; + initialName?: string; + siblingNames: readonly string[]; + onSubmit(name: string): void; + onDismiss(): void; + disabled?: boolean; + disabledReason?: string; +} + +export default function FolderNameDialog({ + anchor, title, initialName = "", siblingNames, onSubmit, onDismiss, + disabled = false, disabledReason, +}: FolderNameDialogProps) { + const [name, setName] = useState(() => initialName.slice(0, FOLDER_NAME_MAX)); + // autoFocus can precede the overlay's opener capture. Restore the supplied trigger + // after removal too, when the captured input is no longer connected. + useEffect(() => () => { + if (anchor?.isConnected) anchor.focus({ preventScroll: true }); + }, [anchor]); + const clean = name.trim(); + const duplicate = siblingNames.some((sibling) => sibling.trim().toLowerCase() === clean.toLowerCase()); + const disabledStyle = disabled ? { opacity: 0.45, cursor: "not-allowed" } : undefined; + + return ( + +
{ + event.preventDefault(); + if (disabled || !clean || duplicate) return; + onSubmit(clean); + }}> + setName(event.currentTarget.value.slice(0, FOLDER_NAME_MAX))} + /> + {duplicate ?

That folder name is already taken. Pick another one.

: null} + +
+
+ ); +} diff --git a/web/src/home/HomePage.tsx b/web/src/home/HomePage.tsx index 643782c58d2787dd0d735992ed1d6d273ff3e7b0..d13cda940756b0649d95269e9c7f63a172918e78 100644 --- a/web/src/home/HomePage.tsx +++ b/web/src/home/HomePage.tsx @@ -28,6 +28,7 @@ import { SHEET_SHAPE } from "../customer-grid/iconShapes"; import { appPathHash } from "../customer-grid/dataPath"; import { BodyPortal, useOverlayLayer } from "../customer-grid/OverlaySurface"; import DatabaseTile from "./DatabaseTile"; +import FolderNameDialog from "./FolderNameDialog"; // R16: Home and Data need the hollow glyph's box without loading the grid chunk. import "../styles/wave50/rail.css"; import "../styles/wave57/home-app.css"; @@ -36,10 +37,9 @@ import "../styles/wave61/home.css"; import "../styles/wave46/rail-select.css"; import "../styles/wave61/rails.css"; import { AGENTS_MODULE_LABEL } from "../inbox/inboxModel"; -import { Mark } from "../shell/Brand"; // AgentIcon carries the rail's shell-nav-icon class, which supplies its stroke-only paint. import { AgentIcon } from "../shell/icons"; -import { agentAddressHash, MAX_NAV_FOLDERS } from "../shell/nav"; +import { agentAddressHash, dataAddressHash, readDataAddress, MAX_NAV_FOLDERS } from "../shell/nav"; import type { DatabaseEntry, NavFolder, Recent } from "../shell/nav"; import { API_V1, CREDENTIALS } from "../apiContract"; // ⭐ WAVE 35 · W35-T15 (R4, C2) — ONE STAR CONTROL, drawn in the module that owns the concept and @@ -69,6 +69,21 @@ import { } from "./homeModel"; import type { AutomationTile, HomeFilter, HomeLayout, ModuleFolderIndex } from "./homeModel"; +// Twin in SheetPage.tsx; this ticket's file fence keeps the timeout local to each chunk. +const FOLDER_WRITE_TIMEOUT_MS = 20000; + +function homeFolderHref(href: string, folder: string | undefined): string { + if (!folder || folder === "__root__") return href; + if (href.startsWith("#/data?") && readDataAddress(href).database) { + const address = dataAddressHash({ ...readDataAddress(href), folder }); + // Keep server-supplied parameters that the shared address contract does not own. + const extras = new URLSearchParams(href.slice(href.indexOf("?") + 1)); + for (const key of ["database", "view", "folder"]) extras.delete(key); + return address + (extras.size ? "&" + extras.toString() : ""); + } + return href + (href.includes("?") ? "&folder=" : "?folder=") + encodeURIComponent(folder); +} + // ── the four quick-start marks ─────────────────────────────────────────────────────────────── // // Drawn here, in the same 16x16 stroke vocabulary as the rail's `DbIcon`/`AutoIcon`/`BellIcon` @@ -284,16 +299,18 @@ function QuickCard({ detail, onClick, disabled = false, + buttonTitle, }: { icon: React.ReactNode; title: string; /** ONE line. R13: the app does not narrate itself, and this is a passing-through surface. */ detail: string; - onClick: () => void; + onClick: (event: MouseEvent) => void; disabled?: boolean; + buttonTitle?: string; }) { return ( - + buttonTitle={mode === "data" && folders.length >= MAX_NAV_FOLDERS ? "Folder limit reached" : undefined} + onClick={(event) => { + setFolderAnchor(event.currentTarget); + setFolderEdit({ id: null, name: "" }); + }} + /> ) :