Skip to content

feat(mobile): native shell mode — useNativeHost, typed bridge calls, and the Console wired to the mobile shell - #29140

Merged
objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-88oszd
Nov 2, 2026
Merged

objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-88oszd

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by Steedos · project thread

Fixes #29039

Clause-②: yes (widening) — @object-ui/mobile is published and gains new exports; the seat writes the TIER_DEFAULT contract review.
Metadata-protocol: none

维护者速读

  • 改了什么:Console 现在能识别自己被 ObjectStack 移动端外壳托管(window.ObjectStackNative)。@object-ui/mobile 新增 useNativeHost()、桥的类型与五个不抛错的调用封装;Console 在路由层向外壳发送 ready()(每次页面加载一次,首个路由渲染之后)与 routeChanged(route)(每次 SPA 导航一次),并接住外壳的 objectstack-native:navigate 事件用自己的路由跳转、不再整页重载。
  • 为什么改:移动端 2.0 是包着 Console 的原生外壳,此前 Console 不知道自己被托管:外壳只能等 WebView 的 load 事件才撤加载遮罩,通知点击会整页重载 Console。
  • 风险与回滚:低。没有这个全局对象时,监听器不安装、事件不发送、页面与今天完全一致(有测试与真实浏览器读数两面钉住)。外壳模式下唯一的界面差异是不再注入 PWA 安装 manifest。安全区内边距按席位裁决不加:外壳已经自己给 WebView 加了内边距,再加会重复。回滚即 revert 本 PR。
  • 先看:① ui/packages/mobile/src/useNativeHost.ts(延迟检测的复查点、路由接线、导航事件只接受站内路径);② ui/packages/mobile/src/nativeHost.ts(桥类型、has() 能力检测、NativeCallResult);③ ui/apps/console/src/App.tsx 的 NativeShellRouting。
  • 两分钟验证:pnpm --dir ui exec vitest run packages/mobile/src/__tests__/nativeHost.test.tsx apps/console/src/__tests__/App.nativeShellMode-29039.test.tsx,预期 44 个测试全绿(外壳模式 ready 只发一次、导航事件被接住、没有全局时一切照旧)。
  • 需要你做的:无,席位落地。

What changes

