Skip to content

KZN-19663 - only persist and restore viewer paths for bundle apps - #46

Open
keegandonley wants to merge 3 commits into
mainfrom
bug/KZN-19663-dev-viewer-last-path
Open

keegandonley wants to merge 3 commits into
mainfrom
bug/KZN-19663-dev-viewer-last-path

Conversation

@keegandonley

Copy link
Copy Markdown
Member

KZN-19663

The dev server reopened the viewer on "No app found with api_name embed." because .kizenapp/config.json had "lastPath": "/embed/sandbox".

Likely cause: a plugin floating frame that calls this.outputIframe(${this.location.origin}/embed/...) loads a second copy of the dev viewer inside the iframe, because in the dev viewer that origin is the viewer's own localhost. That nested copy treated embed as an app and saved its own path. The ticket author inferred this; it hasn't been reproduced end to end.

Changes

Persisting and restoring lastPath

  • shared/lib/lastPath.ts isAppPath: one rule for which paths count. A path matches /<apiName>/<tab>... only when apiName is an app in bundle.json. A bare /<app> is rejected.
  • Viewer (App.tsx): POSTs /api/last-path only when it is the top-level page (window.self === window.top) and on a known app route.
  • POST /api/last-path: ignores paths that aren't valid against outputDir/bundle.json, and still returns 200.
  • dev startup (resolveRestorableLastPath): drops an invalid lastPath and removes it from config.json, so a bad config fixes itself. The write is best-effort: a write failure never blocks startup.
  • Every Chrome launch (DevUI, including v): re-checks the saved path against the current bundle.json. An app removed by a rebuild now opens at /.

Viewer UI

  • One shared AppNotFound state for unknown app routes replaces the four per-page "No app found" cards. NavTabs are hidden for unknown apps.
  • A viewer loaded inside an iframe renders only its routed content (no header, tabs, sidebar or dialogs). At non-app paths it shows a short hint explaining that Kizen routes like /embed/... only exist in the Kizen app. The hint copy is a placeholder (marked in code).
  • A non-array bundle (/api/bundle serves {} when nothing is built) is treated as no apps. Before, it threw on /$apiName/... routes.
  • Sandbox floating frames now render in their own fixed inset-0 z-[15] layer:
    • Before, their BASE_Z (9000+) was trapped inside a wrapper with no z-index, so z-10 page elements painted over them. The routable-page slide-out panel's shadow showed as a faint vertical line across frames.
    • The trigger gutter keeps its original layer, so the header and NavTabs look as before.
    • Frames sit above page content and below toasts (z-20), tooltips, dialogs and dropdowns.
    • A frame dragged over the header or NavTabs now covers them.

Testing

  • pnpm typecheck, pnpm lint: clean.
  • pnpm test: 2185 passed (2157 before). New tests cover:
    • isAppPath
    • the /api/last-path route: /embed/... POSTs leave the ref and config.json untouched
    • the startup heal (unknown app, missing bundle, read-only config.json)
    • restorablePath reading the current bundle
  • The viewer has no test runner, so the iframe gate, not-found and hint rendering, and frame layering are covered only by typecheck, lint and the viewer build.
  • Not checked in a browser yet:
    • On plugin-kizen-ai, opening the chat frame doesn't change lastPath, and the frame shows the hint with no viewer chrome.
    • A hand-set "lastPath": "/embed/sandbox" opens at / and is removed from config.json on the next start.
    • The vertical line no longer shows over frames.

The dev viewer saved every pathname to lastPath, including paths from a
viewer copy nested in a plugin iframe (e.g. /embed/ai-chat), so the next
dev start reopened "No app found with api_name embed".

- shared isAppPath rule: a path counts only when its first segment is an
  app in bundle.json
- viewer saves its path only when top-level and on a known app route
- POST /api/last-path ignores paths that aren't bundle-app routes
- dev startup drops (and best-effort removes) an invalid lastPath; every
  Chrome launch re-checks the saved path against the current bundle