The ObjectStack mobile shell (objectstack-ai/mobile#144) hosts this Console in a WebView and injects window.ObjectStackNative. The bridge contract is the mobile repository's docs/HYBRID-SHELL.md, section "Native bridge" (read at mobile PR 144 head 443207c).

@object-ui/mobile (new exports only):

  • ObjectStackNative types exactly the contract's members: version, platform, appVersion, capabilities, safeArea, has(capability), the calls getPushToken, haptic, openExternal, navigate, resetServer (each a Promise), and the events ready() and routeChanged(route). Nothing else is typed. Supporting types: NativeHapticKind, NativePushToken, NativeSafeArea, NativeHostMethod, NativeCallResult.
  • getNativeHost() reads the global at call time and returns it, or null. useNativeHost() is the React form.
  • Lazy detection (the re-check mechanism the brief asks the PR to name). useNativeHost() reads the global at render time (useSyncExternalStore), re-reads it once on mount, and, only while no bridge is present, re-checks at DOMContentLoaded (if the document is still loading), at the window load event, and at 100 ms, 500 ms and 2000 ms after load. The timed re-checks exist because the shell guarantees the bridge on Android only by evaluating its bootstrap again after the page has loaded (react-native-webview injectedJavaScript), which can land just after load fires. A bridge that appears at any of those points re-renders every caller with it. Without the global the hook returns null and nothing else happens.
  • Typed wrappers that feature-detect and never throw: nativeGetPushToken(), nativeHaptic(kind), nativeOpenExternal(url), nativeNavigate(route), nativeResetServer(). Each checks has(method) (capability names equal method names in the contract) and that the method exists, then calls it. Each resolves a NativeCallResult: { status: 'unavailable' } (no shell, no capability, no method, or a has() that throws), { status: 'ok', value }, or { status: 'failed', code, message } when the shell rejects the call (its error code: unknown_method, bad_args, failed). The wrappers carry a native prefix so the package entry does not export a bare navigate.
  • Events. postNativeReady() posts at most once per page load. postNativeRouteChanged(route) does not post a route equal to the one it posted last.
  • useNativeHostRouting({ route, navigate }) is the router wiring: ready() after the first commit (or as soon as a late bridge appears), routeChanged once per route change, and the navigate-event listener below. Outside the shell it installs nothing.

Console wiring (ui/apps/console/src/App.tsx, main.tsx):

  • NativeShellRouting renders beside the Routes element inside ConsoleShell (so inside its Suspense boundary) under the BrowserRouter. Its first effect therefore runs once the first route has committed, and that is when ready() is posted, not at boot. The route it posts is the router location's pathname + search + hash, which react-router already gives without the basename; the shell's own routeFromConsoleUrl keeps query and fragment too, so the route the shell recovers to is the full one.
  • Maintainer's scope addition (comment on the card): in shell mode the Console listens for the cancelable window event objectstack-native:navigate, moves its own router to detail.route, and calls preventDefault(), so a notification tap does not reload the bundle. Per the seat's ruling, preventDefault() is called only after the route is accepted, and only an in-app path is accepted: a string that starts with exactly one /, with no backslash and no control character (//host, https:, / + backslash + host, a relative path, an empty string or a non-string are left alone, so the shell's location.assign fallback runs). The navigation posts routeChanged once, through the same route-change effect. Outside shell mode the listener is not installed.
  • In shell mode the root element carries data-os-native-shell="ios|android", so a stylesheet can tell shell mode apart. Nothing reads it yet.

Shell-mode chrome (each element hidden, with the reason):

  • The PWA install manifest. In shell mode main.tsx removes the static link rel=manifest from index.html and does not inject the branded blob manifest. The app is installed natively there, so an install manifest has nothing to offer. Read after the boot round-trips; a bridge that lands even later only leaves the manifest in place, which a WebView never acts on.
  • Nothing else. The Console registers no service worker and draws no install prompt, so neither fires in the shell. At the shell's phone width the Console already draws its phone layout, which hides the desktop-only chrome (for example the help menu and the desktop search trigger are hidden md:flex). No new layout.

Safe area: a measured departure from the card's Ask 2 ("pads with the --os-native-safe-* variables"), ruled by the seat. The shell already pads its WebView: components/shell/ConsoleWebView.tsx at mobile 443207c wraps the WebView in a View with paddingTop: insets.top, paddingBottom: insets.bottom, unconditionally. Padding again in the Console would double the top and bottom insets. So the shell stays the single safe-area owner and the Console adds no padding in shell mode. The insets stay readable (--os-native-safe-*, useNativeHost()?.safeArea) for a later surface that opts in.

No spec change, no metadata key, no new gate. ui/packages/app-shell/src/layout/** is not touched.

Tests

  • New ui/packages/mobile/src/__tests__/nativeHost.test.tsx (40 tests): detection (absent, present, non-object global); late injection after load (timed re-check), at DOMContentLoaded, at load, and the control that stays null through every re-check; every wrapper resolves unavailable without a bridge, when has() says no (and the method is never called), and when the method is missing; ok with the value and the forwarded argument; failed with the shell's code; a throwing has(); ready once per page load; routeChanged once per route; useNativeHostRouting under StrictMode (one ready, one post per route), a late bridge receiving ready and the current route, the navigate event (in-app route cancelled and routed; six non-app shapes left alone), no listener outside shell mode, listener removed on unmount.
  • New ui/apps/console/src/__tests__/App.nativeShellMode-29039.test.tsx (4 tests), the real App under its real BrowserRouter with the pages stubbed as in App.docsPortalLazy.test.tsx: with a fake bridge installed before render, ready arrives once and only after the first route's page is on screen, then routeChanged('/login'), and the root carries data-os-native-shell; the navigate event moves the router by a history push (no document load) and posts the new route once; a //host route is left to the shell; control without the global: nothing posted, nothing marked, the event not cancelled.

Ablations (each through scripts/ablation-replace.mjs, mutation landed by anchor count and blob hash, file restored byte-identical by hash, under os-verify-lock), run on the 44 tests above:

  • late-injection watch disabled (watchForLateInjection returns at once): 4 red, the three late-injection cases and the late-bridge routing case;
  • the readyPosted once-guard removed: 2 red;
  • event.preventDefault() removed from the navigate listener: 2 red (mobile and console);
  • the wrappers' has() + method check reduced to "bridge present": 11 red.

Acceptance notes

  • Real-browser reading (Chromium via Playwright, 390 × 844, the Console's Vite dev server with no backend, so the login route shows "Cannot connect to server"; the bridge installed by an init script before any page script):
    • shell: __events = ready@/login, route:/login (ready posted once, after 19 elements had rendered under the root); root attribute data-os-native-shell="ios"; no manifest link; body padding 0px. Dispatching objectstack-native:navigate with /forgot-password: defaultPrevented true, the page moved to /forgot-password with the same boot id (no reload), and route:/forgot-password was posted once.
    • browser (no global): no events, no attribute, the branded blob manifest present, the navigate event not cancelled and the page stayed on /login.
    • late injection: a bridge installed 50 ms after load (the dev server's load fired at about 7.4 s) still received ready and route:/login, and the attribute was set.
  • Finding for the seat (doc mismatch, not filed here): mobile docs/HYBRID-SHELL.md "Console-side integration" says the Console should "pad with the safe-area variables", while the shell pads its WebView unconditionally (ConsoleWebView.tsx at 443207c). One of the two has to change before a Console that pads could ship; this PR follows the shell's behaviour.
  • Noted, not changed: an iPad (or any shell WebView 768 px wide or more) gets the Console's desktop chrome, because the phone layout follows the viewport width. The ruling keeps the phone layout as is and adds no layout; a per-flow change at that width belongs to Console at phone width: audit and fix the key flows at 375px (the mobile app now hosts the Console) #29040's children.
  • Noted, not changed: the contract also lists "register the push token after sign-in and unregister on sign-out". That needs a server device registry and is not in this card's Ask; nativeGetPushToken() is ready for it.

Verification

All at head 1205cf10 (this branch with origin/main 5d3667e2 merged in; the merge brought packages/spec contract files and a @object-ui/components data-table change, nothing under ui/packages/mobile or the console files touched here), after rebuilding the console's dependency closure:

  • pnpm exec vitest run packages/mobile/ plus every apps/console/src/__tests__/App.*.test.tsx and the two other console suites that render App (i18nAfterSignIn-12034, internalFormShell), from ui/, --maxWorkers=2, under os-verify-lock: 14 / 14 files, 113 / 113 tests.
  • pnpm --dir ui exec vitest run scripts/: 169 files passed, 2 skipped; 4900 tests passed, 2 skipped.
  • turbo run type-check --filter=@object-ui/mobile --filter=@object-ui/console: 72 / 72 tasks, exit 0. eslint on every touched source and test file: 0 problems. pnpm --dir ui run check:new-line-citations: 0 new citations.
  • Derived gates (node scripts/pm/dispatch-gates.mjs --commands, 34 commands, check-sdui-manifest among them): 34 / 34 exit 0, reconciled by --ran (34 derived, 34 run, 0 NOT-MEASURED).
  • check-changeset-no-major.mjs --base origin/main with this body as GITHUB_EVENT_PATH, and check-adr-0087-registration, check-closing-keyword-parity and check-partof-closing-keyword on this body: exit 0.
  • Changed lines: about 1,100 (well under 3,000).

Human Minimum

  • 1. Schemas start as Zod; packages/spec holds no runtime logic. (not touched)
  • 2. Every name I added uses the casing for its kind.
  • 3. No formatter ran; pnpm lint passes. (eslint on every touched file: 0 problems; the repo-wide run is CI's)
  • 4. One issue, one branch off an up-to-date main, one pull request.
  • 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset.
  • 6. packages/spec touched: not touched.
  • 7. No hand edits to content/docs/references/, packages/*/CHANGELOG.md or content/docs/releases/.
  • 8. Title, body and comments are in English.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QsR2jT77n7bRx8GRbrHKo7


Generated by Claude Code

@github-actions github-actions Bot added size/xl documentation Improvements or additions to documentation tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 2, 2026
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:ui#4 · zhuangjianguo · session_01QsR2jR7ZFkpqmm4U8S1f9y

Contract review

Served-tier: TIER_DEFAULT
Head-sha: 1205cf1091e55220191ecea0c3876e04377f0fe7
Local-runs: none

Face: declaration. The PR body and the changeset declare Clause-②: yes (widening). No published schema and no governed path are touched: check-governed-merges --pr 29140 hit 0 of 9 paths, and nothing is under packages/spec/src/**. So the review owed is the lane seat's TIER_DEFAULT record.

① Derived judgments

  • New exports only. @object-ui/mobile's entry gains:

    • useNativeHost and useNativeHostRouting;
    • getNativeHost and the constants NATIVE_HOST_GLOBAL and NATIVE_NAVIGATE_EVENT;
    • the five call wrappers, plus postNativeReady and postNativeRouteChanged;
    • the types ObjectStackNative, NativeCallResult, NativeHapticKind, NativeHostMethod, NativePushToken, NativeSafeArea and UseNativeHostRoutingOptions.

    No existing export changes shape, and the test seams (resetNativeHostWatchForTests, resetNativeHostEventsForTests) stay out of the entry. Correct, and a widening.

  • Lazy detection. useNativeHost is a useSyncExternalStore over getNativeHost(), which reads window.ObjectStackNative at call time. A late injection is picked up by a re-read on mount, at DOMContentLoaded and load, and at 100, 500 and 2000 ms after load. The watch is armed only while the bridge is absent. This meets the brief's "at use time, not at module load".

  • Calls never throw. callNative checks that the method exists and that has(method) === true, guards has itself with try/catch, and resolves unavailable, ok or failed with the rejection's code and message. Correct.

  • ready once, routeChanged once per change.

    • postNativeReady is guarded by a module-level flag, so a StrictMode double effect or a re-mount does not post twice.
    • postNativeRouteChanged skips a route equal to the last one posted, so an event-driven navigation posts once.
    • NativeShellRouting sits beside <Routes> inside ConsoleShell's Suspense boundary, so its first effect runs after the first route commits.
    • The route is the router's pathname + search + hash, without the basename.
  • objectstack-native:navigate (the maintainer's scope addition, comment on @object-ui/mobile: shell mode — detect window.ObjectStackNative and adapt the Console when the mobile shell hosts it #29039). The listener is installed only while a host is present. preventDefault() is called only after isInAppRoute accepts the route: a string starting with a single /, with no control characters and no backslash. Anything else is left alone, so the shell's location.assign fallback runs. This matches the seat's two rulings.

  • Shell mode, visible change. The PWA manifest is not injected and the static one is removed, and the root carries data-os-native-shell=<platform>. As ruled (B), there is no safe-area padding, because ConsoleWebView.tsx at mobile 443207c pads the WebView unconditionally. Without the global, the boot path draws the manifest exactly as before.

② Semver level

Both changeset entries are patch (@object-ui/mobile, @object-ui/console) with Metadata-protocol: none. AGENTS.md Post-Task Checklist step 3: a non-protocol change, widening or breaking, ships patch. Consistent.

③ Boundary flags

Implemented-by: claude/project-thread-88oszd
Reviewed-by: session_01QsR2jR7ZFkpqmm4U8S1f9y

VERDICT: PASS


Generated by Claude Code

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

domain:ui#4 · zhuangjianguo · session_01QsR2jR7ZFkpqmm4U8S1f9y

席位核对 · ACCEPT · 2026-11-02T08:43Z

Head-sha: 1205cf1091e55220191ecea0c3876e04377f0fe7

  • Shape: draft → main, Fixes #29039 in the body only. 9 files, +1068 / −22 (1090 lines ≤ 3000). No governed path: check-governed-merges --pr 29140 hit 0 of 9 paths. Author objectstack-fleet[bot]. Clause-②: yes (widening) and Metadata-protocol: none are line-initial in the body and the changeset, as claimed.
  • Contract review: the TIER_DEFAULT record is 6572291131 on this head, with a PASS (check-owed-reviews: DONE).
  • File surface: inside the claim: new nativeHost.ts and useNativeHost.ts, the index.ts exports and README.md in @object-ui/mobile, the console wiring in App.tsx and main.tsx, two test files, and the changeset. Nothing in app-shell/src/layout/** was needed.
  • Wanted, read in the diff:
  • Tests: 113 of 113 at this head (44 new). Four ablations each went red and were restored. Type-check for mobile and console passed 72 of 72. 34 of 34 derived gates exited 0, and the --ran reconciliation shows 0 NOT MEASURED. A real-browser Playwright run (dev server) showed ready once, navigation with no reload, a late bridge picked up, and the control unchanged.
  • Gates (check-runs on this head): 44 success, 11 skipped, 0 failed or pending. All ten required contexts succeeded or were skipped by their path filter.
  • Landing: ordinary queue landing, pr_ready then automerge_enable. Fixes #29039 closes the card on merge.

Generated by Claude Code

hotlong commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Consumer acceptance (mobile shell side)

Verdict: accepted, with follow-ups. No must-fix drift.

Checked against objectstack-ai/mobile main 8c6f13a. The shell's real buildBootstrapScript, createBridgeHandler, buildResolveScript and buildNavigateScript were run against this PR's merged nativeHost.ts, with a bridge reporting platform: 'desktop' and four of the five capabilities. All five wrappers round-trip, including bad_args and unavailable. ready is posted once, routeChanged carries {route}, and the navigate event is cancelled so location.assign never runs.

Contract parity, item by item:

  • global and event name;
  • methods and haptic kinds;
  • positional API → wire shapes;
  • push token {token, vendor};
  • err.code;
  • event payloads;
  • route format relative to the basename (every route the shell's safeConsoleRoute admits also passes isInAppRoute).

The safe area is not padded twice: the Console adds no padding, and the shell pads natively.

Follow-ups filed:


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/xl tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@object-ui/mobile: shell mode — detect window.ObjectStackNative and adapt the Console when the mobile shell hosts it

2 participants