- one shared not-found state for unknown app routes
- framed viewer copies render without viewer chrome and show a hint at
  non-app paths
- sandbox floating frames get their own z-[15] layer so page content
  (e.g. the routable-page slide-out panel) no longer paints over them

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved issues can crash the viewer after a failed bundle refetch and discard a valid saved route when launching during a rebuild.

2 open issues
What changed in this PR

Restricts saved viewer paths to apps in the current bundle, preventing nested viewer instances from overwriting the route restored by appbuilder dev.

Changes:

  • Adds shared path validation, server-side persistence checks, startup cleanup, and validation before each Chrome launch.
  • Centralizes unknown-app states, simplifies embedded viewers, and raises floating frames above page content.
  • Adds tests for path validation, persistence, cleanup, and restoration.

The .copilot-docs/ files were unavailable, so the documentation setup workflow had not populated the plugin contract for verification.

File Description
viewer/​src/​pages/​SandboxPage.tsx Uses centralized not-found handling and separates frame bodies from the trigger layer.
viewer/​src/​pages/​ConfigurationPage.tsx Defers unknown-app rendering to the shared state.
viewer/​src/​pages/​CodeStepsPage.tsx Defers unknown-app rendering to the shared state.
viewer/​src/​pages/​AppDetailPage.tsx Defers unknown-app rendering to the shared state.
viewer/​src/​components/​EmbeddedRouteHint.tsx Adds an explanatory hint for embedded non-app routes.
viewer/​src/​components/​AppNotFound.tsx Adds the shared unknown-app message.
viewer/​src/​App.tsx Gates path persistence, centralizes route states, and hides embedded viewer chrome.
src/​ui/​DevUI.tsx Revalidates saved paths before launching Chrome.
src/​server/​requestHandler.ts Validates paths against the bundle before saving them.
src/​server/​requestHandler.test.ts Tests valid persistence and rejection of non-app paths.
src/​lib/​lastPath.ts Adds bundle lookup, restoration validation, and saved-path cleanup.
src/​lib/​lastPath.test.ts Tests restoration, bundle changes, and best-effort cleanup.
src/​commands/​dev.ts Resolves and cleans saved paths during startup.
shared/​lib/​lastPath.ts Defines shared path parsing and bundle-membership checks.
shared/​lib/​lastPath.test.ts Tests shared parsing and validation rules.

Review effort: Balanced

Comment thread src/ui/DevUI.tsx Resolved
Comment thread viewer/src/App.tsx Resolved
…the bundle query

- runBuild writes bundle.json via a same-directory temp file and rename, so
  readers (last-path route, launch check) never see a partial file
- the viewer bundle query normalizes /api/bundle's {} to [], and App's
  not-found / iframe gates key on data presence rather than isSuccess, so a
  failed refetch with retained data can't mount pages on a non-array bundle

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Saved paths are checked for bundle membership but not valid tabs or nested segments, allowing invalid routes to survive startup healing and reopen in the not-found view.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Validate saved paths against complete router routes

shared/​lib/​lastPath.ts:34

With demo_app in the bundle, this also accepts /demo_app/not-a-tab and /demo_app/sandbox/extra, neither of which matches a route in viewer/src/router.tsx. Such saved paths survive startup healing and are restored into the router's not-found view. Check the tab and permitted nested segments as well as bundle membership.

2 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The raw-prefix validation accepts traversal paths such as /demo_app/../embed/sandbox, which Chrome normalizes to an unknown-app route, causing startup failure.

1 open issue

Review effort: Balanced

Comment thread shared/lib/lastPath.ts Resolved
isAppPath now requires URL parsing to leave the path unchanged, so a path
like /demo_app/../embed/sandbox (or a %2e%2e, query, hash or // variant)
can't pass the app check and then open a different page once Chrome
normalizes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants