diff --git a/.github/workflows/performance-contracts.yml b/.github/workflows/performance-contracts.yml index 8af9f5342b2..cd3ba1dfa3a 100644 --- a/.github/workflows/performance-contracts.yml +++ b/.github/workflows/performance-contracts.yml @@ -12,22 +12,29 @@ on: - 'config/oxlint-performance-audit.json' - 'config/oxlint-plugins/*performance.mjs' - 'config/oxlint-plugins/quadratic-buffer-concat.mjs' - - 'config/scripts/*-plugin.test.mjs' - - '!config/scripts/static-analysis/oxlint-contracts/renderer-scrollbar-style-plugin.test.mjs' - # Keep in sync with the contract list in config/vitest.performance.config.ts; + - 'config/scripts/static-analysis/oxlint-contracts/app-store-performance-plugin.test.mjs' + - 'config/scripts/static-analysis/oxlint-contracts/quadratic-buffer-concat-plugin.test.mjs' + - '**/*.performance.test.ts' + - '**/*.performance.test.tsx' + # Contracts without the .performance suffix. Keep in sync with config/vitest.performance.config.ts; # without these a rename lands green and only breaks the next nightly. - - 'src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-terminal-writer-acquire.test.ts' + - 'src/main/runtime/session-tabs/session-tabs-phone-layout-moves.test.ts' - 'src/renderer/src/components/terminal-pane/pty-connection/startup/saved-resume-cancellation.test.ts' + - 'src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-terminal-writer-acquire.test.ts' + - 'src/shared/agent-launch/permissions/tui-agent-permission-choice.test.ts' + - 'src/main/runtime/rpc/methods/projects/repo.test.ts' + - 'src/renderer/src/components/tab-bar/use-new-tab-agent-menu-host-preferences.test.tsx' - 'src/main/sqlite/sync-database.test.ts' - 'src/main/runtime/orchestration/db/row-column-lists.test.ts' - - 'src/shared/native-chat/tools/native-chat-tool-pairing.performance.test.ts' - 'src/main/codex-usage/codex-rollout-file-lifetime.test.ts' - 'src/main/codex-usage/scanner-incremental-append.test.ts' - 'src/main/usage/jsonl-file-reader.test.ts' - 'src/main/usage/jsonl-file-snapshot.test.ts' - 'src/renderer/src/lib/pane-manager/pane-terminal-output-scheduler-queue-retention.test.ts' + - 'src/renderer/src/store/diagnostics/store-identity-churn-probe.test.ts' - 'src/main/claude-usage/scanner-incremental-append.test.ts' - 'src/main/claude-usage/store-checkpoint-persistence.test.ts' + - 'src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx' permissions: contents: read diff --git a/config/flaky-test-pattern-baseline.txt b/config/flaky-test-pattern-baseline.txt index be1d0d861f9..02a7704ea2a 100644 --- a/config/flaky-test-pattern-baseline.txt +++ b/config/flaky-test-pattern-baseline.txt @@ -620,7 +620,7 @@ 4 test-no-wall-clock:src/main/skills/runtime-upload/skill-upload-process-restart.integration.test.ts 4 test-no-wall-clock:src/main/sqlite/sync-database.test.ts 4 test-no-wall-clock:src/main/managed-server/activation/orcad-exited-own-fence-mutation-race.test.ts -8 test-no-wall-clock:src/main/managed-server/activation/orcad-exited-own-fence.test.ts +6 test-no-wall-clock:src/main/managed-server/activation/orcad-exited-own-fence.test.ts 4 test-no-wall-clock:src/main/managed-server/activation/orcad-exited-own-install-lock.test.ts 2 test-no-wall-clock:src/main/managed-server/activation/orcad-exited-own-lock-windows.test.ts 4 test-no-wall-clock:src/main/ssh/remote-exec/ssh-remote-commands.test.ts @@ -783,7 +783,6 @@ 1 test-timeout-needs-reason:src/main/skills/install/skill-bundle-process-termination.integration.test.ts 1 test-timeout-needs-reason:src/main/sqlite/sync-database-concurrent-backup.test.ts 2 test-timeout-needs-reason:src/main/ssh/connection/ssh-connection-sftp-wire.test.ts -1 test-timeout-needs-reason:src/main/managed-server/activation/orcad-exited-own-fence.test.ts 1 test-timeout-needs-reason:src/main/ssh/file-transfer/ssh-exec-stdin-file-transfer.test.ts 2 test-timeout-needs-reason:src/main/ssh/system-transport/forwarding/system-ssh-forward-process-real-openssh.test.ts 2 test-timeout-needs-reason:src/main/startup/single-instance-lock-exit.electron.test.ts diff --git a/config/main-sync-fs-baseline.txt b/config/main-sync-fs-baseline.txt new file mode 100644 index 00000000000..f64f48d6883 --- /dev/null +++ b/config/main-sync-fs-baseline.txt @@ -0,0 +1,617 @@ +# Synchronous node:fs calls (every *Sync export) in production src/main and src/shared, per file. +# Startup-only or one-shot uses are fine to keep; per-RPC, per-spawn, per-focus or per-poll uses are the target. +# This is a RATCHET: counts may only go DOWN. +# Prune after removing calls: pnpm check:main-sync-fs-ratchet --prune +1 src/main/agent-hooks/first-work/first-work-rename-runtime.ts +2 src/main/agent-hooks/installation/configuration/hook-config-write-path.ts +1 src/main/agent-hooks/installation/configuration/hooks-json-read.ts +11 src/main/agent-hooks/installation/configuration/hooks-json-write.ts +6 src/main/agent-hooks/installation/scripts/locked-executable-rename.ts +9 src/main/agent-hooks/installation/scripts/managed-hook-script-write.ts +1 src/main/agent-hooks/server/server-hydration.ts +1 src/main/agent-hooks/wsl-relay/wsl-hook-relay-deps.ts +3 src/main/agent-hooks/wsl-relay/wsl-hook-relay-launch.ts +1 src/main/agent-launch/trust/agent-trust-ownership-ledger.ts +14 src/main/agent-launch/trust/agent-trust-presets.ts +2 src/main/agent-launch/trust/antigravity-workspace-trust-ownership.ts +1 src/main/agent-launch/trust/execution-host-workspace-trust.ts +4 src/main/ai-vault/parse-cache/session-parse-cache-legacy-json.ts +2 src/main/ai-vault/scan-service/session-scanner-service-entry-path.ts +1 src/main/ai-vault/scan-service/session-scanner-service-spawn.ts +1 src/main/ai-vault/scanners/omp/omp-session-root.ts +1 src/main/ai-vault/scanners/opencode/reader-transport/opencode-wsl-runtime-preparation.ts +2 src/main/ai-vault/scanners/opencode/reader-transport/session-scanner-opencode-sqlite-worker-spawn.ts +8 src/main/amp/hook-service.ts +2 src/main/amp/managed-plugin-install-status.ts +1 src/main/antigravity/antigravity-host-acp.ts +1 src/main/antigravity/native-account-launch.ts +5 src/main/antigravity/native-account-store.ts +2 src/main/antigravity/native-credential-backend.ts +7 src/main/antigravity/native-file-credentials.ts +12 src/main/artifacts/artifact-create-intent-store.ts +2 src/main/artifacts/artifact-share-record-store.ts +1 src/main/automations/external-manager-discovery.ts +7 src/main/bitbucket/credential-store.ts +1 src/main/browser-passwords/browser-password-chromium-import.ts +2 src/main/browser-passwords/browser-password-vault-file.ts +2 src/main/browser/agent-browser/agent-browser-bridge-process.ts +2 src/main/browser/agent-browser/agent-browser-dependency-binary.ts +2 src/main/browser/agent-browser/agent-browser-process-environment.ts +1 src/main/browser/agent-browser/agent-browser-session-reset.ts +2 src/main/browser/client-hosts/file-transfers/browser-client-download-relay.ts +1 src/main/browser/client-hosts/file-transfers/browser-client-upload-staging.ts +2 src/main/browser/client-hosts/leases/browser-host-client-identity.ts +1 src/main/browser/cookies/browser-cookie-import-diagnostics.ts +2 src/main/browser/cookies/chromium/chromium-cookie-path.ts +4 src/main/browser/cookies/chromium/chromium-cookie-snapshot.ts +1 src/main/browser/cookies/chromium/import-cookies.ts +3 src/main/browser/cookies/client-route/client-route-cookie-import-source-store.ts +2 src/main/browser/cookies/sources/browser-cookie-detection.ts +8 src/main/browser/cookies/sources/browser-cookie-firefox-import.ts +2 src/main/browser/cookies/sources/browser-cookie-key.ts +1 src/main/browser/cookies/sources/browser-cookie-safari-import.ts +5 src/main/browser/cookies/sources/browser-cookie-source-profiles.ts +11 src/main/browser/cookies/staging/browser-session-cookie-staging.ts +1 src/main/browser/downloads/browser-download-destination.ts +2 src/main/browser/extensions/api-runtime/browser-extension-worker-shim-source.ts +8 src/main/browser/extensions/installation/browser-extension-install-store.ts +1 src/main/browser/extensions/installation/browser-extension-load-directory.ts +1 src/main/browser/extensions/profile-sessions/browser-extension-session-loader.ts +2 src/main/browser/extensions/profile-storage/browser-extension-data-removal.ts +8 src/main/browser/extensions/profile-storage/browser-extension-profile-document-store.ts +4 src/main/browser/extensions/profile-storage/browser-extension-profile-state-store.ts +3 src/main/browser/extensions/profile-storage/browser-extension-retired-profile-storage.ts +1 src/main/browser/guest-lifecycle/popups/browser-popup-background-cdp-probe.ts +2 src/main/browser/guest-lifecycle/popups/popup-identity-electron-probe.ts +1 src/main/browser/identity/browser-identity-mode-record.ts +2 src/main/browser/identity/browser-identity-mode-store.ts +2 src/main/browser/network/egress/browser-route-egress-electron-launch.ts +4 src/main/browser/network/execution-routes/wsl-browser-network-relay-launch.ts +5 src/main/browser/routing/partitions/browser-route-partition-binding-file.ts +3 src/main/browser/routing/partitions/browser-route-partition-binding-store.ts +4 src/main/browser/routing/partitions/browser-route-partition-pending-deletes.ts +4 src/main/browser/sessions/browser-session-meta-store.ts +2 src/main/browser/site-settings/browser-profile-site-store.ts +7 src/main/claude-accounts/claude-account-folder.ts +1 src/main/claude-accounts/claude-observed-config-dirs.ts +14 src/main/claude-accounts/claude-profile-history.ts +2 src/main/claude-accounts/claude-profile-homes.ts +1 src/main/claude-accounts/claude-profile-launch-preparation.ts +7 src/main/claude-accounts/claude-profile-paths.ts +26 src/main/claude-accounts/claude-profile-prompt-history.ts +5 src/main/claude-accounts/claude-profile-provisioning.ts +5 src/main/claude-accounts/claude-profile-router.ts +1 src/main/claude-accounts/claude-profile-settings.ts +1 src/main/claude-accounts/claude-profile-setup-worker.ts +1 src/main/claude-accounts/claude-profile-setup.ts +12 src/main/claude-accounts/claude-profile-sharing.ts +1 src/main/claude-accounts/claude-profile-wsl-router.ts +2 src/main/claude-accounts/keychain.ts +1 src/main/claude-accounts/runtime-paths.ts +3 src/main/claude/agent-view/agent-view-install.ts +1 src/main/claude/claude-diff-panel-auto-open.ts +14 src/main/claude/claude-folder-trust-file.ts +2 src/main/claude/claude-profile-hook-target.ts +1 src/main/claude/claude-rejected-api-key.ts +1 src/main/claude/claude-rewind-transcript.ts +1 src/main/claude/hook-service.ts +3 src/main/claude/statusline-install.ts +6 src/main/claude/windows-hook-files.ts +1 src/main/cli/appimage-copy-pre-extraction.ts +3 src/main/cli/appimage-extracted-root.ts +1 src/main/cli/appimage-extraction-pruning.ts +2 src/main/cli/appimage-generation-lease.ts +4 src/main/cli/appimage-generation-residency.ts +16 src/main/cli/appimage-stable-launcher.ts +1 src/main/cli/cli-command-inspection.ts +1 src/main/cli/cli-dev-launcher.ts +3 src/main/cli/cli-install-location.ts +2 src/main/cli/linux-bare-orca-dispatcher.ts +7 src/main/cli/linux-terminal-orca-cli-shim.ts +2 src/main/cli/wsl-managed-cli.ts +2 src/main/codex-accounts/codex-account-identity.ts +3 src/main/codex-accounts/codex-account-registration.ts +4 src/main/codex-accounts/codex-config-mirror.ts +1 src/main/codex-accounts/codex-credential-absence-grace.ts +1 src/main/codex-accounts/codex-login-auth-snapshot.ts +5 src/main/codex-accounts/codex-managed-home-lifecycle.ts +6 src/main/codex-accounts/codex-managed-home-path.ts +17 src/main/codex-accounts/fs-utils.ts +1 src/main/codex-accounts/guest-codex-account-homes.ts +6 src/main/codex-accounts/host-codex-managed-home-ownership.ts +2 src/main/codex-accounts/legacy-shared-auth-migration.ts +21 src/main/codex-accounts/legacy-wsl-runtime-auth-drain-recovery-script-harness.ts +36 src/main/codex-accounts/legacy-wsl-runtime-auth-drain-script-harness.ts +6 src/main/codex-accounts/legacy-wsl-runtime-auth-drain-script-interference-shims.ts +1 src/main/codex-accounts/managed-codex-auth-readiness.ts +7 src/main/codex-accounts/runtime-home-service-auth-core.ts +5 src/main/codex-accounts/runtime-home-service-auth-provenance.ts +23 src/main/codex-accounts/runtime-home-service-auth-sync.ts +19 src/main/codex-accounts/runtime-home-service-legacy-migration.ts +1 src/main/codex-accounts/runtime-home-service-managed-home.ts +14 src/main/codex-accounts/runtime-home-service-paths.ts +1 src/main/codex-accounts/runtime-home-service-sync.ts +2 src/main/codex-accounts/runtime-home-service-wsl-core.ts +1 src/main/codex-usage/codex-session-file-discovery.ts +1 src/main/codex/configuration/codex-path-observation.ts +21 src/main/codex/configuration/homes/codex-home-paths.ts +6 src/main/codex/configuration/homes/codex-managed-home-resource-copy-marker.ts +8 src/main/codex/configuration/homes/codex-managed-home-rules.ts +5 src/main/codex/configuration/homes/codex-pane-account-registry.ts +5 src/main/codex/configuration/homes/codex-windows-sandbox-read-grant.ts +1 src/main/codex/configuration/settings/codex-config-path-reference-rewrite.ts +1 src/main/codex/configuration/settings/config-settings-baseline.ts +5 src/main/codex/configuration/settings/config-settings-promotion-write-target.ts +3 src/main/codex/configuration/settings/config-settings-promotion.ts +1 src/main/codex/configuration/shared-server/codex-daemon-socket-path-guard.ts +8 src/main/codex/configuration/toml/config-toml-atomic-write.ts +4 src/main/codex/history/backfill/codex-session-backfill-marker.ts +17 src/main/codex/history/bridges/codex-session-bridge.ts +5 src/main/codex/history/index/codex-session-index-heal-state.ts +2 src/main/codex/history/index/codex-state-db.ts +2 src/main/codex/history/resume/codex-session-resume-home.ts +2 src/main/codex/history/rollouts/codex-account-home-discovery.ts +1 src/main/codex/history/rollouts/codex-session-file-listing.ts +2 src/main/codex/history/rollouts/codex-session-link.ts +1 src/main/codex/hooks/installation/codex-hook-local-install.ts +2 src/main/codex/hooks/installation/codex-hook-local-maintenance.ts +4 src/main/codex/hooks/reconciliation/codex-hook-legacy-cleanup.ts +2 src/main/codex/hooks/reconciliation/codex-hook-status.ts +4 src/main/codex/hooks/reconciliation/codex-real-home-hooks-json.ts +3 src/main/codex/trust/grants/codex-trust-grant-ledger.ts +2 src/main/codex/trust/hooks/codex-hook-trust-derivation.ts +1 src/main/codex/trust/hooks/codex-hook-trust-memo.ts +2 src/main/codex/trust/hooks/codex-hook-user-mirroring.ts +4 src/main/codex/trust/hooks/hook-trust-promotion.ts +1 src/main/codex/trust/identity/codex-trust-identity.ts +1 src/main/codex/trust/toml/config-toml-project-trust.ts +7 src/main/codex/trust/toml/config-toml-trust.ts +2 src/main/computer/desktop-script-provider-paths.ts +1 src/main/computer/macos-native-provider-client.ts +3 src/main/computer/macos-native-provider-paths.ts +5 src/main/computer/macos-native-provider-transport.ts +1 src/main/computer/sidecar-client.ts +3 src/main/connector-cloud/connector-turn-events.ts +1 src/main/copilot/hook-service.ts +4 src/main/crash-reporting/appimage-crashpad-handler-exit.ts +2 src/main/credentials/credential-file-protection.ts +6 src/main/credentials/encrypted-api-key-file-store.ts +10 src/main/credentials/integration-credential-file.ts +1 src/main/credentials/integration-state-file-cache.ts +1 src/main/credentials/integration-token-file-cache.ts +2 src/main/credentials/saved-credential-readability.ts +2 src/main/daemon/admission/automatic-work-run-marker-store.ts +5 src/main/daemon/admission/host-terminal-ledger.ts +1 src/main/daemon/admission/legacy-restore-records.ts +1 src/main/daemon/daemon-entry.ts +1 src/main/daemon/endpoint/identity/daemon-endpoint-incarnation.ts +3 src/main/daemon/endpoint/identity/daemon-pid-identity.ts +2 src/main/daemon/endpoint/identity/daemon-pid-record-quarantine.ts +2 src/main/daemon/endpoint/identity/daemon-process-start-time.ts +2 src/main/daemon/endpoint/identity/daemon-ready-identity.ts +3 src/main/daemon/endpoint/publication/daemon-endpoint-lifecycle.ts +7 src/main/daemon/endpoint/publication/daemon-endpoint-ownership.ts +1 src/main/daemon/endpoint/publication/daemon-endpoint-probe.ts +3 src/main/daemon/endpoint/publication/daemon-private-file-modes.ts +2 src/main/daemon/endpoint/publication/daemon-socket-endpoint-path.ts +3 src/main/daemon/executable-host/linux/daemon-appimage-generation-launch.ts +1 src/main/daemon/executable-host/linux/daemon-appimage-launch.ts +3 src/main/daemon/executable-host/linux/daemon-host-image.ts +2 src/main/daemon/executable-host/macos/macos-daemon-bundle.ts +9 src/main/daemon/executable-host/windows/daemon-host-build-inventory.ts +2 src/main/daemon/executable-host/windows/daemon-host-file-hash-cache.ts +3 src/main/daemon/executable-host/windows/daemon-host-manifest.ts +2 src/main/daemon/executable-host/windows/daemon-host-reclaim.ts +12 src/main/daemon/executable-host/windows/daemon-host-relocation.ts +4 src/main/daemon/health/daemon-health.ts +4 src/main/daemon/history/restoration/history-reader.ts +1 src/main/daemon/history/restoration/terminal-history-checkpoint-reader.ts +1 src/main/daemon/history/restoration/terminal-history-legacy-scrollback-restore.ts +1 src/main/daemon/history/storage/history-manager.ts +3 src/main/daemon/history/storage/terminal-history-metadata.ts +2 src/main/daemon/history/storage/terminal-history-permission-repair.ts +8 src/main/daemon/history/storage/terminal-history-recovery-quarantine.ts +1 src/main/daemon/history/storage/terminal-history-session-files.ts +3 src/main/daemon/history/storage/terminal-history-session-tombstone.ts +6 src/main/daemon/history/storage/terminal-history-session-writer.ts +1 src/main/daemon/launch/daemon-bundle-staleness.ts +1 src/main/daemon/launch/daemon-endpoint-adoption.ts +3 src/main/daemon/launch/daemon-generation-liveness.ts +2 src/main/daemon/launch/daemon-launch-paths.ts +1 src/main/daemon/launch/daemon-legacy-process-liveness.ts +2 src/main/daemon/launch/daemon-protocol-cleanup.ts +11 src/main/daemon/launch/daemon-spawner.ts +1 src/main/daemon/launch/daemon-stale-kill.ts +1 src/main/daemon/observability/daemon-adoption-telemetry-event.ts +1 src/main/daemon/observability/daemon-file-log.ts +3 src/main/daemon/permissions/daemon-tcc-attribution.ts +5 src/main/daemon/process-scopes/daemon-cgroup-scope.ts +1 src/main/daemon/process-scopes/daemon-user-manager-lifetime.ts +1 src/main/daemon/process-scopes/terminal-scopes-runtime.ts +2 src/main/daemon/process-scopes/terminal-scopes.ts +1 src/main/daemon/providers/adapter/daemon-pty-daemon-recovery.ts +1 src/main/daemon/providers/adapter/daemon-pty-session-inventory.ts +1 src/main/daemon/providers/routing/daemon-legacy-adapters.ts +2 src/main/daemon/pty-subprocess/spawn-preflight.ts +1 src/main/daemon/shell-readiness/shell-ready.ts +1 src/main/daemon/transport/client/daemon-client-token.ts +2 src/main/daemon/transport/stream/daemon-stream-backlog-probe.ts +4 src/main/devin/hook-config-json.ts +3 src/main/dsh/dsh-hook-packages.ts +3 src/main/dsh/hook-service.ts +1 src/main/emulator/android/android-sdk-host-discovery.ts +7 src/main/emulator/android/scrcpy-server-download.ts +1 src/main/emulator/emulator-probe.ts +2 src/main/emulator/serve-sim-detached-session.ts +7 src/main/emulator/serve-sim-execution.ts +17 src/main/emulator/serve-sim-runtime-materializer.ts +12 src/main/external-editors/external-editor-launch.ts +9 src/main/filesystem/durable-file-write.ts +2 src/main/filesystem/rolling-file-backup.ts +2 src/main/foreign-sqlite-readers/foreign-sqlite-reader-entry-path.ts +1 src/main/foreign-sqlite-readers/foreign-sqlite-reader-spawn.ts +1 src/main/foreign-sqlite-readers/readers/cursor-profile.ts +1 src/main/foreign-sqlite-readers/readers/opencode-binder-sessions.ts +1 src/main/git/huge-folder-ignore.ts +5 src/main/git/repo-detection.ts +13 src/main/git/repo-git-marker-scan.ts +3 src/main/git/worktrees/catalog/worktree-git-path.ts +1 src/main/github/client/check/check-job-log-cache-retention-child.mjs +3 src/main/grok/grok-hook-backup-cleanup.ts +3 src/main/grok/grok-hook-owners.ts +4 src/main/grok/grok-hook-symlink-cleanup-marker.ts +4 src/main/grok/hook-service.ts +4 src/main/hang-watchdog/hang-detection-marker.ts +1 src/main/hang-watchdog/hang-watchdog-worker-path.ts +1 src/main/hang-watchdog/previous-hang-detection.ts +1 src/main/hermes/hermes-default-home.ts +10 src/main/hermes/hermes-home-filesystem.ts +2 src/main/hermes/hook-service.ts +2 src/main/host/secret-protection-report.ts +1 src/main/ipc/app.ts +1 src/main/ipc/command-path/command-path-directory-worker-client.ts +1 src/main/ipc/command-path/command-path-resolver.ts +4 src/main/ipc/diagnostics.ts +2 src/main/ipc/filesystem/access/filesystem-path-containment.ts +2 src/main/ipc/filesystem/watcher-process/parcel-watcher-canary-directory.ts +1 src/main/ipc/filesystem/watcher-process/parcel-watcher-child-launch.ts +4 src/main/ipc/filesystem/watcher-process/parcel-watcher-delivery-canary.ts +3 src/main/ipc/filesystem/watcher-process/parcel-watcher-entry-path.ts +1 src/main/ipc/filesystem/watcher-process/parcel-watcher-shallow-file-subscription.ts +1 src/main/ipc/filesystem/watcher-process/parcel-watcher-shallow-subscription.ts +1 src/main/ipc/filesystem/watcher-process/parcel-watcher-supervisor-subscribe.ts +1 src/main/ipc/filesystem/watcher-process/rebinding-file-watch.ts +1 src/main/ipc/filesystem/watcher-process/watcher-event-root-path-rewrite.ts +1 src/main/ipc/macos-keyboard-layout-snapshot.ts +1 src/main/ipc/notification-authorization-status.ts +1 src/main/ipc/pty/spawn-pane-env.ts +1 src/main/ipc/ssh/ssh-connect-flow.ts +2 src/main/ipc/worktree-remote.ts +2 src/main/ipc/wsl-guest-windows-drive-path.ts +7 src/main/jcode/hook-service.ts +8 src/main/jira/site-credential-store.ts +7 src/main/keybindings/keybinding-file-parser.ts +1 src/main/keybindings/keybinding-file.ts +13 src/main/kimi/hook-service.ts +3 src/main/kiro/hook-service.ts +1 src/main/kiro/standalone-hooks.ts +4 src/main/linear/linear-credential-paths.ts +3 src/main/linear/linear-legacy-viewer-store.ts +1 src/main/linear/linear-token-store.ts +3 src/main/linear/linear-workspace-registry.ts +1 src/main/macos-permissions/macos-tcc-prompt-notice.ts +3 src/main/managed-data-accounts/credential-capture.ts +18 src/main/managed-data-accounts/profile-removal.ts +10 src/main/managed-data-accounts/service.ts +1 src/main/managed-server/activation/orcad-held-fence-tokens.ts +1 src/main/managed-server/install/orcad-deployment-target.ts +2 src/main/managed-server/install/remote-versioned-install.ts +1 src/main/memory/darwin-process-footprint.ts +13 src/main/mimo/hook-service.ts +6 src/main/minimax/minimax-cookie-store.ts +1 src/main/muse/hook-config-json.ts +4 src/main/muse/hook-service.ts +2 src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.ts +1 src/main/native-chat/agent-session-journal/database/journal-host-database.ts +1 src/main/native-chat/host-readable-transcript-path.ts +2 src/main/native-chat/wsl-transcript-fs-process-spawn.ts +1 src/main/node-runtime/appimage-bundled-node-runtime.ts +2 src/main/node-runtime/managed-runtime-release-install.ts +7 src/main/node-runtime/managed-runtime-release.ts +11 src/main/notebook/python-environments.ts +1 src/main/observability/bounded-log-file-writer.ts +2 src/main/observability/bundle.ts +6 src/main/observability/local-file-sink.ts +14 src/main/opencode/hook-service.ts +1 src/main/opencode/legacy-plugin-acl-retry.ts +2 src/main/opencode/opencode-overlay-manifest.ts +1 src/main/opencode/opencode-plugin-config-writer.ts +1 src/main/opencode/opencode-private-server.ts +1 src/main/opencode/opencode-wsl-text-task.ts +2 src/main/orca-profiles/new-profile-state.ts +3 src/main/orca-profiles/other-profile-workspace-catalog.ts +3 src/main/orca-profiles/profile-artifact-cloud-cleanup.ts +1 src/main/orca-profiles/profile-cloud-index.ts +4 src/main/orca-profiles/profile-cloud-session-store.ts +12 src/main/orca-profiles/profile-index-store.ts +5 src/main/orca-profiles/profile-legacy-state-import.ts +4 src/main/orca-profiles/profile-project-move-intent.ts +3 src/main/orca-profiles/profile-project-move-record.ts +4 src/main/orca-profiles/profile-project-state-file.ts +2 src/main/orcad/launcher.ts +7 src/main/orcad/node-pty-prebuilt-slot.ts +2 src/main/orcad/node-pty-precondition.ts +2 src/main/orcad/orcad-agent-browser-binary.ts +1 src/main/orcad/orcad-app-paths.ts +1 src/main/orcad/orcad-artifact-identity.ts +2 src/main/orcad/orcad-artifact-materializer.ts +12 src/main/orcad/orcad-bundled-runtime.ts +2 src/main/orcad/orcad-cli-launcher.ts +1 src/main/orcad/orcad-command-arguments.ts +1 src/main/orcad/orcad-completed-stop-receipt.ts +2 src/main/orcad/orcad-daemon-slot-pins.ts +2 src/main/orcad/orcad-data-root-locality.ts +3 src/main/orcad/orcad-data-root-privacy.ts +4 src/main/orcad/orcad-host-account-launch-env-store.ts +1 src/main/orcad/orcad-host-adapters.ts +2 src/main/orcad/orcad-idle-stop-record.ts +3 src/main/orcad/orcad-instance-lock-hold.ts +16 src/main/orcad/orcad-instance-lock-reclaim.ts +4 src/main/orcad/orcad-instance-lock.ts +7 src/main/orcad/orcad-local-serve-selection.ts +1 src/main/orcad/orcad-lock-clone-machine.ts +1 src/main/orcad/orcad-managed-stop-cancellation.ts +3 src/main/orcad/orcad-managed-stop-decision.ts +2 src/main/orcad/orcad-managed-stop-request.ts +4 src/main/orcad/orcad-plaintext-credential-migration.ts +5 src/main/orcad/orcad-process-start-time.ts +11 src/main/orcad/orcad-secret-key-record.ts +3 src/main/orcad/orcad-stale-lock-reclaim.ts +1 src/main/orcad/orcad-stop-request-listener.ts +1 src/main/orcad/orcad-wsl-holder-release.ts +2 src/main/orcad/orcad-wsl-host-claude-profile-refresh.ts +1 src/main/orcad/serve-profile-ssh-targets.ts +6 src/main/persistence/atomic-snapshot-writer.ts +6 src/main/persistence/loading-store/user-data-path.ts +3 src/main/persistence/loading-store/write-flush-barriers.ts +9 src/main/persistence/migrating-orcad-catalog/migration-journal-directory.ts +4 src/main/persistence/migrating-orcad-catalog/orcad-migration-blob-installers.ts +3 src/main/persistence/migrating-orcad-catalog/orcad-migration-blob-staging.ts +17 src/main/persistence/migrating-orcad-catalog/orcad-migration-staged-file.ts +1 src/main/persistence/migrating-orcad-catalog/orcad-scrollback-snapshot-transfer.ts +11 src/main/persistence/migrating-orcad-catalog/orcad-shell-history-installer.ts +7 src/main/persistence/migrating-orcad-catalog/orcad-terminal-history-installer.ts +1 src/main/persistence/migrating-orcad-catalog/orcad-terminal-restore-hint-store.ts +1 src/main/persistence/moving-wsl-projects/wsl-move-orchestration-history-rewrite.ts +1 src/main/persistence/moving-wsl-projects/wsl-move-orchestrator-store.ts +6 src/main/persistence/moving-wsl-projects/wsl-move-slept-scrollback.ts +2 src/main/persistence/moving-wsl-projects/wsl-move-slept-spool-sweep.ts +5 src/main/persistence/preferences/active-view-preference.ts +4 src/main/persistence/profile-state/legacy-json/profile-state-authority-exports.ts +2 src/main/persistence/profile-state/legacy-json/profile-state-export-path.ts +1 src/main/persistence/profile-state/legacy-json/profile-state-legacy-backup-path.ts +2 src/main/persistence/profile-state/legacy-json/profile-state-legacy-import.ts +8 src/main/persistence/profile-state/legacy-json/profile-state-recovery.ts +6 src/main/persistence/profile-state/legacy-json/profile-state-versioned-export.ts +5 src/main/persistence/profile-state/profile-state-access-identity.ts +12 src/main/persistence/profile-state/profile-state-access-owner.ts +3 src/main/persistence/profile-state/profile-state-access.ts +2 src/main/persistence/profile-state/profile-state-active-location.ts +5 src/main/persistence/profile-state/profile-state-authority-bootstrap.ts +1 src/main/persistence/profile-state/profile-state-authority-marker.ts +2 src/main/persistence/profile-state/profile-state-backup-path.ts +2 src/main/persistence/profile-state/profile-state-backup-worker.ts +5 src/main/persistence/profile-state/profile-state-database-quarantine.ts +9 src/main/persistence/profile-state/profile-state-database-recovery.ts +2 src/main/persistence/profile-state/profile-state-database.ts +4 src/main/persistence/profile-state/profile-state-migration.ts +6 src/main/persistence/profile-state/profile-state-offline-settings.ts +6 src/main/persistence/profile-state/profile-state-recovery-command.ts +8 src/main/persistence/profile-state/profile-state-recovery-copy.ts +2 src/main/persistence/profile-state/profile-state-recovery-crash-process.ts +1 src/main/persistence/profile-state/profile-state-recovery-required.ts +2 src/main/persistence/profile-state/profile-state-section-repair.ts +2 src/main/persistence/profile-state/profile-state-sqlite-authority.ts +1 src/main/persistence/profile-state/profile-state-startup-recovery-dialog.ts +2 src/main/persistence/profile-state/profile-state-storage-classification.ts +1 src/main/persistence/profile-state/profile-state-writer-worker-path.ts +2 src/main/persistence/restoring-sessions/floating-workspace-normalization.ts +1 src/main/persistence/tracking-repos/worktree-metadata-normalization.ts +1 src/main/persistence/window-save-divergence.ts +8 src/main/pi/legacy-omp-overlay-migration.ts +5 src/main/pi/titlebar-extension-service.ts +3 src/main/plugins/appearance/plugin-appearance-service.ts +2 src/main/plugins/installation/plugin-install-staging.ts +1 src/main/plugins/installation/plugin-install.ts +1 src/main/plugins/panels/plugin-frame-guest-sessions.ts +1 src/main/plugins/panels/plugin-source-transpile-client.ts +2 src/main/plugins/plugin-dev-watcher.ts +3 src/main/plugins/plugin-secrets-store.ts +3 src/main/plugins/plugin-storage-store.ts +1 src/main/plugins/testing/plugin-editor-static-check.ts +1 src/main/plugins/workers/plugin-host-process.ts +3 src/main/plugins/workers/plugin-worker-source-hooks.ts +8 src/main/ports/darwin-port-scan-lock.ts +1 src/main/ports/port-scan-command-client.ts +1 src/main/ports/port-scan-command-execution.ts +2 src/main/provider-process/provider-executable-missing.ts +5 src/main/providers/local-pty/local-pty-executable-spawn.ts +1 src/main/providers/local-pty/local-pty-session-operations.ts +1 src/main/providers/local-pty/local-pty-shell-ready-wrapper-root.ts +1 src/main/providers/macos-login-session-pty-probe.ts +3 src/main/providers/macos-tcc-login-shell.ts +3 src/main/providers/windows-powershell-executable.ts +2 src/main/providers/working-directory-validation.ts +4 src/main/pty/history/fish-history-session.ts +4 src/main/pty/history/terminal-history-deletion.ts +2 src/main/pty/history/terminal-history-paths.ts +5 src/main/pty/history/terminal-history.ts +1 src/main/pty/process-retirement/posix-pty-process-table.ts +12 src/main/pty/scrollback/overlay-mirror.ts +1 src/main/pty/scrollback/terminal-scrollback-snapshot-nonblocking-reads.ts +11 src/main/pty/scrollback/terminal-scrollback-snapshots.ts +2 src/main/pty/shell-startup/codex-shell-launch-preflight.ts +7 src/main/pty/shell-startup/git-bash.ts +3 src/main/pty/shell-startup/legacy-terminal-posix-tombstone.ts +9 src/main/pty/shell-startup/legacy-terminal-shim-dir.ts +1 src/main/pty/shell-startup/managed-ssh-host-login-shell.ts +1 src/main/pty/shell-startup/powershell-profile-env.ts +3 src/main/pty/shell-startup/shell-startup-env.ts +5 src/main/pty/shell-startup/shell-wrapper-file-writer.ts +6 src/main/pty/shell-startup/zsh-startup-hook-pty-harness.ts +2 src/main/pty/shell-startup/zsh-wrapper-dir-ownership.ts +1 src/main/qoder/workspace-trust.ts +4 src/main/rate-limits/cursor-auth.ts +2 src/main/rate-limits/grok-auth.ts +3 src/main/rate-limits/hidden-probe-cwd.ts +2 src/main/rate-limits/opencode-go-api-key-source.ts +1 src/main/rate-limits/zcode-usage-credentials.ts +3 src/main/ripgrep/bundled-ripgrep-path.ts +8 src/main/runtime/agent-session/ownership/agent-session-claim-identity.ts +1 src/main/runtime/agent-session/records/agent-session-record-store-slot.ts +7 src/main/runtime/agent-session/records/agent-session-runtime-end-record.ts +1 src/main/runtime/agent-session/structured-agent-session-attachment-wiring.ts +1 src/main/runtime/client/navigation/desktop-chat-creation-history.ts +4 src/main/runtime/hosts/platform-probe/host-descriptor.ts +5 src/main/runtime/mobile-cloud-relay/relay-region-preference.ts +2 src/main/runtime/mobile-cloud-relay/relay-revoke-outbox.ts +1 src/main/runtime/orchestration/db/runtime-orchestration-database-access.ts +1 src/main/runtime/push/notifications/mobile-notification-dismissal-store.ts +2 src/main/runtime/push/push-unregister-outbox.ts +2 src/main/runtime/rpc/methods/mobile-delivery/bundled-mobile-web-bundle.ts +1 src/main/runtime/rpc/server/network-source-policy-store.ts +2 src/main/runtime/rpc/server/pairing/device-registry-corruption.ts +2 src/main/runtime/rpc/server/pairing/device-registry-disk-merge.ts +1 src/main/runtime/rpc/server/pairing/device-registry-file-read.ts +3 src/main/runtime/rpc/server/pairing/e2ee-keypair.ts +3 src/main/runtime/rpc/server/runtime-metadata.ts +2 src/main/runtime/rpc/server/runtime-rpc-socket-metadata.ts +5 src/main/runtime/rpc/unix-socket-transport.ts +3 src/main/runtime/rpc/ws-fallback-port-store.ts +2 src/main/runtime/terminal/launch/claude-agent-teams-shim-env.ts +1 src/main/runtime/workspaces/persisted-surfaces/crew-operations.ts +1 src/main/server/serve-pairing-output.ts +2 src/main/speech/model-cache-path.ts +9 src/main/speech/model-manager.ts +10 src/main/speech/openai-api-key-store.ts +2 src/main/speech/speech-model-download-cleanup.ts +1 src/main/speech/speech-model-download-transport.ts +2 src/main/speech/speech-model-http-download.ts +1 src/main/speech/stt-worker-model-config.ts +2 src/main/speech/stt-worker-paths.ts +2 src/main/sqlite/harden-database-files.ts +1 src/main/sqlite/sync-database.ts +1 src/main/sqlite/versioned-cache-database.ts +1 src/main/ssh/authentication/ssh-auth-resolution.ts +2 src/main/ssh/config/ssh-config-alias-claim.ts +5 src/main/ssh/config/ssh-config-include-expander.ts +1 src/main/ssh/config/ssh-config-parser.ts +1 src/main/ssh/config/ssh-g-config-resolution.ts +3 src/main/ssh/system-transport/file-transfer/system-ssh-sftp-transfer.ts +3 src/main/ssh/system-transport/ssh-control-socket.ts +3 src/main/ssh/system-transport/system-ssh-binary.ts +3 src/main/startup/cli-launch-redirect.ts +2 src/main/startup/configure-process.ts +4 src/main/startup/gpu-fallback-marker.ts +2 src/main/startup/http1-compatibility-marker.ts +5 src/main/startup/http1-compatibility-profile-state.ts +1 src/main/startup/linux-dev-shm-policy.ts +4 src/main/startup/linux-realtime-scheduling-guard.ts +1 src/main/startup/linux-rttime-limit.ts +2 src/main/startup/profile-state-recovery-preflight.ts +1 src/main/startup/startup-diagnostics.ts +1 src/main/startup/windows-app-data-path.ts +5 src/main/startup/windows-install-dir-acl-poison-marker.ts +1 src/main/startup/windows-install-dir-acl-probe.ts +1 src/main/startup/windows-install-dir-package-acl-grant.ts +4 src/main/startup/windows-install-dir-package-acl-repair.ts +3 src/main/startup/windows-renderer-runtime-files.ts +3 src/main/startup/windows-user-data-acl.ts +6 src/main/startup/x-display-artifacts.ts +2 src/main/stats/stats-file-loader.ts +2 src/main/text-generation/source-control-prompt-files.ts +1 src/main/updater/lifecycle/windows-update-install-directory.ts +9 src/main/updater/linux-package/appimage-runtime-identity.ts +1 src/main/updater/linux-package/linux-package-install-command.ts +1 src/main/updater/linux-package/linux-update-package-type.ts +6 src/main/updater/serve-update-handoff.ts +2 src/main/usage/usage-provider-store-lifecycle.ts +1 src/main/usage/usage-scan-worker-spawn.ts +1 src/main/warp-themes/discovery.ts +1 src/main/window/desktop-integration/custom-app-icon.ts +3 src/main/window/desktop-integration/macos-press-and-hold-default.ts +1 src/main/windows/windows-administrator-restart.ts +1 src/main/windows/windows-command-paths.ts +1 src/main/windows/windows-process-table.ts +1 src/main/worker-thread-child-process/worker-thread-child-process.ts +2 src/main/worker-threads/forked-child-entry-path.ts +4 src/main/workspace-hooks/hooks.ts +10 src/main/workspace-hooks/issue-command-file.ts +4 src/main/workspace-hooks/worktree-runner-script.ts +1 src/main/workspaces/catalog/folder-repo-git-conversion.ts +2 src/main/workspaces/catalog/folder-upgrade-worktree-path.ts +3 src/main/workspaces/execution-host/project-runtime-git-router.ts +1 src/main/workspaces/space-analysis/workspace-space-scan-worker-client.ts +1 src/main/wsl/execution/wsl-executable-path.ts +1 src/main/wsl/execution/wsl-interop-spawn-directory.ts +1 src/main/wsl/execution/wsl-node-runtime-prefetch.ts +3 src/main/wsl/filesystem/wsl-drive-project-new-default.ts +10 src/main/wsl/filesystem/wsl-drive-unc-conversion-stores.ts +2 src/main/wsl/host-lifecycle/wsl-host-automation-wake-store.ts +4 src/main/wsl/host-lifecycle/wsl-host-e2e-fake-connector.ts +1 src/main/wsl/host-lifecycle/wsl-host-early-wake.ts +1 src/main/wsl/host-lifecycle/wsl-orphan-holder-release.ts +1 src/main/zcode/hook-config-json.ts +12 src/main/zcode/zcode-plan-api-key-store.ts +6 src/shared/agent-hook-listener/claude-session-continuation.ts +4 src/shared/agent-hook-listener/reverse-file-region-scan.ts +3 src/shared/agent-hook-listener/transcript-reader.ts +3 src/shared/agent-hook-listener/transport/agent-hook-endpoint-temp-cleanup.ts +13 src/shared/agent-hook-listener/transport/agent-hook-spool.ts +5 src/shared/agent-hook-listener/transport/endpoint-publication.ts +4 src/shared/agent-launch/startup/omp-fresh-config.ts +2 src/shared/appimage-cache-identity.ts +2 src/shared/bundled-web-client-manifest.ts +3 src/shared/bundled-web-client-root.ts +5 src/shared/cli-fs-reads.ts +3 src/shared/cli-runtime-pairing-inputs.ts +9 src/shared/codex-rollout-jsonl-cursor.ts +1 src/shared/codex-subagent-roster.ts +4 src/shared/ephemeral-vm-recipe-doctor.ts +1 src/shared/ephemeral-vm-recipe-runner.ts +1 src/shared/ephemeral-vm-runtime-feature-store.ts +1 src/shared/ephemeral-vm-runtime-store.ts +6 src/shared/file-content/node-bounded-file-reader.ts +5 src/shared/file-content/node-file-content-equality.ts +6 src/shared/file-content/node-source-copy-content-equality.ts +3 src/shared/grok-session-paths.ts +2 src/shared/jcode-session-files.ts +1 src/shared/muse-session-log.ts +1 src/shared/opencode-installed-plugin.ts +10 src/shared/opencode-plugin-atomic-write.ts +5 src/shared/opencode-tui-config-registration.ts +1 src/shared/opencode-tui-plugin-install.ts +2 src/shared/orcad-build-identity.ts +1 src/shared/plugins/declarative/testing/orca-testing.ts +2 src/shared/real-path-sync.ts +1 src/shared/remote-runtime/e2ee/node/remote-runtime-e2ee-v2-host-latch.ts +1 src/shared/remote-runtime/host-store/runtime-e2ee-keypair-file.ts +1 src/shared/remote-runtime/host-store/runtime-environment-sidecar.ts +2 src/shared/remote-runtime/host-store/runtime-environment-store-read-cache.ts +2 src/shared/remote-runtime/host-store/runtime-environment-store-records.ts +1 src/shared/remote-runtime/host-store/wsl-host-deployment-store.ts +13 src/shared/secure-storage/secure-file.ts +1 src/shared/secure-storage/secure-path-metadata-snapshot.ts +1 src/shared/secure-storage/secure-path-windows-acl.ts +3 src/shared/serve-abort-park.ts +7 src/shared/serve-bundle-park.ts +1 src/shared/serve-user-data-path.ts +3 src/shared/source-scan/source-tree-scan.ts +5 src/shared/startup-command-staging.ts +1 src/shared/terminal/pty/native-process/pty-process-ceiling-state.ts +8 src/shared/windows-breakaway-launcher.ts +3 src/shared/windows-console-input.ts +2 src/shared/windows-interactive-login-spawn.ts +2 src/shared/windows-retry-file-operations.ts +2 src/shared/windows-security-addon.ts +1 src/shared/windows-transient-lock-removal.ts diff --git a/config/performance-audit.md b/config/performance-audit.md index 3a11b03e7e8..e414cb79ba2 100644 --- a/config/performance-audit.md +++ b/config/performance-audit.md @@ -54,3 +54,11 @@ identity, queue-admission or retained-memory oracle. Confirm it fails with the old behavior. Use controlled, counterbalanced benchmark samples for timings; avoid new machine-dependent millisecond gates in the normal unit suite. A green source scan and these contracts cannot establish that the whole app is fast. + +Two count ratchets (counts per file may only fall) cover classes the audit only warns about: +`pnpm check:main-sync-fs-ratchet` counts synchronous `node:fs` calls (every `*Sync` export) in production `src/main` and `src/shared` +(`config/main-sync-fs-baseline.txt`), and `pnpm check:recurring-timer-ratchet` counts `setInterval` +calls in production `src/main`, `src/renderer`, `src/shared` and `src/cli` +(`config/recurring-timer-baseline.txt`). Both run in `check:ratchets` on every non-draft PR that runs static analysis. A new use either +moves off the hot path (async fs or a stat-fingerprint cache; a push signal, a visibility-gated or +backing-off timer) or raises its file's count with reviewer sign-off. diff --git a/config/recurring-timer-baseline.txt b/config/recurring-timer-baseline.txt new file mode 100644 index 00000000000..4f313845137 --- /dev/null +++ b/config/recurring-timer-baseline.txt @@ -0,0 +1,92 @@ +# setInterval calls (global, or imported from node:timers / node:timers/promises) in production src/main, +# src/renderer, src/shared and src/cli, per file. Bare calls are resolved per scope; imported bindings follow the shared +# collector (file-wide). Not counted: destructuring off a global object, a self-rescheduling setTimeout (it must back off or pause while hidden). +# This is a RATCHET: counts may only go DOWN. +# Prune after removing intervals: pnpm check:recurring-timer-ratchet --prune +1 src/cli/handlers/orchestration/check-keepalive.ts +1 src/cli/runtime/mac-app-update-bundle.ts +1 src/cli/runtime/serve-supervised-service.ts +1 src/main/agent-hooks/server/server-opencode-binder.ts +1 src/main/automations/retained-run-reconciliation.ts +1 src/main/automations/service.ts +1 src/main/browser/extensions/native-messaging/browser-extension-native-ports.ts +1 src/main/browser/extensions/web-store/browser-extension-webstore-updater.ts +1 src/main/claude/agent-view/agent-view-bridge.ts +1 src/main/cli/appimage-serve-preparation.ts +1 src/main/codex-accounts/codex-login-session.ts +1 src/main/codex/configuration/homes/codex-windows-sandbox-read-grant.ts +1 src/main/crash-reporting/pre-gone-host-memory.ts +1 src/main/crash-reporting/process-gone-diagnostics.ts +1 src/main/crash-reporting/renderer-heap-allocation-profile.ts +1 src/main/daemon/admission/host-terminal-ledger.ts +1 src/main/daemon/admission/host-terminal-policy.ts +1 src/main/daemon/endpoint/publication/daemon-endpoint-lifecycle.ts +1 src/main/daemon/transport/stream/daemon-stream-backlog-probe.ts +1 src/main/diagnostics/event-loop-delay-probe.ts +1 src/main/diagnostics/main-thread-churn-probe.ts +1 src/main/git/refs/maintenance/pack-refs-lock-ownership.ts +1 src/main/hang-watchdog/main-thread-hang-watchdog-entry.ts +1 src/main/hang-watchdog/main-thread-hang-watchdog.ts +2 src/main/ipc/filesystem/watcher-process/parcel-watcher-shallow-file-subscription.ts +1 src/main/ipc/filesystem/watcher-process/parcel-watcher-shallow-subscription.ts +1 src/main/ipc/runtime-environments/runtime-environment-preference.ts +1 src/main/ipc/worktrees/git/worktree-git-common-narrow-watch.ts +1 src/main/ipc/worktrees/git/worktree-git-common-native-upgrade-poll.ts +1 src/main/ipc/wsl/wsl-host-automation-wake-wiring.ts +1 src/main/native-chat/agent-session-attachments/agent-session-attachment-sweep.ts +1 src/main/native-chat/agent-session-wire/host/structured-agent-session-lease-renewer.ts +1 src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-continue.ts +1 src/main/native-chat/agent-session-wire/termination/structured-agent-session-idle-sweep.ts +1 src/main/notifications/desktop-presence-reporter.ts +1 src/main/orcad/orcad-browser-install-park.ts +1 src/main/orcad/orcad-lock-clone-machine.ts +1 src/main/orcad/orcad-stop-request-listener.ts +1 src/main/plugins/catalog/plugin-auto-updater.ts +1 src/main/plugins/catalog/plugin-kill-list-service.ts +1 src/main/plugins/installation/plugin-git-repository.ts +1 src/main/plugins/plugin-service-housekeeping.ts +1 src/main/rate-limits/service/service-polling.ts +1 src/main/runtime/agent-state-rules/agent-state-rules-live-update.ts +1 src/main/runtime/mobile-cloud-relay/desktop-relay-service.ts +2 src/main/runtime/mobile-cloud-relay/relay-control-liveness.ts +1 src/main/runtime/rpc/methods/orchestration/messaging/check-run.ts +1 src/main/runtime/rpc/mobile-socket-wiring.ts +1 src/main/runtime/rpc/remote-runtime-server-heartbeat.ts +1 src/main/runtime/rpc/server/runtime-metadata-ownership-watch.ts +1 src/main/runtime/rpc/unix-socket-transport.ts +1 src/main/runtime/sidebar-inputs/paired-server-rows-producer.ts +1 src/main/runtime/terminal/input/agent-prompt-permission-hold.ts +1 src/main/runtime/terminal/output/terminal-derived-agent-facts.ts +1 src/main/runtime/terminal/wait/terminal-idle-polls.ts +1 src/main/ssh/legacy-relay/bridge/legacy-relay-daily-activity.ts +1 src/main/ssh/legacy-relay/cli/legacy-relay-cli-call-limits.ts +1 src/main/ssh/port-forwarding/ssh-tcp-forwarding-probe.ts +1 src/main/ssh/protocol/ssh-channel-multiplexer.ts +1 src/main/startup/configure-process.ts +1 src/main/startup/event-loop-stall-probe.ts +1 src/main/startup/synthetic-title-runtime.ts +1 src/main/telemetry/wsl-usage-reporter.ts +1 src/main/wsl/host-lifecycle/wsl-hosts-policy-live-update.ts +1 src/main/wsl/orcad/wsl-orcad-connect-stop-fence.ts +1 src/renderer/src/app-shell/use-app-session-persistence.ts +1 src/renderer/src/components/browser-pane/browser-client-page-position-driver.ts +1 src/renderer/src/components/contextual-tours/use-contextual-tour.ts +1 src/renderer/src/components/editor/setup-contextual-copy.ts +1 src/renderer/src/components/right-sidebar/plugin-panels/health/plugin-panel-watchdog.ts +1 src/renderer/src/components/sidebar/SeveredTerminalDaemonBanner.tsx +1 src/renderer/src/components/sidebar/StalledTerminalDaemonBanner.tsx +1 src/renderer/src/components/sidebar/workspace-card/activity/prompt-cache-countdown-clock.ts +1 src/renderer/src/components/status-bar/use-resource-memory-snapshot.ts +1 src/renderer/src/components/status-bar/use-runtime-host-resources.ts +1 src/renderer/src/components/tab-bar/tab-strip-drag-scroll.ts +1 src/renderer/src/components/terminal-pane/pty-transport/delivery/terminal-delivery-watchdog.ts +1 src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/recovery/RemoteRuntimeRecoveryCard.tsx +1 src/renderer/src/components/terminal-pane/render-health/terminal-render-desync-sentinel.ts +1 src/renderer/src/hooks/use-now.ts +1 src/renderer/src/lib/agent-session/hibernation/agent-hibernation-coordinator.ts +1 src/renderer/src/lib/crash-diagnostics.ts +1 src/renderer/src/lib/window-visibility-interval.ts +1 src/renderer/src/lib/worker-auto-close.ts +1 src/renderer/src/runtime/hosts/wsl/wsl-refused-read-watch.ts +1 src/shared/remote-runtime/remote-runtime-socket-liveness.ts +1 src/shared/tmux-agent-hook-owner.ts diff --git a/config/scripts/ci/sharding/ci-shard-timings.json b/config/scripts/ci/sharding/ci-shard-timings.json index 9c25664e63d..45c1aa25247 100644 --- a/config/scripts/ci/sharding/ci-shard-timings.json +++ b/config/scripts/ci/sharding/ci-shard-timings.json @@ -1115,9 +1115,9 @@ "src/main/browser/extensions/api-runtime/browser-extension-worker-api-host.test.ts": 237, "src/main/browser/extensions/declarative-net-request/engine-stages.test.ts": 112, "src/main/browser/extensions/declarative-net-request/engine.test.ts": 145, - "src/main/browser/extensions/declarative-net-request/redirect-url.test.ts": 66, - "src/main/browser/extensions/declarative-net-request/regex-filter.test.ts": 116, - "src/main/browser/extensions/declarative-net-request/url-pattern.test.ts": 84, + "src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.test.ts": 66, + "src/main/browser/extensions/declarative-net-request/regex/regex-filter.test.ts": 116, + "src/main/browser/extensions/declarative-net-request/matching/url-pattern.test.ts": 84, "src/main/browser/element-grab/grab-guest-overlay-follow.test.ts": 332, "src/main/browser/element-grab/grab-guest-script.test.ts": 107, "src/main/browser/cdp/debugger/guest-cdp-command.test.ts": 105, diff --git a/config/scripts/performance/agent-lineage/agent-status-hot-path-benchmark.ts b/config/scripts/performance/agent-lineage/agent-status-hot-path-benchmark.ts index dd2ddb8fa18..774c2411787 100644 --- a/config/scripts/performance/agent-lineage/agent-status-hot-path-benchmark.ts +++ b/config/scripts/performance/agent-lineage/agent-status-hot-path-benchmark.ts @@ -9,23 +9,17 @@ import type { AppState } from '@/store/types' import type { AgentStatusBatchUpdate } from '@/store/slices/agent-status/agent-status-contract' import '@/store/slices/agent-status/agent-status' import { MAX_LIVE_AGENT_STATUSES } from '@/store/slices/agent-status/agent-status-capacity-eviction' -import { createTestStore } from '@/store/testing/app-store-test-fixture' -import { makeTab } from '@/store/terminals/terminal-tab.test-fixture' -import { makeUnifiedTab } from '@/store/slices/tabs/tab-group.test-fixture' -import { makeWorktree } from '@/store/slices/worktrees/worktree.test-fixture' -import { TEST_REPO } from '@/store/repos/repo.test-fixture' -import { makePaneKey } from '../../../../src/shared/workspace-layout/stable-pane-id' -import { getDefaultSettings } from '../../../../src/shared/constants' +import type { createTestStore } from '@/store/testing/app-store-test-fixture' import { createAgentStatusPaneRoutingIndex, resolvePaneKeyFromRoutingIndex } from '@/hooks/ipc-events/agent-status-pane-routing-index' +import { buildAgentStatusScaleFixture } from '@/store/slices/agent-status/agent-status-scale.test-fixture' import { resolvePaneKey } from '@/hooks/ipc-events/agent-status-routing' const WORKTREES = 423 const EVENTS = 1_000 const BATCH_SIZE = 8 -const LEAF_ID = '11111111-1111-4111-8111-111111111111' const BASE_TIME = 2_000_000_000 const counters = { maps: 0, sets: 0, tabComparisons: 0 } @@ -58,62 +52,16 @@ function withAllocationCounting(run: () => T): T { } } -/** Tab list whose iteration is observable, so the nested-loop resolver's comparisons are countable. */ -class CountingTabList extends Array { - [Symbol.iterator](): ArrayIterator { - const iterator = super[Symbol.iterator]() - const next = iterator.next.bind(iterator) - iterator.next = (...args) => { - const result = next(...args) - if (!result.done) { - counters.tabComparisons += 1 - } - return result - } - return iterator - } -} - function buildFixture(countTabIteration: boolean) { - const store = createTestStore() - const settings = store.getState().settings ?? getDefaultSettings(tmpdir()) - const tabsByWorktree: AppState['tabsByWorktree'] = {} - const unifiedTabsByWorktree: AppState['unifiedTabsByWorktree'] = {} - const worktrees: ReturnType[] = [] - const paneKeys: string[] = [] - const owners: { tabId: string; worktreeId: string }[] = [] - for (let index = 0; index < WORKTREES; index += 1) { - const worktreeId = `wt-${index}` - worktrees.push(makeWorktree({ id: worktreeId, repoId: TEST_REPO.id })) - const tabs: ReturnType[] = [] - const unified: ReturnType[] = [] - for (let tab = 0; tab < (index % 2 === 0 ? 1 : 2); tab += 1) { - const tabId = `tab-${index}-${tab}` - tabs.push(makeTab({ id: tabId, worktreeId, title: `Terminal ${index}-${tab}` })) - unified.push( - makeUnifiedTab({ - id: tabId, - worktreeId, - groupId: `group-${index}`, - label: `Label ${index}` - }) - ) - paneKeys.push(makePaneKey(tabId, LEAF_ID)) - owners.push({ tabId, worktreeId }) - } - tabsByWorktree[worktreeId] = countTabIteration ? CountingTabList.from(tabs) : tabs - unifiedTabsByWorktree[worktreeId] = unified - } - store.setState({ - repos: [TEST_REPO], - worktreesByRepo: { [TEST_REPO.id]: worktrees }, - tabsByWorktree, - unifiedTabsByWorktree, - terminalLayoutsByTabId: {}, - setGeneratedTabTitlesFromAgentPrompts: () => {}, - settings: { ...settings, tabAutoGenerateTitle: false } - }) - return { store, paneKeys, owners } + const fixture = buildAgentStatusScaleFixture( + countTabIteration + ? () => { + counters.tabComparisons += 1 + } + : undefined + ) + fixture.store.setState({ setGeneratedTabTitlesFromAgentPrompts: () => {} }) + return fixture } function measure(run: () => void): number { diff --git a/config/scripts/preflight/changed-checks/check-changed-selection.mjs b/config/scripts/preflight/changed-checks/check-changed-selection.mjs index 14eae82cea5..3bf510cc912 100644 --- a/config/scripts/preflight/changed-checks/check-changed-selection.mjs +++ b/config/scripts/preflight/changed-checks/check-changed-selection.mjs @@ -35,8 +35,15 @@ const RATCHET_INPUTS = { 'check-host-list-render-ratchet.mjs': ['src/renderer/src/'], 'check-id-only-store-write-ratchet.mjs': ['src/main/persistence/'], 'check-layout-writer-ratchets.mjs': ['src/main/', 'src/renderer/src/'], + 'check-main-sync-fs-ratchet.mjs': ['src/main/', 'src/shared/'], 'check-main-sync-spawn-ratchet.mjs': ['src/main/'], 'check-react-compiler-compat-ratchet.mjs': ['src/renderer/src/'], + 'check-recurring-timer-ratchet.mjs': [ + 'src/main/', + 'src/renderer/src/', + 'src/shared/', + 'src/cli/' + ], 'check-render-path-translate-ratchet.mjs': ['src/renderer/src/'], 'check-repo-host-read-ratchet.mjs': ['src/cli/', 'src/main/', 'src/shared/'], 'check-runtime-electron-ratchet.mjs': ['src/main/', 'src/cli/', 'src/shared/'], @@ -258,7 +265,9 @@ export function selectTypecheckProjects(files) { const RATCHET_SHARED_MODULES = { 'config/scripts/static-analysis/process-boundaries/gated-call-use-collector.mjs': [ 'check-daemon-blocking-call-ratchet.mjs', - 'check-main-sync-spawn-ratchet.mjs' + 'check-main-sync-fs-ratchet.mjs', + 'check-main-sync-spawn-ratchet.mjs', + 'check-recurring-timer-ratchet.mjs' ], 'config/scripts/static-analysis/ratchets/react-compiler/react-compiler-hazards.mjs': [ 'check-react-compiler-compat-ratchet.mjs' diff --git a/config/scripts/preflight/changed-checks/run-lint.mjs b/config/scripts/preflight/changed-checks/run-lint.mjs index 7b3d7be3632..0a2bbe3c723 100644 --- a/config/scripts/preflight/changed-checks/run-lint.mjs +++ b/config/scripts/preflight/changed-checks/run-lint.mjs @@ -41,6 +41,8 @@ export const LINT_STEPS = [ 'check:workspace-record-read-ratchet', 'check:unproven-pty-retirement-ratchet', 'check:daemon-blocking-call-ratchet', + 'check:main-sync-fs-ratchet', + 'check:recurring-timer-ratchet', 'check:node-runtime-pin', 'check:path-literal-ratchet', 'check:host-list-render-ratchet', diff --git a/config/scripts/static-analysis/process-boundaries/check-main-sync-spawn-ratchet.test.mjs b/config/scripts/static-analysis/process-boundaries/check-main-sync-spawn-ratchet.test.mjs index 01fce47e89b..15c416efd14 100644 --- a/config/scripts/static-analysis/process-boundaries/check-main-sync-spawn-ratchet.test.mjs +++ b/config/scripts/static-analysis/process-boundaries/check-main-sync-spawn-ratchet.test.mjs @@ -77,12 +77,12 @@ describe('main sync-spawn ratchet', () => { ], [ "const { execSync } = require('child_process')\nconst run = execSync\nrun('a')", - 2, + 1, ['execSync'] ], [ "function f() { const run = cp.execSync; run('a') }\nconst cp = require('child_process')", - 2, + 1, ['execSync'] ], [ @@ -91,10 +91,18 @@ describe('main sync-spawn ratchet', () => { ['execFileSync'] ], [ - "import { runProcessSync } from '@orca/process-host'\nconst run = runProcessSync", + "import { runProcessSync } from '@orca/process-host'\nconst run = runProcessSync\nrun(a)", 1, ['runProcessSync'] - ] + ], + // Binding an alias is setup; each call through it is a use, so an alias refactor keeps the count. + [ + "import { execSync } from 'child_process'\nconst run = execSync\nrun('a')\nrun('b')", + 2, + ['execSync'] + ], + ["import { execSync } from 'child_process'\nexport const run = execSync", 1, ['execSync']], + ["import { execSync } from 'child_process'\nconst out = execSync('a')", 1, ['execSync']] ])('counts %s', (source, count, symbols) => { expect(uses(source)).toEqual({ count, symbols }) }) diff --git a/config/scripts/static-analysis/process-boundaries/gated-call-use-collector.mjs b/config/scripts/static-analysis/process-boundaries/gated-call-use-collector.mjs index da92736ba0a..e17bb894adf 100644 --- a/config/scripts/static-analysis/process-boundaries/gated-call-use-collector.mjs +++ b/config/scripts/static-analysis/process-boundaries/gated-call-use-collector.mjs @@ -38,6 +38,15 @@ function isValueReference(node) { ) } +function isExportedDeclaration(declaration) { + const statement = declaration.parent?.parent + return ( + !!statement && + ts.isVariableStatement(statement) && + !!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) + ) +} + /** * Each family lists module specifiers (packages, or repo paths without extension) and the gated * exports to count, each with advice for the failure message. @@ -72,7 +81,7 @@ export function createGatedCallCollector(families, ratchetName) { ) } - // Count each gated value reference, alias use and re-export; imports alone do not count. + // Count each gated value reference, alias use and re-export; imports and plain alias bindings alone do not count. // File-wide, not scope-aware: computed specifiers/members and non-const holder flows are unresolved; local wrappers are not traced. function collectUses(file, contents) { // Escaped spellings and relative imports inside gated modules still need the full analysis. @@ -197,7 +206,7 @@ export function createGatedCallCollector(families, ratchetName) { identifiers.push(node) } else if (ts.isVariableDeclaration(node) && node.initializer) { if (ts.isIdentifier(node.name)) { - aliases.push({ name: node.name.text, initializer: node.initializer }) + aliases.push({ name: node.name.text, initializer: node.initializer, declaration: node }) } else if (ts.isObjectBindingPattern(node.name)) { destructures.push(node) } @@ -236,14 +245,30 @@ export function createGatedCallCollector(families, ratchetName) { } } } + // Binding a tracked alias is setup, not a use: its calls are charged instead, so an alias refactor + // keeps the count. Conditional/fallback aliases and exported bindings stay charged (conservative). + const aliasSetup = new Set() + for (const { name, initializer, declaration } of aliases) { + const target = unwrap(initializer) + if ( + functions.has(name) && + (ts.isIdentifier(target) || + ts.isPropertyAccessExpression(target) || + ts.isElementAccessExpression(target)) && + gatedFunction(target) && + !isExportedDeclaration(declaration) + ) { + aliasSetup.add(target) + } + } for (const node of members) { const name = propertyName(node) - if (moduleSymbols(node.expression)?.has(name)) { + if (!aliasSetup.has(node) && moduleSymbols(node.expression)?.has(name)) { recordUse(name) } } for (const node of identifiers) { - const name = functions.get(node.text) + const name = aliasSetup.has(node) ? null : functions.get(node.text) if (name) { recordUse(name) } diff --git a/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-main-sync-fs-ratchet.mjs b/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-main-sync-fs-ratchet.mjs new file mode 100644 index 00000000000..82ad501f702 --- /dev/null +++ b/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-main-sync-fs-ratchet.mjs @@ -0,0 +1,115 @@ +import process from 'node:process' +import { pathToFileURL } from 'node:url' +import { createGatedCallCollector } from '../../process-boundaries/gated-call-use-collector.mjs' +import { readTrackedSources, runCountRatchet } from '../../ratchet-baseline/ratchet-baseline.mjs' +import { isTestOnlySourcePath } from '../../process-boundaries/test-only-source-path.mjs' + +// Sync fs on Electron main stalls every window (and in the daemon/orcad, every terminal); per-spawn/RPC/focus stat and read sweeps were a recurring lag source (#39805, #35967, #38481). + +const BASELINE_PATH = 'config/main-sync-fs-baseline.txt' +// src/shared runs on main too; leaving it out would let a helper move bypass the gate. +const SCAN_ROOTS = ['src/main', 'src/shared'] +const ADVICE = + 'use node:fs/promises, or cache the result on a stat fingerprint outside the hot path' +// Every *Sync export of node:fs (descriptor and copy APIs included), listed so the baseline doesn't depend on the runtime. +const SYNC_FS = [ + 'accessSync', + 'appendFileSync', + 'chmodSync', + 'chownSync', + 'closeSync', + 'copyFileSync', + 'cpSync', + 'existsSync', + 'fchmodSync', + 'fchownSync', + 'fdatasyncSync', + 'fstatSync', + 'fsyncSync', + 'ftruncateSync', + 'futimesSync', + 'globSync', + 'lchmodSync', + 'lchownSync', + 'linkSync', + 'lstatSync', + 'lutimesSync', + 'mkdirSync', + 'mkdtempDisposableSync', + 'mkdtempSync', + 'openSync', + 'opendirSync', + 'readFileSync', + 'readSync', + 'readdirSync', + 'readlinkSync', + 'readvSync', + 'realpathSync', + 'renameSync', + 'rmSync', + 'rmdirSync', + 'statSync', + 'statfsSync', + 'symlinkSync', + 'truncateSync', + 'unlinkSync', + 'utimesSync', + 'writeFileSync', + 'writeSync', + 'writevSync' +] +const collector = createGatedCallCollector( + [ + { + modules: ['fs', 'node:fs', 'original-fs', 'graceful-fs'], + advice: Object.fromEntries(SYNC_FS.map((name) => [name, ADVICE])) + } + ], + 'main sync-fs' +) + +function isScannedPath(rel) { + return /\.[cm]?[jt]sx?$/.test(rel) && !rel.endsWith('.d.ts') && !isTestOnlySourcePath(rel) +} + +export function countSyncFsCalls(rel, source) { + return collector.collectUses(rel, source).count +} + +export function collectMainSyncFsCounts(root = process.cwd()) { + const counts = new Map() + for (const [rel, source] of readTrackedSources(root, SCAN_ROOTS, isScannedPath)) { + const count = countSyncFsCalls(rel, source) + if (count > 0) { + counts.set(rel, count) + } + } + return counts +} + +export function main(root = process.cwd(), arg = undefined) { + return runCountRatchet( + { + name: 'main-sync-fs', + baselinePath: BASELINE_PATH, + header: [ + '# Synchronous node:fs calls (every *Sync export) in production src/main and src/shared, per file.', + '# Startup-only or one-shot uses are fine to keep; per-RPC, per-spawn, per-focus or per-poll uses are the target.', + '# This is a RATCHET: counts may only go DOWN.', + '# Prune after removing calls: pnpm check:main-sync-fs-ratchet --prune' + ], + collect: collectMainSyncFsCounts, + unit: 'sync fs call(s)', + fix: [ + `Sync fs blocks the thread it runs on: Electron main (every window) or the daemon/orcad loop (every terminal). ${ADVICE}.`, + `A one-shot startup or shutdown use may raise the file's count in ${BASELINE_PATH} with reviewer sign-off.` + ] + }, + root, + arg + ) +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + process.exit(main(process.cwd(), process.argv[2])) +} diff --git a/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.mjs b/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.mjs new file mode 100644 index 00000000000..e038949c153 --- /dev/null +++ b/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.mjs @@ -0,0 +1,168 @@ +import process from 'node:process' +import { pathToFileURL } from 'node:url' +import ts from 'typescript-api' +import { createGatedCallCollector } from '../../process-boundaries/gated-call-use-collector.mjs' +import { readTrackedSources, runCountRatchet } from '../../ratchet-baseline/ratchet-baseline.mjs' +import { isTestOnlySourcePath } from '../../process-boundaries/test-only-source-path.mjs' + +// Fixed-cadence polls that never back off, settle or pause while hidden were the most common perf fix class (#39734, #38902, #33829, #39650). + +const BASELINE_PATH = 'config/recurring-timer-baseline.txt' +const SCAN_ROOTS = ['src/main', 'src/renderer/src', 'src/shared', 'src/cli'] +const TIMER_HOSTS = new Set(['window', 'globalThis', 'self', 'global']) +const TIMER_MODULES = ['timers', 'node:timers', 'timers/promises', 'node:timers/promises'] +const timerImports = createGatedCallCollector( + [ + { + modules: TIMER_MODULES, + advice: { setInterval: 'prefer a push signal or a backing-off timer' } + } + ], + 'recurring-timer' +) + +function isScannedPath(rel) { + return /\.[cm]?[jt]sx?$/.test(rel) && !rel.endsWith('.d.ts') && !isTestOnlySourcePath(rel) +} + +function isHostInterval(node) { + if (!ts.isIdentifier(node.expression) || !TIMER_HOSTS.has(node.expression.text)) { + return false + } + if (ts.isPropertyAccessExpression(node)) { + return node.name.text === 'setInterval' + } + return ( + ts.isElementAccessExpression(node) && + ts.isStringLiteralLike(node.argumentExpression) && + node.argumentExpression.text === 'setInterval' + ) +} + +// Skips transparent wrappers so `typeof (setInterval)` and `(setInterval) = x` read like the bare forms. +function outerExpression(node) { + let current = node + while (ts.isParenthesizedExpression(current.parent)) { + current = current.parent + } + return current +} + +// Reads the timer value: not a plain `=` target (compound assignments read it), a `typeof` probe or a JSX tag name. +function isRuntimeRead(node) { + const outer = outerExpression(node) + const parent = outer.parent + return !( + ts.isTypeOfExpression(parent) || + (ts.isBinaryExpression(parent) && + parent.left === outer && + parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) || + ((ts.isJsxOpeningElement(parent) || + ts.isJsxSelfClosingElement(parent) || + ts.isJsxClosingElement(parent)) && + parent.tagName === node) + ) +} + +// A value reference to the identifier, not a declaration, member name or key. +function isBareValueReference(node) { + const parent = node.parent + return ( + isRuntimeRead(node) && + !( + (ts.isPropertyAccessExpression(parent) && parent.name === node) || + (ts.isPropertyAssignment(parent) && parent.name === node) || + ts.isImportSpecifier(parent) || + ts.isExportSpecifier(parent) || + (ts.isBindingElement(parent) && (parent.name === node || parent.propertyName === node)) || + (ts.isVariableDeclaration(parent) && parent.name === node) || + (ts.isParameter(parent) && parent.name === node) || + ((ts.isFunctionDeclaration(parent) || ts.isMethodDeclaration(parent)) && parent.name === node) + ) + ) +} + +const SCOPE_OPTIONS = { noLib: true, noResolve: true, allowJs: true, types: [] } + +// Counts references, not calls, to the global timer, so `const every = setInterval; every(f)` and +// `window['setInterval']` are charged once. A bare name is the global when it binds to nothing +// declared in this file (resolved per scope); module bindings and their aliases are the collector's. +function countGlobalTimerReferences(source) { + const host = ts.createCompilerHost(SCOPE_OPTIONS) + host.getSourceFile = (name) => (name === source.fileName ? source : undefined) + const checker = ts.createProgram([source.fileName], SCOPE_OPTIONS, host).getTypeChecker() + let count = 0 + const visit = (node) => { + if (ts.isTypeNode(node) && !ts.isExpressionWithTypeArguments(node)) { + return + } + if ( + (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) && + isHostInterval(node) && + isRuntimeRead(node) + ) { + count += 1 + } else if (ts.isIdentifier(node) && node.text === 'setInterval' && isBareValueReference(node)) { + // A shorthand `{ setInterval }` names the property; its value is the binding being read. + const symbol = ts.isShorthandPropertyAssignment(node.parent) + ? checker.getShorthandAssignmentValueSymbol(node.parent) + : checker.getSymbolAtLocation(node) + const declarations = symbol?.declarations ?? [] + if (!declarations.some((declaration) => declaration.getSourceFile() === source)) { + count += 1 + } + } + ts.forEachChild(node, visit) + } + visit(source) + return count +} + +export function countRecurringTimers(rel, source) { + if (!source.includes('setInterval')) { + return 0 + } + const file = ts.createSourceFile(rel, source, ts.ScriptTarget.Latest, true) + return countGlobalTimerReferences(file) + timerImports.collectUses(rel, source).count +} + +export function collectRecurringTimerCounts(root = process.cwd()) { + const counts = new Map() + for (const [rel, source] of readTrackedSources(root, SCAN_ROOTS, isScannedPath)) { + const count = countRecurringTimers(rel, source) + if (count > 0) { + counts.set(rel, count) + } + } + return counts +} + +export function main(root = process.cwd(), arg = undefined) { + return runCountRatchet( + { + name: 'recurring-timer', + baselinePath: BASELINE_PATH, + header: [ + '# setInterval calls (global, or imported from node:timers / node:timers/promises) in production src/main,', + '# src/renderer, src/shared and src/cli, per file. Bare calls are resolved per scope; imported bindings follow the shared', + '# collector (file-wide). Not counted: destructuring off a global object, a self-rescheduling setTimeout (it must back off or pause while hidden).', + '# This is a RATCHET: counts may only go DOWN.', + '# Prune after removing intervals: pnpm check:recurring-timer-ratchet --prune' + ], + collect: collectRecurringTimerCounts, + unit: 'setInterval call(s)', + fix: [ + 'A fixed-cadence interval keeps working when nothing changes, the window is hidden or its dependency keeps failing.', + 'Prefer a push signal; otherwise use installWindowVisibilityInterval (renderer) or a timer that backs off and stops when idle.', + 'Rewriting it as a self-rescheduling setTimeout at the same fixed cadence is the same bug; review will treat it so.', + `An interval whose cadence is the feature (a clock, a heartbeat with a stop) may raise the file's count in ${BASELINE_PATH} with reviewer sign-off.` + ] + }, + root, + arg + ) +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + process.exit(main(process.cwd(), process.argv[2])) +} diff --git a/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.test.mjs b/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.test.mjs new file mode 100644 index 00000000000..51223986d45 --- /dev/null +++ b/config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.test.mjs @@ -0,0 +1,40 @@ +import { describe, expect, it } from 'vitest' +import { countRecurringTimers } from './check-recurring-timer-ratchet.mjs' + +describe('recurring-timer ratchet', () => { + it.each([ + ['setInterval(poll, 1000)', 1], + ['window.setInterval(poll, 1)\nglobalThis.setInterval(poll, 1)\nobj.setInterval(1)', 2], + [ + "import { setInterval } from 'node:timers'\nsetInterval(poll, 1)\nwindow.setInterval(poll, 1)", + 2 + ], + ["const { setInterval } = require('node:timers')\nsetInterval(poll, 1)", 1], + ["import { setInterval as every } from 'node:timers/promises'\nevery(5000)", 1], + // Alias and destructure forms of an imported timer count once, like the direct call. + [ + "import * as timers from 'node:timers'\nconst setInterval = timers.setInterval\nsetInterval(poll, 1)", + 1 + ], + [ + "const timers = require('node:timers')\nconst { setInterval } = timers\nsetInterval(poll, 1)", + 1 + ], + ["const { setInterval } = await import('node:timers')\nsetInterval(poll, 1)", 1], + // An unrelated local binding must not hide a global poll elsewhere in the file. + ['function configure(setInterval) {\n return setInterval\n}\nsetInterval(poll, 1000)', 1], + ['type Configure = (setInterval: number) => void\nsetInterval(poll, 1000)', 1], + ['function a(setInterval) { setInterval(f) }\nfunction b() { setInterval(g, 1) }', 1], + ['function setInterval(f) {}\nsetInterval(poll)', 0], + // Aliases and bracket access of the global timer are charged at the reference. + ['const schedule = setInterval\nschedule(poll, 1000)', 1], + ["window['setInterval'](poll, 1000)", 1], + ['const every = window.setInterval\nevery(poll, 1)', 1], + ["if (typeof setInterval === 'function') {}\ntype T = typeof setInterval", 0], + ['const deps = { setInterval, clearInterval }\ndeps.setInterval(poll, 1)', 1], + ["setInterval = replacement\nwindow['setInterval'] = replacement\ntypeof (setInterval)", 0], + ['(window.setInterval ||= fallback)(poll, 1000)', 1] + ])('counts %s', (source, count) => { + expect(countRecurringTimers('src/main/example.ts', source)).toBe(count) + }) +}) diff --git a/config/vitest.performance.config.ts b/config/vitest.performance.config.ts index 764857f9fe3..3d058e384c9 100644 --- a/config/vitest.performance.config.ts +++ b/config/vitest.performance.config.ts @@ -23,7 +23,20 @@ const contracts = [ 'config/scripts/static-analysis/oxlint-contracts/app-store-performance-plugin.test.mjs', 'config/scripts/static-analysis/oxlint-contracts/quadratic-buffer-concat-plugin.test.mjs', 'src/main/claude-usage/scanner-incremental-append.test.ts', - 'src/main/claude-usage/store-checkpoint-persistence.test.ts' + 'src/main/claude-usage/store-checkpoint-persistence.test.ts', + // Renderer hot paths: render, identity and publication counts at N and 10N (agent status, sidebar, tab strip, chat). + 'src/renderer/src/store/slices/agent-status/agent-status-batch.performance.test.ts', + 'src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-agent-rows.performance.test.tsx', + 'src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-activity-status.performance.test.tsx', + 'src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.performance.test.tsx', + 'src/renderer/src/components/tab-bar/use-terminal-tab-activity.performance.test.tsx', + 'src/renderer/src/components/tab-bar/tab-agent-types-by-tab-id.performance.test.ts', + 'src/renderer/src/app-shell/use-runtime-graph-sync.performance.test.tsx', + 'src/renderer/src/components/native-chat/markdown/NativeChatMarkdown.streaming.performance.test.tsx', + 'src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.composer-typing.performance.test.tsx', + 'src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx', + 'src/renderer/src/components/native-chat/viewport/virtualization/NativeChatMessageList.windowing.performance.test.tsx', + 'src/renderer/src/components/activity/use-agent-pane-threads.performance.test.tsx' ] for (const contract of contracts) { if (!existsSync(resolve(contract))) { diff --git a/package.json b/package.json index 283d28dcd45..4014ada5610 100644 --- a/package.json +++ b/package.json @@ -226,6 +226,8 @@ "build:packages": "node config/scripts/workspace-package-build/build-workspace-packages.mjs", "check:process-host-imports": "node config/scripts/static-analysis/process-boundaries/check-process-host-imports.mjs", "check:main-sync-spawn-ratchet": "node config/scripts/static-analysis/process-boundaries/check-main-sync-spawn-ratchet.mjs", + "check:main-sync-fs-ratchet": "node config/scripts/static-analysis/ratchets/runtime-lifecycle/check-main-sync-fs-ratchet.mjs", + "check:recurring-timer-ratchet": "node config/scripts/static-analysis/ratchets/runtime-lifecycle/check-recurring-timer-ratchet.mjs", "check:compat-boundary-imports": "node config/scripts/static-analysis/process-boundaries/check-compat-boundary-imports.mjs", "check:import-boundaries": "bun config/scripts/static-analysis/process-boundaries/check-import-boundaries.mjs" }, diff --git a/src/main/browser/extensions/actions/browser-extension-toolbar.ts b/src/main/browser/extensions/actions/browser-extension-toolbar.ts index c7546303b53..ea8108e6c7e 100644 --- a/src/main/browser/extensions/actions/browser-extension-toolbar.ts +++ b/src/main/browser/extensions/actions/browser-extension-toolbar.ts @@ -1,5 +1,5 @@ import { webContents } from 'electron' -import { noteBrowserExtensionUserGesture } from '../api/permissions' +import { noteBrowserExtensionUserGesture } from '../permissions/browser-extension-user-gesture' import type { BrowserExtensionActionResult } from '../../../../shared/browser/extensions/browser-extension-types' import { BROWSER_EXTENSION_POPUP_REQUEST_CHANNEL, diff --git a/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts b/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts index 9c6d2083c11..f603dd5cd73 100644 --- a/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts +++ b/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts @@ -70,7 +70,7 @@ await import('./permissions') const { getBrowserExtensionProfileStateStore } = await import('../profile-storage/browser-extension-profile-state-store') const menuHost = await import('../context-menus/browser-extension-context-menu-host') -const webRequest = await import('../browser-extension-web-request') +const webRequest = await import('../requests/browser-extension-web-request-observer') const { setBrowserExtensionSupportEnabled } = await import('../browser-extension-support') const contexts = await import('../api-runtime/browser-extension-contexts') const eventDispatch = await import('../api-runtime/browser-extension-event-dispatch') diff --git a/src/main/browser/extensions/api/commands.ts b/src/main/browser/extensions/api/commands.ts index 2d360905381..8e21f2f3f33 100644 --- a/src/main/browser/extensions/api/commands.ts +++ b/src/main/browser/extensions/api/commands.ts @@ -22,7 +22,7 @@ import { resolveBrowserExtensionTab } from '../tabs-and-windows/browser-extension-tab-model' import { runBrowserExtensionToolbarAction } from '../actions/browser-extension-toolbar' -import { noteBrowserExtensionUserGesture } from './permissions' +import { noteBrowserExtensionUserGesture } from '../permissions/browser-extension-user-gesture' function isOrcaShortcut(input: Electron.Input): boolean { const overrides = browserManager.getKeybindingOverrides() diff --git a/src/main/browser/extensions/api/cookies.ts b/src/main/browser/extensions/api/cookies.ts index 92e5d49c716..d28b9ca95f3 100644 --- a/src/main/browser/extensions/api/cookies.ts +++ b/src/main/browser/extensions/api/cookies.ts @@ -9,7 +9,7 @@ import { } from '../api-runtime/browser-extension-namespace-registry' import { hasBrowserExtensionHostPermission } from '../permissions/browser-extension-permission-grants' import { listBrowserExtensionTabs } from '../tabs-and-windows/browser-extension-tab-model' -import { browserExtensionHostPatternsInSession } from './permissions' +import { browserExtensionHostPatternsInSession } from '../permissions/browser-extension-session-permissions' /** Each extension session has one cookie store; Chrome names the regular store "0". */ const STORE_ID = '0' diff --git a/src/main/browser/extensions/api/declarative-net-request-test-match.ts b/src/main/browser/extensions/api/declarative-net-request-test-match.ts index 4b8dd84c158..564602f9bd9 100644 --- a/src/main/browser/extensions/api/declarative-net-request-test-match.ts +++ b/src/main/browser/extensions/api/declarative-net-request-test-match.ts @@ -1,9 +1,15 @@ import { z } from 'zod' import { createDnrRequestState } from '../declarative-net-request/request-state' import { DnrEngine } from '../declarative-net-request/engine' -import type { DnrHostAccess } from '../declarative-net-request/extension-matcher' -import { compareActions, type DnrRequestAction } from '../declarative-net-request/ruleset-matcher' -import { DNR_REQUEST_METHODS, DNR_RESOURCE_TYPES } from '../declarative-net-request/rule-types' +import type { DnrHostAccess } from '../declarative-net-request/matching/extension-matcher' +import { + compareActions, + type DnrRequestAction +} from '../declarative-net-request/matching/ruleset-matcher' +import { + DNR_REQUEST_METHODS, + DNR_RESOURCE_TYPES +} from '../declarative-net-request/rule-compilation/rule-types' import { BrowserExtensionApiError } from '../api-runtime/browser-extension-namespace-registry' import type { DnrExtensionRules } from '../declarative-net-request/session/browser-extension-dnr-extension-rules' import { DNR_ERRORS } from '../declarative-net-request/session/browser-extension-dnr-limits' diff --git a/src/main/browser/extensions/api/declarative-net-request.ts b/src/main/browser/extensions/api/declarative-net-request.ts index 010fef4770f..beaeba5e2a6 100644 --- a/src/main/browser/extensions/api/declarative-net-request.ts +++ b/src/main/browser/extensions/api/declarative-net-request.ts @@ -1,5 +1,5 @@ import { z } from 'zod' -import { isDnrRegexSupported } from '../declarative-net-request/regex-filter' +import { isDnrRegexSupported } from '../declarative-net-request/regex/regex-filter' import { hasBrowserExtensionActiveTab } from '../permissions/browser-extension-active-tab' import { DnrApiError, diff --git a/src/main/browser/extensions/api/management.test.ts b/src/main/browser/extensions/api/management.test.ts index e16e2e09e3d..96ff8bd8026 100644 --- a/src/main/browser/extensions/api/management.test.ts +++ b/src/main/browser/extensions/api/management.test.ts @@ -25,7 +25,9 @@ vi.mock('../browser-extension-management', () => ({ vi.mock('../installation/browser-extension-list-changes', () => ({ notifyBrowserExtensionListChanged: mocks.notify })) -vi.mock('./permissions', () => ({ hasBrowserExtensionUserGesture: () => mocks.gesture })) +vi.mock('../permissions/browser-extension-user-gesture', () => ({ + hasBrowserExtensionUserGesture: () => mocks.gesture +})) const registry = await import('../api-runtime/browser-extension-namespace-registry') const { registerManagementNamespace } = await import('./management') diff --git a/src/main/browser/extensions/api/management.ts b/src/main/browser/extensions/api/management.ts index 22dbc257e1d..be6cf127186 100644 --- a/src/main/browser/extensions/api/management.ts +++ b/src/main/browser/extensions/api/management.ts @@ -12,7 +12,7 @@ import { registerBrowserExtensionNamespace, type BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' -import { hasBrowserExtensionUserGesture } from './permissions' +import { hasBrowserExtensionUserGesture } from '../permissions/browser-extension-user-gesture' /** * chrome.management for extensions that declare "management": Orca's installed list as the diff --git a/src/main/browser/extensions/api/notifications.test.ts b/src/main/browser/extensions/api/notifications.test.ts index 1f305468c42..4443dcd4b8b 100644 --- a/src/main/browser/extensions/api/notifications.test.ts +++ b/src/main/browser/extensions/api/notifications.test.ts @@ -31,13 +31,16 @@ vi.mock('../../../window/foreground-activation-policy', () => ({ isBackgroundLaunch: () => mocks.background })) vi.mock('../api-runtime/browser-extension-api-host', () => ({ emitBrowserExtensionEvent: vi.fn() })) -vi.mock('./permissions', () => ({ noteBrowserExtensionUserGesture: vi.fn() })) +vi.mock('../permissions/browser-extension-user-gesture', () => ({ + noteBrowserExtensionUserGesture: vi.fn() +})) const { registerNotificationsNamespace } = await import('./notifications') const { getBrowserExtensionNamespace } = await import('../api-runtime/browser-extension-namespace-registry') const { emitBrowserExtensionEvent } = await import('../api-runtime/browser-extension-api-host') -const { noteBrowserExtensionUserGesture } = await import('./permissions') +const { noteBrowserExtensionUserGesture } = + await import('../permissions/browser-extension-user-gesture') registerNotificationsNamespace() const extension = { id: 'a'.repeat(32), path: join('/ext', 'root') } diff --git a/src/main/browser/extensions/api/notifications.ts b/src/main/browser/extensions/api/notifications.ts index 067c8546ed3..e397022f9a5 100644 --- a/src/main/browser/extensions/api/notifications.ts +++ b/src/main/browser/extensions/api/notifications.ts @@ -14,7 +14,7 @@ import { registerBrowserExtensionNamespace, type BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' -import { noteBrowserExtensionUserGesture } from './permissions' +import { noteBrowserExtensionUserGesture } from '../permissions/browser-extension-user-gesture' const optionsSchema = z .object({ diff --git a/src/main/browser/extensions/api/permissions.ts b/src/main/browser/extensions/api/permissions.ts index 79154866d1f..d575757ce9e 100644 --- a/src/main/browser/extensions/api/permissions.ts +++ b/src/main/browser/extensions/api/permissions.ts @@ -1,3 +1,5 @@ +import { readBrowserExtensionProfileGrants } from '../permissions/browser-extension-session-permissions' +import { hasBrowserExtensionUserGesture } from '../permissions/browser-extension-user-gesture' import { z } from 'zod' import { emitBrowserExtensionEvent, @@ -11,7 +13,6 @@ import { type BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' import { - browserExtensionHostPatterns, containsBrowserExtensionPermissions, effectiveBrowserExtensionPermissions, planBrowserExtensionPermissionRemoval, @@ -25,10 +26,8 @@ import { getBrowserExtensionProfileStateStore } from '../profile-storage/browser import { getHostSessionState } from '../api-runtime/browser-extension-contexts' import { getBrowserExtensionContextSignal } from '../api-runtime/browser-extension-context-lifetime' import { getDnrSessionRuntime } from '../declarative-net-request/session/browser-extension-dnr-session' -import { observeBrowserExtensionWebRequests } from '../browser-extension-web-request' +import { observeBrowserExtensionWebRequests } from '../requests/browser-extension-web-request-observer' -const USER_GESTURE_WINDOW_MS = 5_000 -const recentGestures = new WeakMap() const pendingRequests = new Set() const permissionsSchema = z @@ -40,51 +39,6 @@ function gestureKey(profileId: string, extensionId: string): string { return `${profileId}\n${extensionId}` } -/** - * Chrome lets permissions.request run only inside a user gesture. Orca records the gestures it - * delivers to an extension (menu click, command, notification click, toolbar click). - */ -export function noteBrowserExtensionUserGesture( - session: Electron.Session, - extensionId: string -): void { - const state = getHostSessionState(session) - if (!state) { - return - } - const signal = getBrowserExtensionContextSignal(state, extensionId) - recentGestures.set(signal, Date.now()) -} - -export function hasBrowserExtensionUserGesture(caller: BrowserExtensionCaller): boolean { - if (caller.userGesture === true) { - return true - } - const state = getHostSessionState(caller.session) - if (!state) { - return false - } - const signal = getBrowserExtensionContextSignal(state, caller.extension.id) - const at = recentGestures.get(signal) - return at !== undefined && !signal.aborted && Date.now() - at <= USER_GESTURE_WINDOW_MS -} - -function readGrants(profileId: string, extensionId: string) { - return getBrowserExtensionProfileStateStore().read(profileId, extensionId) -} - -/** Manifest host patterns plus origins the user granted in this session's profile. */ -export function browserExtensionHostPatternsInSession( - extension: Electron.Extension, - session: Electron.Session -): string[] { - const profileId = getBrowserExtensionProfileIdForSession(session) - const grants = profileId - ? readGrants(profileId, extension.id) - : { grantedPermissions: [], grantedOrigins: [] } - return browserExtensionHostPatterns(extension.manifest, grants) -} - function emitChange( caller: BrowserExtensionCaller, event: 'onAdded' | 'onRemoved', @@ -108,7 +62,7 @@ async function request( const { extension, browserProfileId } = caller const plan = planBrowserExtensionPermissionRequest( extension.manifest, - readGrants(browserProfileId, extension.id), + readBrowserExtensionProfileGrants(browserProfileId, extension.id), wanted ) if (plan.kind === 'invalid') { @@ -161,7 +115,7 @@ async function remove( const { extension, browserProfileId } = caller const plan = planBrowserExtensionPermissionRemoval( extension.manifest, - readGrants(browserProfileId, extension.id), + readBrowserExtensionProfileGrants(browserProfileId, extension.id), wanted ) if (plan.kind === 'invalid') { @@ -178,21 +132,23 @@ async function remove( export function registerPermissionsNamespace(): void { setBrowserExtensionGrantedPermissionsLookup((extension, session) => { const profileId = getBrowserExtensionProfileIdForSession(session) - return profileId ? readGrants(profileId, extension.id).grantedPermissions : [] + return profileId + ? readBrowserExtensionProfileGrants(profileId, extension.id).grantedPermissions + : [] }) registerBrowserExtensionNamespace('permissions', { methods: { contains: defineBrowserExtensionMethod(z.tuple([permissionsSchema]), (caller, wanted) => containsBrowserExtensionPermissions( caller.extension.manifest, - readGrants(caller.browserProfileId, caller.extension.id), + readBrowserExtensionProfileGrants(caller.browserProfileId, caller.extension.id), wanted ) ), getAll: defineBrowserExtensionMethod(z.tuple([]), (caller) => effectiveBrowserExtensionPermissions( caller.extension.manifest, - readGrants(caller.browserProfileId, caller.extension.id) + readBrowserExtensionProfileGrants(caller.browserProfileId, caller.extension.id) ) ), request: defineBrowserExtensionMethod(z.tuple([permissionsSchema]), request), diff --git a/src/main/browser/extensions/api/runtime.ts b/src/main/browser/extensions/api/runtime.ts index 0c7d9c29919..4b0f8d16974 100644 --- a/src/main/browser/extensions/api/runtime.ts +++ b/src/main/browser/extensions/api/runtime.ts @@ -1,31 +1,9 @@ +import { openBrowserExtensionOptionsPage } from '../tabs-and-windows/browser-extension-options-page' import { z } from 'zod' -import { getChromeExtensionOrigin } from '../../../../shared/browser/extensions/browser-extension-url' import { - BrowserExtensionApiError, defineBrowserExtensionMethod, registerBrowserExtensionNamespace } from '../api-runtime/browser-extension-namespace-registry' -import { openBrowserExtensionTab } from '../tabs-and-windows/browser-extension-tab-model' - -function optionsPagePath(extension: Electron.Extension): string | null { - const manifest: { options_ui?: { page?: unknown }; options_page?: unknown } = - extension.manifest ?? {} - const page = manifest.options_ui?.page ?? manifest.options_page - return typeof page === 'string' && page.length > 0 ? page : null -} - -/** Opens the extension's options page as an Orca tab in the given browser profile. */ -export async function openBrowserExtensionOptionsPage( - browserProfileId: string, - extension: Electron.Extension -): Promise { - const page = optionsPagePath(extension) - if (!page) { - throw new BrowserExtensionApiError('This extension has no options page') - } - const url = new URL(page.replace(/^\/+/, ''), `${getChromeExtensionOrigin(extension.id)}/`) - await openBrowserExtensionTab(browserProfileId, url.toString(), true) -} /** Only what Electron leaves unimplemented (lifecycle events: runtime-lifecycle); the rest is native. */ export function registerRuntimeNamespace(): void { diff --git a/src/main/browser/extensions/api/storage-sync.ts b/src/main/browser/extensions/api/storage-sync.ts index 16fc39273c2..17862c39604 100644 --- a/src/main/browser/extensions/api/storage-sync.ts +++ b/src/main/browser/extensions/api/storage-sync.ts @@ -6,10 +6,8 @@ import { registerBrowserExtensionNamespace, type BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' -import { - getBrowserExtensionSyncStorageStore, - type BrowserExtensionSyncItems -} from '../profile-storage/browser-extension-sync-storage-store' +import { getBrowserExtensionSyncStorageStore } from '../profile-storage/browser-extension-sync-storage-store' +import type { BrowserExtensionProfileDocumentItems } from '../profile-storage/browser-extension-profile-document-store' /** * Electron disables chrome.storage.sync ("not available in this instance of Chrome"), so Orca @@ -50,7 +48,7 @@ function itemBytes(key: string, value: unknown): number { return Buffer.byteLength(key) + Buffer.byteLength(JSON.stringify(value)) } -function bytesInUse(items: BrowserExtensionSyncItems, keys: string[]): number { +function bytesInUse(items: BrowserExtensionProfileDocumentItems, keys: string[]): number { return keys.reduce( (total, key) => (Object.hasOwn(items, key) ? total + itemBytes(key, items[key]) : total), 0 @@ -65,15 +63,15 @@ function sameValue(left: unknown, right: unknown): boolean { return JSON.stringify(left) === JSON.stringify(right) } -function readItems(caller: BrowserExtensionCaller): BrowserExtensionSyncItems { +function readItems(caller: BrowserExtensionCaller): BrowserExtensionProfileDocumentItems { return getBrowserExtensionSyncStorageStore().read(caller.browserProfileId, caller.extension.id) } /** Saves `next`; returns and announces the changes for every key whose value differs. */ function commit( caller: BrowserExtensionCaller, - current: BrowserExtensionSyncItems, - next: BrowserExtensionSyncItems, + current: BrowserExtensionProfileDocumentItems, + next: BrowserExtensionProfileDocumentItems, keys: Iterable ): StorageChanges { const changes: StorageChanges = {} @@ -103,7 +101,7 @@ function commit( function get( caller: BrowserExtensionCaller, keys: z.infer -): BrowserExtensionSyncItems { +): BrowserExtensionProfileDocumentItems { const items = readItems(caller) if (keys == null) { return { ...items } @@ -113,7 +111,7 @@ function get( [keys].flat().flatMap((key) => (Object.hasOwn(items, key) ? [[key, items[key]]] : [])) ) } - const result: BrowserExtensionSyncItems = {} + const result: BrowserExtensionProfileDocumentItems = {} for (const [key, fallback] of Object.entries(keys)) { const value = Object.hasOwn(items, key) ? items[key] : toStoredValue(fallback) if (value !== undefined) { @@ -123,7 +121,10 @@ function get( return result } -function set(caller: BrowserExtensionCaller, input: BrowserExtensionSyncItems): StorageChanges { +function set( + caller: BrowserExtensionCaller, + input: BrowserExtensionProfileDocumentItems +): StorageChanges { const current = readItems(caller) const next = { ...current } let total = bytesInUse(current, Object.keys(current)) diff --git a/src/main/browser/extensions/api/tabs-capture.test.ts b/src/main/browser/extensions/api/tabs-capture.test.ts index df386f80fd7..407758f63b6 100644 --- a/src/main/browser/extensions/api/tabs-capture.test.ts +++ b/src/main/browser/extensions/api/tabs-capture.test.ts @@ -28,7 +28,7 @@ vi.mock('../tabs-and-windows/browser-extension-window-model', () => ({ WINDOW_ID_CURRENT: -2, getBrowserExtensionWindow: (id: number) => (mocks.windows.has(id) ? {} : null) })) -vi.mock('./permissions', () => ({ +vi.mock('../permissions/browser-extension-session-permissions', () => ({ browserExtensionHostPatternsInSession: () => mocks.hostPatterns })) vi.mock('../permissions/browser-extension-active-tab', () => ({ diff --git a/src/main/browser/extensions/api/tabs-capture.ts b/src/main/browser/extensions/api/tabs-capture.ts index 0b5386de259..976eeaa7b07 100644 --- a/src/main/browser/extensions/api/tabs-capture.ts +++ b/src/main/browser/extensions/api/tabs-capture.ts @@ -17,7 +17,7 @@ import { WINDOW_ID_CURRENT, getBrowserExtensionWindow } from '../tabs-and-windows/browser-extension-window-model' -import { browserExtensionHostPatternsInSession } from './permissions' +import { browserExtensionHostPatternsInSession } from '../permissions/browser-extension-session-permissions' // Chrome's default JPEG quality for tabs.captureVisibleTab. const DEFAULT_JPEG_QUALITY = 90 diff --git a/src/main/browser/extensions/api/tabs.ts b/src/main/browser/extensions/api/tabs.ts index 04d1e8f34f9..b69233ca4d3 100644 --- a/src/main/browser/extensions/api/tabs.ts +++ b/src/main/browser/extensions/api/tabs.ts @@ -1,6 +1,7 @@ /// +import { requireOpenableUrl } from '../tabs-and-windows/browser-extension-requested-navigation' + import { z } from 'zod' -import { resolveBrowserExtensionRequestedUrl } from '../../../../shared/browser/extensions/browser-extension-url' import { BrowserExtensionApiError, defineBrowserExtensionMethod, @@ -76,22 +77,6 @@ const updatePropertiesSchema = z .partial() .loose() -/** Resolves an extension-requested URL through Orca's rules; extension pages must be loaded in `sess`. */ -export function requireOpenableUrl( - caller: BrowserExtensionCaller, - rawUrl: string | undefined, - sess: Electron.Session -): string { - const resolved = resolveBrowserExtensionRequestedUrl(rawUrl, caller.extension.id) - if ( - !resolved || - (resolved.kind === 'extension' && !sess.extensions.getExtension(resolved.extensionId)) - ) { - throw new BrowserExtensionApiError(`Cannot open URL: ${rawUrl ?? ''}`) - } - return resolved.url -} - async function createTab( caller: BrowserExtensionCaller, url: string, diff --git a/src/main/browser/extensions/api/web-navigation.ts b/src/main/browser/extensions/api/web-navigation.ts index 50959520698..d9b28b16b79 100644 --- a/src/main/browser/extensions/api/web-navigation.ts +++ b/src/main/browser/extensions/api/web-navigation.ts @@ -1,4 +1,9 @@ /// +import { + chromeFrameId, + chromeFrameDocument +} from '../tabs-and-windows/browser-extension-frame-projection' + import { webFrameMain } from 'electron' import { z } from 'zod' import { emitBrowserExtensionEvent } from '../api-runtime/browser-extension-api-host' @@ -19,32 +24,6 @@ import { resolveBrowserExtensionTab } from '../tabs-and-windows/browser-extension-tab-model' -/** - * Chrome frame ids: 0 for a tab's main frame, the frame-tree-node id otherwise. Electron's own - * content-script `sender.frameId` uses the same numbering, so tabs.sendMessage targets line up. - */ -export function chromeFrameId(frame: Electron.WebFrameMain): number { - return frame.parent ? frame.frameTreeNodeId : 0 -} - -function chromeParentFrameId(frame: Electron.WebFrameMain): number { - return frame.parent ? chromeFrameId(frame.parent) : -1 -} - -/** Chrome's frame fields; `documentId` is left out where the document does not exist yet. */ -export function chromeFrameDocument(frame: Electron.WebFrameMain, withDocument: boolean) { - return { - frameId: chromeFrameId(frame), - parentFrameId: chromeParentFrameId(frame), - ...(withDocument ? { documentId: getBrowserExtensionDocumentId(frame.frameTreeNodeId) } : {}), - ...(frame.parent - ? { parentDocumentId: getBrowserExtensionDocumentId(frame.parent.frameTreeNodeId) } - : {}), - frameType: frame.parent ? ('sub_frame' as const) : ('outermost_frame' as const), - documentLifecycle: 'active' as const - } -} - function frameDetails( tabId: number, frame: Electron.WebFrameMain, diff --git a/src/main/browser/extensions/api/windows.ts b/src/main/browser/extensions/api/windows.ts index dec336d5512..dcf3834777f 100644 --- a/src/main/browser/extensions/api/windows.ts +++ b/src/main/browser/extensions/api/windows.ts @@ -21,7 +21,7 @@ import { presentBrowserExtensionWindow, type BrowserExtensionWindowEntry } from '../tabs-and-windows/browser-extension-window-model' -import { requireOpenableUrl } from './tabs' +import { requireOpenableUrl } from '../tabs-and-windows/browser-extension-requested-navigation' const windowIdSchema = z.number().int() const queryOptionsSchema = z diff --git a/src/main/browser/extensions/browser-extension-management.test.ts b/src/main/browser/extensions/browser-extension-management.test.ts index 356326785cd..f93accf963a 100644 --- a/src/main/browser/extensions/browser-extension-management.test.ts +++ b/src/main/browser/extensions/browser-extension-management.test.ts @@ -23,7 +23,9 @@ vi.mock('../sessions/browser-session-registry', () => ({ getProfile: () => ({ id: 'default' }) } })) -vi.mock('./api/runtime', () => ({ openBrowserExtensionOptionsPage: vi.fn() })) +vi.mock('./tabs-and-windows/browser-extension-options-page', () => ({ + openBrowserExtensionOptionsPage: vi.fn() +})) vi.mock('./installation/browser-extension-install-store', () => ({ getBrowserExtensionInstallStore: () => mocks.store })) diff --git a/src/main/browser/extensions/browser-extension-management.ts b/src/main/browser/extensions/browser-extension-management.ts index c6ef6cfdec6..31267be9a85 100644 --- a/src/main/browser/extensions/browser-extension-management.ts +++ b/src/main/browser/extensions/browser-extension-management.ts @@ -5,7 +5,7 @@ import type { InstalledBrowserExtension } from '../../../shared/browser/extensions/browser-extension-types' import { browserSessionRegistry } from '../sessions/browser-session-registry' -import { openBrowserExtensionOptionsPage } from './api/runtime' +import { openBrowserExtensionOptionsPage } from './tabs-and-windows/browser-extension-options-page' import { getBrowserExtensionInstallStore } from './installation/browser-extension-install-store' import { reloadBrowserExtensionWhereEnabled } from './profile-sessions/browser-extension-profile-reload' import { @@ -32,7 +32,7 @@ import { } from './profile-sessions/browser-extension-load-errors' import { notifyBrowserExtensionListChanged } from './installation/browser-extension-list-changes' import { reviewHeldBrowserExtensionUpdate } from './web-store/browser-extension-webstore-updater' -import { withBrowserExtensionLock } from './installation/browser-extension-webstore-install' +import { withBrowserExtensionLock } from './installation/browser-extension-mutation-lock' import { isBrowserExtensionSupportEnabled } from './browser-extension-support' import { withExtensionEnabled, diff --git a/src/main/browser/extensions/browser-extension-startup.ts b/src/main/browser/extensions/browser-extension-startup.ts index 340d29d7245..6a740be8901 100644 --- a/src/main/browser/extensions/browser-extension-startup.ts +++ b/src/main/browser/extensions/browser-extension-startup.ts @@ -17,7 +17,7 @@ import { } from './profile-sessions/browser-extension-session-loader' import { notifyBrowserExtensionActionsChanged } from './actions/browser-extension-action-registry' import { registerBrowserExtensionNamespaces } from './register-browser-extension-namespaces' -import { observeBrowserExtensionTabLifecycle } from './api/tabs-lifecycle' +import { observeBrowserExtensionTabLifecycle } from './tabs-and-windows/browser-extension-tab-lifecycle' import { observeBrowserExtensionWebNavigation } from './api/web-navigation' import { observeBrowserExtensionCommands } from './api/commands' import { observeBrowserExtensionDnrContents } from './declarative-net-request/session/browser-extension-dnr-contents' diff --git a/src/main/browser/extensions/browser-extension-web-request.ts b/src/main/browser/extensions/browser-extension-web-request.ts deleted file mode 100644 index 43df3da9131..00000000000 --- a/src/main/browser/extensions/browser-extension-web-request.ts +++ /dev/null @@ -1,305 +0,0 @@ -import { CHROME_WEB_STORE_ORIGIN } from '../../../shared/browser/extensions/browser-extension-webstore' -import { - installBrowserSessionHeadersReceived, - installBrowserSessionRequestLifecycle, - removeBrowserSessionHeadersReceived, - removeBrowserSessionRequestLifecycle -} from '../sessions/requests/browser-session-request-decisions' -import { - setBrowserSessionWebRequestObserver, - type ObservedWebRequestDetails, - type ObservedWebRequestEvent -} from '../sessions/requests/browser-session-web-request-observer' -import { chromeFrameDocument } from './api/web-navigation' -import { browserExtensionHostPatternsInSession } from './api/permissions' -import { - emitBrowserExtensionEvent, - getBrowserExtensionProfileIdForSession -} from './api-runtime/browser-extension-api-host' -import { browserExtensionEventHasListener } from './api-runtime/browser-extension-contexts' -import { matchesExtensionMatchPattern } from './permissions/browser-extension-match-pattern' -import { extensionHasPermission } from './api-runtime/browser-extension-namespace-registry' -import { hasBrowserExtensionHostPermission } from './permissions/browser-extension-permission-grants' -import { canExtensionAccessRequest } from './permissions/browser-extension-request-host-access' -import { resolveBrowserExtensionTab } from './tabs-and-windows/browser-extension-tab-model' - -/** - * chrome.webRequest, observed rather than blocking. Electron 43 delivers no webRequest events to - * MV3 workers, and Orca's own session handlers switch Electron's extension webRequest off anyway, - * so Orca forwards what its handlers see. MV3 store extensions cannot block requests in Chrome - * either (webRequestBlocking is policy-only), so nothing they could do is lost. - */ - -type ChromeResourceType = - | 'main_frame' - | 'sub_frame' - | 'stylesheet' - | 'script' - | 'image' - | 'font' - | 'object' - | 'xmlhttprequest' - | 'ping' - | 'csp_report' - | 'media' - | 'websocket' - | 'other' - -const CHROME_RESOURCE_TYPES = new Map([ - ['mainFrame', 'main_frame'], - ['subFrame', 'sub_frame'], - ['stylesheet', 'stylesheet'], - ['script', 'script'], - ['image', 'image'], - ['font', 'font'], - ['object', 'object'], - ['xhr', 'xmlhttprequest'], - ['ping', 'ping'], - ['cspReport', 'csp_report'], - ['media', 'media'], - ['webSocket', 'websocket'] -]) - -/** Electron's resourceType → Chrome's, shared by webRequest and declarativeNetRequest. */ -export function chromeResourceType(resourceType: string): ChromeResourceType { - return CHROME_RESOURCE_TYPES.get(resourceType) ?? 'other' -} - -// Chrome hides these unless a listener asks for 'extraHeaders'. -const HIDDEN_REQUEST_HEADERS = new Set(['accept-language', 'accept-encoding', 'referer', 'cookie']) -const HIDDEN_RESPONSE_HEADERS = new Set(['set-cookie']) - -export type ChromeWebRequestDetails = { - requestId: string - url: string - method: string - frameId: number - parentFrameId: number - tabId: number - windowId: number - type: string - timeStamp: number - frameType?: 'outermost_frame' | 'sub_frame' - documentId?: string - parentDocumentId?: string - documentLifecycle?: 'active' - requestHeaders?: { name: string; value: string }[] - responseHeaders?: { name: string; value: string }[] - statusCode?: number - statusLine?: string - fromCache?: boolean - ip?: string - redirectUrl?: string - error?: string -} - -function requestHeaderList(headers: Record): { name: string; value: string }[] { - return Object.entries(headers) - .filter(([name]) => !HIDDEN_REQUEST_HEADERS.has(name.toLowerCase())) - .map(([name, value]) => ({ name, value })) -} - -function responseHeaderList(headers: Record): { name: string; value: string }[] { - return Object.entries(headers) - .filter(([name]) => !HIDDEN_RESPONSE_HEADERS.has(name.toLowerCase())) - .flatMap(([name, values]) => values.map((value) => ({ name, value }))) -} - -type WebRequestFrameFields = Pick< - ChromeWebRequestDetails, - | 'frameId' - | 'parentFrameId' - | 'frameType' - | 'documentId' - | 'parentDocumentId' - | 'documentLifecycle' -> - -/** A navigation request has no document yet; any other request belongs to its frame's current one. */ -function frameFields(details: ObservedWebRequestDetails): WebRequestFrameFields { - if (details.resourceType === 'mainFrame') { - return { - frameId: 0, - parentFrameId: -1, - frameType: 'outermost_frame', - documentLifecycle: 'active' - } - } - const frame = details.frame && !details.frame.isDestroyed() ? details.frame : null - return frame - ? chromeFrameDocument(frame, details.resourceType !== 'subFrame') - : { frameId: 0, parentFrameId: -1 } -} - -export function toChromeWebRequestDetails( - details: ObservedWebRequestDetails, - tab: { tabId: number; windowId: number }, - frame: WebRequestFrameFields -): ChromeWebRequestDetails { - const type = chromeResourceType(details.resourceType) - return { - requestId: String(details.id), - url: details.url, - method: details.method, - ...frame, - ...tab, - type, - timeStamp: details.timestamp, - ...(details.requestHeaders - ? { requestHeaders: requestHeaderList(details.requestHeaders) } - : {}), - ...(details.responseHeaders - ? { responseHeaders: responseHeaderList(details.responseHeaders) } - : {}), - ...(details.statusCode !== undefined ? { statusCode: details.statusCode } : {}), - ...(details.statusLine !== undefined ? { statusLine: details.statusLine } : {}), - ...(details.fromCache !== undefined ? { fromCache: details.fromCache } : {}), - ...(details.ip ? { ip: details.ip } : {}), - ...(details.redirectURL ? { redirectUrl: details.redirectURL } : {}), - ...(details.error ? { error: details.error } : {}) - } -} - -/** Main-side pre-filter against one listener's chrome.webRequest.RequestFilter. */ -export function webRequestFilterMatches( - filter: unknown, - details: ChromeWebRequestDetails -): boolean { - if (filter === null || typeof filter !== 'object') { - return true - } - const fields = new Map(Object.entries(filter)) - const urls = fields.get('urls') - if ( - Array.isArray(urls) && - !urls.some( - (pattern) => typeof pattern === 'string' && matchesExtensionMatchPattern(pattern, details.url) - ) - ) { - return false - } - const types = fields.get('types') - if (Array.isArray(types) && !types.includes(details.type)) { - return false - } - const tabId = fields.get('tabId') - const windowId = fields.get('windowId') - return ( - (typeof tabId !== 'number' || tabId === details.tabId) && - (typeof windowId !== 'number' || windowId === details.windowId) - ) -} - -const WEB_URL = /^(?:https?|wss?):/i - -/** WebRequestPermissions::HideRequest: requests extensions never see or act on. */ -export function isRequestHiddenFromExtensions(details: { - url: string - webContentsId?: number - initiatorOrigin?: string -}): boolean { - if (!WEB_URL.test(details.url)) { - return true - } - // Orca's own main-process requests carry neither a webContents nor an initiator. - if (details.webContentsId === undefined && details.initiatorOrigin === undefined) { - return true - } - return ( - details.url.startsWith(`${CHROME_WEB_STORE_ORIGIN}/`) || - details.url.startsWith('https://chrome.google.com/webstore') - ) -} - -function forward( - session: Electron.Session, - event: ObservedWebRequestEvent, - details: ObservedWebRequestDetails -): void { - if ( - isRequestHiddenFromExtensions(details) || - !getBrowserExtensionProfileIdForSession(session) || - !browserExtensionEventHasListener(session, 'webRequest', event) - ) { - return - } - const entry = - details.webContentsId === undefined ? null : resolveBrowserExtensionTab(details.webContentsId) - const chromeDetails = toChromeWebRequestDetails( - details, - entry ? { tabId: entry.contents.id, windowId: entry.windowId } : { tabId: -1, windowId: -1 }, - frameFields(details) - ) - const request = { - url: details.url, - initiator: details.initiatorOrigin ?? null, - tabId: chromeDetails.tabId, - type: chromeDetails.type - } - emitBrowserExtensionEvent( - { session }, - 'webRequest', - event, - (extension) => { - const patterns = browserExtensionHostPatternsInSession(extension, session) - return canExtensionAccessRequest(extension.id, request, (url) => - hasBrowserExtensionHostPermission(patterns, url) - ) - ? [chromeDetails] - : null - }, - (filter) => webRequestFilterMatches(filter, chromeDetails) - ) -} - -const observedSessions = new WeakSet() -const watchedSessions = new WeakSet() -const OBSERVE_ALL = { urls: ['http://*/*', 'https://*/*', 'ws://*/*', 'wss://*/*'] } - -/** The events Orca does not otherwise handle, installed once a webRequest extension loads. */ -function observeRemainingEvents(session: Electron.Session): void { - if (observedSessions.has(session)) { - return - } - observedSessions.add(session) - const { webRequest } = session - webRequest.onSendHeaders(OBSERVE_ALL, (details) => forward(session, 'onSendHeaders', details)) - installBrowserSessionHeadersReceived(session) - webRequest.onResponseStarted(OBSERVE_ALL, (details) => - forward(session, 'onResponseStarted', details) - ) - webRequest.onBeforeRedirect(OBSERVE_ALL, (details) => - forward(session, 'onBeforeRedirect', details) - ) - installBrowserSessionRequestLifecycle(session) -} - -export function observeBrowserExtensionWebRequests(session: Electron.Session): void { - const maybeObserve = (extension: Electron.Extension): void => { - if (extensionHasPermission(extension, 'webRequest', session)) { - observeRemainingEvents(session) - } - } - if (!watchedSessions.has(session)) { - watchedSessions.add(session) - session.extensions.on('extension-ready', (_event, extension) => maybeObserve(extension)) - } - session.extensions.getAllExtensions().forEach(maybeObserve) -} - -/** Extensions turned off: hand the session's request path back to Orca's own handlers alone. */ -export function stopObservingBrowserExtensionWebRequests(session: Electron.Session): void { - removeBrowserSessionHeadersReceived(session) - removeBrowserSessionRequestLifecycle(session) - if (!observedSessions.delete(session)) { - return - } - const { webRequest } = session - webRequest.onSendHeaders(null) - webRequest.onResponseStarted(null) - webRequest.onBeforeRedirect(null) -} - -export function installBrowserExtensionWebRequestForwarding(): void { - setBrowserSessionWebRequestObserver(forward) -} diff --git a/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.test.ts b/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.test.ts index ee17628fa2e..47ef817d418 100644 --- a/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.test.ts +++ b/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.test.ts @@ -7,8 +7,12 @@ const mocks = vi.hoisted(() => ({ })) const VAULT = 'a'.repeat(32) -vi.mock('../api/web-navigation', () => ({ chromeFrameId: () => 0 })) -vi.mock('../api/permissions', () => ({ noteBrowserExtensionUserGesture: vi.fn() })) +vi.mock('../tabs-and-windows/browser-extension-frame-projection', () => ({ + chromeFrameId: () => 0 +})) +vi.mock('../permissions/browser-extension-user-gesture', () => ({ + noteBrowserExtensionUserGesture: vi.fn() +})) vi.mock('../api-runtime/browser-extension-api-host', () => ({ emitBrowserExtensionEvent: mocks.emit, getBrowserExtensionProfileIdForSession: () => 'default' @@ -37,7 +41,6 @@ vi.mock('../tabs-and-windows/browser-extension-tab-model', () => ({ resolveBrowserExtensionTab: () => null, describeBrowserExtensionTab: vi.fn() })) - import { createContextMenuItem } from './browser-extension-context-menu-model' import * as menuHost from './browser-extension-context-menu-host' diff --git a/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.ts b/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.ts index 4e748f9d52c..892d6d346a7 100644 --- a/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.ts +++ b/src/main/browser/extensions/context-menus/browser-extension-context-menu-host.ts @@ -4,8 +4,8 @@ import type { BrowserExtensionContextMenuGroup, BrowserExtensionContextMenuPayload } from '../../../../shared/browser/extensions/browser-extension-context-menu-types' -import { chromeFrameId } from '../api/web-navigation' -import { noteBrowserExtensionUserGesture } from '../api/permissions' +import { chromeFrameId } from '../tabs-and-windows/browser-extension-frame-projection' +import { noteBrowserExtensionUserGesture } from '../permissions/browser-extension-user-gesture' import { emitBrowserExtensionEvent, getBrowserExtensionProfileIdForSession diff --git a/src/main/browser/extensions/declarative-net-request/engine-stages.test.ts b/src/main/browser/extensions/declarative-net-request/engine-stages.test.ts index 7d1b9380cba..11daab5e627 100644 --- a/src/main/browser/extensions/declarative-net-request/engine-stages.test.ts +++ b/src/main/browser/extensions/declarative-net-request/engine-stages.test.ts @@ -1,10 +1,10 @@ import { describe, expect, it } from 'vitest' import { createDnrRequestState } from './request-state' import { DnrEngine } from './engine' -import { applyDnrRequestHeaderActions } from './header-modification' +import { applyDnrRequestHeaderActions } from './header-actions/header-modification' import type { DnrRequestInfo } from './request-context' -import type { DnrRule } from './rule-types' -import { buildDnrRuleset } from './ruleset-builder' +import type { DnrRule } from './rule-compilation/rule-types' +import { buildDnrRuleset } from './rule-compilation/ruleset-builder' const OLD = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const NEW = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' diff --git a/src/main/browser/extensions/declarative-net-request/engine.test.ts b/src/main/browser/extensions/declarative-net-request/engine.test.ts index 7caf03bf64a..cea3c2e0cc5 100644 --- a/src/main/browser/extensions/declarative-net-request/engine.test.ts +++ b/src/main/browser/extensions/declarative-net-request/engine.test.ts @@ -2,8 +2,8 @@ import { describe, expect, it } from 'vitest' import { createDnrRequestState } from './request-state' import { DnrEngine, type DnrEvaluateOptions } from './engine' import type { DnrRequestInfo } from './request-context' -import type { DnrRule, DnrRulesetSource } from './rule-types' -import { buildDnrRuleset } from './ruleset-builder' +import type { DnrRule, DnrRulesetSource } from './rule-compilation/rule-types' +import { buildDnrRuleset } from './rule-compilation/ruleset-builder' const OLD = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' diff --git a/src/main/browser/extensions/declarative-net-request/engine.ts b/src/main/browser/extensions/declarative-net-request/engine.ts index 4e7bb97ac82..66431fb0d7e 100644 --- a/src/main/browser/extensions/declarative-net-request/engine.ts +++ b/src/main/browser/extensions/declarative-net-request/engine.ts @@ -2,8 +2,8 @@ import { DnrExtensionMatcher, type DnrFrameAllowState, type DnrHostAccess -} from './extension-matcher' -import { sameDomainOrHost } from './domain-matching' +} from './matching/extension-matcher' +import { sameDomainOrHost } from './matching/domain-matching' import { createDnrRequestContext, dnrOriginHost, @@ -11,8 +11,8 @@ import { type DnrRequestInfo, type DnrResponseHeaders } from './request-context' -import { DnrCandidatePostings } from './candidate-postings' -import type { DnrRulesetIndex } from './ruleset-index' +import { DnrCandidatePostings } from './indexing/candidate-postings' +import type { DnrRulesetIndex } from './indexing/ruleset-index' import { crossExtensionRank, emptyDnrEvaluation, @@ -25,8 +25,8 @@ import { DnrRulesetMatcher, type DnrRequestAction, type DnrStage -} from './ruleset-matcher' -import type { DnrRulesetSource } from './rule-types' +} from './matching/ruleset-matcher' +import type { DnrRulesetSource } from './rule-compilation/rule-types' // RulesetManager (ruleset_manager.cc) plus the per-request bookkeeping WebRequestInfo keeps // between stages (see request-state.ts). diff --git a/src/main/browser/extensions/declarative-net-request/header-modification.ts b/src/main/browser/extensions/declarative-net-request/header-actions/header-modification.ts similarity index 98% rename from src/main/browser/extensions/declarative-net-request/header-modification.ts rename to src/main/browser/extensions/declarative-net-request/header-actions/header-modification.ts index a6591cb454f..e9f11134eab 100644 --- a/src/main/browser/extensions/declarative-net-request/header-modification.ts +++ b/src/main/browser/extensions/declarative-net-request/header-actions/header-modification.ts @@ -1,5 +1,5 @@ import { REQUEST_HEADER_APPEND_DELIMITERS, type DnrHeaderModification } from './header-rules' -import type { DnrRequestAction } from './ruleset-matcher' +import type { DnrRequestAction } from '../matching/ruleset-matcher' // Applies modifyHeaders actions the way web_request_api_helpers.cc does // (ModifyRequestHeadersForAction, ModifyResponseHeadersForAction, DNRHeaderAction). diff --git a/src/main/browser/extensions/declarative-net-request/header-rules.ts b/src/main/browser/extensions/declarative-net-request/header-actions/header-rules.ts similarity index 94% rename from src/main/browser/extensions/declarative-net-request/header-rules.ts rename to src/main/browser/extensions/declarative-net-request/header-actions/header-rules.ts index c3c9ec95050..090f01a21f3 100644 --- a/src/main/browser/extensions/declarative-net-request/header-rules.ts +++ b/src/main/browser/extensions/declarative-net-request/header-actions/header-rules.ts @@ -1,6 +1,10 @@ -import { dnrRuleProblem, type DnrRuleProblem } from './parse-errors' -import type { DnrHeaderInfo, DnrHeaderOperation, DnrModifyHeaderInfo } from './rule-types' -import { toLowerAscii } from './url-text' +import { dnrRuleProblem, type DnrRuleProblem } from '../rule-compilation/parse-errors' +import type { + DnrHeaderInfo, + DnrHeaderOperation, + DnrModifyHeaderInfo +} from '../rule-compilation/rule-types' +import { toLowerAscii } from '../matching/url-text' // Header validation from indexed_rule.cc (ValidateHeadersForModification, // ValidateResponseHeadersForMatching) and the request-header append allowlist with each diff --git a/src/main/browser/extensions/declarative-net-request/candidate-postings.ts b/src/main/browser/extensions/declarative-net-request/indexing/candidate-postings.ts similarity index 98% rename from src/main/browser/extensions/declarative-net-request/candidate-postings.ts rename to src/main/browser/extensions/declarative-net-request/indexing/candidate-postings.ts index 56a141cf882..040249be382 100644 --- a/src/main/browser/extensions/declarative-net-request/candidate-postings.ts +++ b/src/main/browser/extensions/declarative-net-request/indexing/candidate-postings.ts @@ -1,4 +1,4 @@ -import type { DnrRequestContext } from './request-context' +import type { DnrRequestContext } from '../request-context' import type { DnrRulePartition, DnrRulesetIndex } from './ruleset-index' // Engine-wide inverted index over every attached ruleset: n-gram id or domain -> the partitions diff --git a/src/main/browser/extensions/declarative-net-request/ngram-dictionary.ts b/src/main/browser/extensions/declarative-net-request/indexing/ngram-dictionary.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/ngram-dictionary.ts rename to src/main/browser/extensions/declarative-net-request/indexing/ngram-dictionary.ts diff --git a/src/main/browser/extensions/declarative-net-request/ruleset-index.ts b/src/main/browser/extensions/declarative-net-request/indexing/ruleset-index.ts similarity index 96% rename from src/main/browser/extensions/declarative-net-request/ruleset-index.ts rename to src/main/browser/extensions/declarative-net-request/indexing/ruleset-index.ts index 93200a76ef4..d022f52d441 100644 --- a/src/main/browser/extensions/declarative-net-request/ruleset-index.ts +++ b/src/main/browser/extensions/declarative-net-request/indexing/ruleset-index.ts @@ -1,8 +1,8 @@ -import { hostLabelSuffixes, type DnrDomainList } from './domain-matching' -import type { DnrIndexedRule } from './indexed-rule' +import { hostLabelSuffixes, type DnrDomainList } from '../matching/domain-matching' +import type { DnrIndexedRule } from '../rule-compilation/indexed-rule' import { dnrNgramDictionary, patternNgrams } from './ngram-dictionary' -import type { DnrRequestContext } from './request-context' -import { toLowerAscii } from './url-text' +import type { DnrRequestContext } from '../request-context' +import { toLowerAscii } from '../matching/url-text' // One url_pattern_index (url_pattern_index.cc UrlPatternIndexBuilder): rules keyed by a 5-gram // of their filter. Rules without one are keyed by an included request or initiator domain, diff --git a/src/main/browser/extensions/declarative-net-request/domain-matching.ts b/src/main/browser/extensions/declarative-net-request/matching/domain-matching.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/domain-matching.ts rename to src/main/browser/extensions/declarative-net-request/matching/domain-matching.ts diff --git a/src/main/browser/extensions/declarative-net-request/extension-matcher.ts b/src/main/browser/extensions/declarative-net-request/matching/extension-matcher.ts similarity index 98% rename from src/main/browser/extensions/declarative-net-request/extension-matcher.ts rename to src/main/browser/extensions/declarative-net-request/matching/extension-matcher.ts index da2b2371b35..8e38a49a07c 100644 --- a/src/main/browser/extensions/declarative-net-request/extension-matcher.ts +++ b/src/main/browser/extensions/declarative-net-request/matching/extension-matcher.ts @@ -1,4 +1,4 @@ -import type { DnrRequestContext } from './request-context' +import type { DnrRequestContext } from '../request-context' import { compareActions, isAllowAction, diff --git a/src/main/browser/extensions/declarative-net-request/rule-matching.ts b/src/main/browser/extensions/declarative-net-request/matching/rule-matching.ts similarity index 95% rename from src/main/browser/extensions/declarative-net-request/rule-matching.ts rename to src/main/browser/extensions/declarative-net-request/matching/rule-matching.ts index 5ec676d9cfa..fea70d5dd5e 100644 --- a/src/main/browser/extensions/declarative-net-request/rule-matching.ts +++ b/src/main/browser/extensions/declarative-net-request/matching/rule-matching.ts @@ -1,7 +1,7 @@ import { hostMatchesDomainLists } from './domain-matching' -import type { DnrHeaderCondition } from './header-rules' -import type { DnrIndexedRule } from './indexed-rule' -import type { DnrRequestContext } from './request-context' +import type { DnrHeaderCondition } from '../header-actions/header-rules' +import type { DnrIndexedRule } from '../rule-compilation/indexed-rule' +import type { DnrRequestContext } from '../request-context' // Per-rule checks from url_pattern_index.cc (DoesRuleFlagsMatch, DoesOriginMatchInitiatorDomainList, // DoesURLMatchRequestDomainList) and request_params.cc DoEmbedderConditionsMatch. diff --git a/src/main/browser/extensions/declarative-net-request/ruleset-matcher.ts b/src/main/browser/extensions/declarative-net-request/matching/ruleset-matcher.ts similarity index 92% rename from src/main/browser/extensions/declarative-net-request/ruleset-matcher.ts rename to src/main/browser/extensions/declarative-net-request/matching/ruleset-matcher.ts index 8cf3f82bf9e..3d49c2f2c51 100644 --- a/src/main/browser/extensions/declarative-net-request/ruleset-matcher.ts +++ b/src/main/browser/extensions/declarative-net-request/matching/ruleset-matcher.ts @@ -1,13 +1,17 @@ -import type { DnrHeaderModification } from './header-rules' -import type { DnrIndexedRule } from './indexed-rule' -import { transformDnrUrl, upgradeDnrUrl } from './redirect-url' -import { applyRegexSubstitution } from './regex-filter' -import { isCollapsibleType, type DnrRequestContext } from './request-context' -import { ELEMENT_TYPE } from './rule-flags' +import type { DnrHeaderModification } from '../header-actions/header-rules' +import type { DnrIndexedRule } from '../rule-compilation/indexed-rule' +import { transformDnrUrl, upgradeDnrUrl } from '../redirect-actions/redirect-url' +import { applyRegexSubstitution } from '../regex/regex-filter' +import { isCollapsibleType, type DnrRequestContext } from '../request-context' +import { ELEMENT_TYPE } from '../rule-compilation/rule-flags' import { regexRuleMetadataMatches, urlRuleMatches } from './rule-matching' -import { compareRulePriority, type DnrRulePartition, type DnrRulesetIndex } from './ruleset-index' -import type { DnrRulesetSource } from './rule-types' -import { dnrPublicRulesetId, dnrRulesetOrdinal } from './rule-types' +import { + compareRulePriority, + type DnrRulePartition, + type DnrRulesetIndex +} from '../indexing/ruleset-index' +import type { DnrRulesetSource } from '../rule-compilation/rule-types' +import { dnrPublicRulesetId, dnrRulesetOrdinal } from '../rule-compilation/rule-types' // One ruleset's matcher: ruleset_matcher.cc, extension_url_pattern_index_matcher.cc, // regex_rules_matcher.cc and ruleset_matcher_base.cc. diff --git a/src/main/browser/extensions/declarative-net-request/url-pattern.test.ts b/src/main/browser/extensions/declarative-net-request/matching/url-pattern.test.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/url-pattern.test.ts rename to src/main/browser/extensions/declarative-net-request/matching/url-pattern.test.ts diff --git a/src/main/browser/extensions/declarative-net-request/url-pattern.ts b/src/main/browser/extensions/declarative-net-request/matching/url-pattern.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/url-pattern.ts rename to src/main/browser/extensions/declarative-net-request/matching/url-pattern.ts diff --git a/src/main/browser/extensions/declarative-net-request/url-text.ts b/src/main/browser/extensions/declarative-net-request/matching/url-text.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/url-text.ts rename to src/main/browser/extensions/declarative-net-request/matching/url-text.ts diff --git a/src/main/browser/extensions/declarative-net-request/redirect-url.test.ts b/src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.test.ts similarity index 94% rename from src/main/browser/extensions/declarative-net-request/redirect-url.test.ts rename to src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.test.ts index cdadd533660..bb32dd224b4 100644 --- a/src/main/browser/extensions/declarative-net-request/redirect-url.test.ts +++ b/src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.test.ts @@ -1,8 +1,8 @@ import { describe, expect, it } from 'vitest' -import { sameDomainOrHost } from './domain-matching' +import { sameDomainOrHost } from '../matching/domain-matching' import { parseDnrRedirect, transformDnrUrl } from './redirect-url' -import { isDnrRuleProblem } from './parse-errors' -import type { DnrUrlTransform } from './rule-types' +import { isDnrRuleProblem } from '../rule-compilation/parse-errors' +import type { DnrUrlTransform } from '../rule-compilation/rule-types' function transform(url: string, spec: DnrUrlTransform): string | null { const parsed = parseDnrRedirect({ transform: spec }, 1, 'ext') diff --git a/src/main/browser/extensions/declarative-net-request/redirect-url.ts b/src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.ts similarity index 98% rename from src/main/browser/extensions/declarative-net-request/redirect-url.ts rename to src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.ts index be584833a37..e3087ffdd7f 100644 --- a/src/main/browser/extensions/declarative-net-request/redirect-url.ts +++ b/src/main/browser/extensions/declarative-net-request/redirect-actions/redirect-url.ts @@ -1,5 +1,5 @@ -import { dnrRuleProblem, type DnrRuleProblem } from './parse-errors' -import type { DnrRedirect, DnrUrlTransform } from './rule-types' +import { dnrRuleProblem, type DnrRuleProblem } from '../rule-compilation/parse-errors' +import type { DnrRedirect, DnrUrlTransform } from '../rule-compilation/rule-types' // Redirect parsing (indexed_rule.cc ParseRedirect/ValidateTransform, flat_ruleset_indexer.cc // BuildTransformOffset) and application (ruleset_matcher_base.cc GetTransformedURL, diff --git a/src/main/browser/extensions/declarative-net-request/regex-char-set.ts b/src/main/browser/extensions/declarative-net-request/regex/regex-char-set.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/regex-char-set.ts rename to src/main/browser/extensions/declarative-net-request/regex/regex-char-set.ts diff --git a/src/main/browser/extensions/declarative-net-request/regex-filter.test.ts b/src/main/browser/extensions/declarative-net-request/regex/regex-filter.test.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/regex-filter.test.ts rename to src/main/browser/extensions/declarative-net-request/regex/regex-filter.test.ts diff --git a/src/main/browser/extensions/declarative-net-request/regex-filter.ts b/src/main/browser/extensions/declarative-net-request/regex/regex-filter.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/regex-filter.ts rename to src/main/browser/extensions/declarative-net-request/regex/regex-filter.ts diff --git a/src/main/browser/extensions/declarative-net-request/regex-piece.ts b/src/main/browser/extensions/declarative-net-request/regex/regex-piece.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/regex-piece.ts rename to src/main/browser/extensions/declarative-net-request/regex/regex-piece.ts diff --git a/src/main/browser/extensions/declarative-net-request/regex-repetition.ts b/src/main/browser/extensions/declarative-net-request/regex/regex-repetition.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/regex-repetition.ts rename to src/main/browser/extensions/declarative-net-request/regex/regex-repetition.ts diff --git a/src/main/browser/extensions/declarative-net-request/regex-translation.ts b/src/main/browser/extensions/declarative-net-request/regex/regex-translation.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/regex-translation.ts rename to src/main/browser/extensions/declarative-net-request/regex/regex-translation.ts diff --git a/src/main/browser/extensions/declarative-net-request/request-context.ts b/src/main/browser/extensions/declarative-net-request/request-context.ts index 9e878bfe10e..5a01843437f 100644 --- a/src/main/browser/extensions/declarative-net-request/request-context.ts +++ b/src/main/browser/extensions/declarative-net-request/request-context.ts @@ -1,9 +1,9 @@ -import { hostLabelSuffixes, sameDomainOrHost } from './domain-matching' -import { dnrNgramDictionary } from './ngram-dictionary' -import { ELEMENT_TYPE, requestMethodBit, resourceTypeBit } from './rule-flags' -import type { DnrResourceType } from './rule-types' -import type { DnrUrlText } from './url-pattern' -import { dnrUrlHost, dnrUrlText, toLowerAscii } from './url-text' +import { hostLabelSuffixes, sameDomainOrHost } from './matching/domain-matching' +import { dnrNgramDictionary } from './indexing/ngram-dictionary' +import { ELEMENT_TYPE, requestMethodBit, resourceTypeBit } from './rule-compilation/rule-flags' +import type { DnrResourceType } from './rule-compilation/rule-types' +import type { DnrUrlText } from './matching/url-pattern' +import { dnrUrlHost, dnrUrlText, toLowerAscii } from './matching/url-text' // Everything a rule looks at for one request, computed once and shared by every ruleset // (request_params.cc RequestParams). diff --git a/src/main/browser/extensions/declarative-net-request/request-state.ts b/src/main/browser/extensions/declarative-net-request/request-state.ts index 2b17dc9e628..f29eb39df77 100644 --- a/src/main/browser/extensions/declarative-net-request/request-state.ts +++ b/src/main/browser/extensions/declarative-net-request/request-state.ts @@ -1,4 +1,4 @@ -import { compareActions, isAllowAction, type DnrRequestAction } from './ruleset-matcher' +import { compareActions, isAllowAction, type DnrRequestAction } from './matching/ruleset-matcher' // What WebRequestInfo carries for one request between onBeforeRequest and onHeadersReceived // (dnr_actions, max_priority_allow_action), and the event router's merge between the stages diff --git a/src/main/browser/extensions/declarative-net-request/indexed-rule.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/indexed-rule.ts similarity index 96% rename from src/main/browser/extensions/declarative-net-request/indexed-rule.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/indexed-rule.ts index 0e8898e6599..b935d4f1872 100644 --- a/src/main/browser/extensions/declarative-net-request/indexed-rule.ts +++ b/src/main/browser/extensions/declarative-net-request/rule-compilation/indexed-rule.ts @@ -3,10 +3,10 @@ import { parseResponseHeaderConditions, type DnrHeaderCondition, type DnrHeaderModification -} from './header-rules' +} from '../header-actions/header-rules' import { dnrRuleProblem, isDnrRuleProblem, type DnrRuleProblem } from './parse-errors' -import { parseDnrRedirect, type DnrRedirectSpec } from './redirect-url' -import { isValidRegexSubstitution, translateDnrRegex } from './regex-filter' +import { parseDnrRedirect, type DnrRedirectSpec } from '../redirect-actions/redirect-url' +import { isValidRegexSubstitution, translateDnrRegex } from '../regex/regex-filter' import { emptyConditionListProblem, parseRuleConditionFields, @@ -14,8 +14,8 @@ import { } from './rule-conditions' import { indexedRulePriority } from './rule-flags' import type { DnrActionType, DnrRule } from './rule-types' -import { DnrUrlPatternMatcher, parseUrlFilter } from './url-pattern' -import { isAsciiText, toLowerAscii } from './url-text' +import { DnrUrlPatternMatcher, parseUrlFilter } from '../matching/url-pattern' +import { isAsciiText, toLowerAscii } from '../matching/url-text' // IndexedRule::CreateIndexedRule (indexed_rule.cc). Checks run in Chromium's order so the // first problem reported is the one Chrome would report. diff --git a/src/main/browser/extensions/declarative-net-request/parse-errors.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/parse-errors.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/parse-errors.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/parse-errors.ts diff --git a/src/main/browser/extensions/declarative-net-request/rule-conditions.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/rule-conditions.ts similarity index 97% rename from src/main/browser/extensions/declarative-net-request/rule-conditions.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/rule-conditions.ts index df1506dc306..fc44fcc1e8e 100644 --- a/src/main/browser/extensions/declarative-net-request/rule-conditions.ts +++ b/src/main/browser/extensions/declarative-net-request/rule-compilation/rule-conditions.ts @@ -1,4 +1,4 @@ -import { DnrDomainList } from './domain-matching' +import { DnrDomainList } from '../matching/domain-matching' import { dnrRuleProblem, isDnrRuleProblem, type DnrRuleProblem } from './parse-errors' import { ELEMENT_TYPE, @@ -9,7 +9,7 @@ import { ruleMethodBit } from './rule-flags' import type { DnrRule } from './rule-types' -import { isAsciiText, toLowerAscii } from './url-text' +import { isAsciiText, toLowerAscii } from '../matching/url-text' // Condition parsing steps of IndexedRule::CreateIndexedRule (indexed_rule.cc): // ComputeRequestMethods, ComputeElementTypes, CanonicalizeDomains and tab ids. diff --git a/src/main/browser/extensions/declarative-net-request/rule-flags.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/rule-flags.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/rule-flags.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/rule-flags.ts diff --git a/src/main/browser/extensions/declarative-net-request/rule-schema.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/rule-schema.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/rule-schema.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/rule-schema.ts diff --git a/src/main/browser/extensions/declarative-net-request/rule-types.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/rule-types.ts similarity index 100% rename from src/main/browser/extensions/declarative-net-request/rule-types.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/rule-types.ts diff --git a/src/main/browser/extensions/declarative-net-request/ruleset-builder.ts b/src/main/browser/extensions/declarative-net-request/rule-compilation/ruleset-builder.ts similarity index 98% rename from src/main/browser/extensions/declarative-net-request/ruleset-builder.ts rename to src/main/browser/extensions/declarative-net-request/rule-compilation/ruleset-builder.ts index 33fd81070ea..4705a27f540 100644 --- a/src/main/browser/extensions/declarative-net-request/ruleset-builder.ts +++ b/src/main/browser/extensions/declarative-net-request/rule-compilation/ruleset-builder.ts @@ -2,7 +2,7 @@ import { createIndexedRule } from './indexed-rule' import { dnrRuleProblem, type DnrRuleProblem } from './parse-errors' import { readDnrRuleJson } from './rule-schema' import type { DnrRulesetSource } from './rule-types' -import { DnrRulesetIndex } from './ruleset-index' +import { DnrRulesetIndex } from '../indexing/ruleset-index' // RulesetSource::IndexRules (ruleset_source.cc). Static rulesets skip bad rules with warnings; // updateDynamicRules / updateSessionRules reject the whole update on the first bad rule. diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-extension-rules.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-extension-rules.ts index 9581670859c..e4edf987e07 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-extension-rules.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-extension-rules.ts @@ -1,6 +1,6 @@ import type { DnrEngine } from '../engine' -import { readDnrRuleJson } from '../rule-schema' -import type { DnrRule, DnrRulesetSource } from '../rule-types' +import { readDnrRuleJson } from '../rule-compilation/rule-schema' +import type { DnrRule, DnrRulesetSource } from '../rule-compilation/rule-types' import { DNR_API_CONSTANTS, DNR_ERRORS, diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.test.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.test.ts index f132f16584b..4a04c618c1a 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.test.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import { DnrEngine } from '../engine' import { createDnrRequestState } from '../request-state' -import { buildDnrRuleset } from '../ruleset-builder' +import { buildDnrRuleset } from '../rule-compilation/ruleset-builder' import { DnrFrameTracker } from './browser-extension-dnr-frames' const EXT = 'f'.repeat(32) diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.ts index e607c45d0a4..8e203a098e3 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-frames.ts @@ -1,5 +1,5 @@ import type { DnrEngine, DnrFrameCommit } from '../engine' -import type { DnrFrameAllowState } from '../extension-matcher' +import type { DnrFrameAllowState } from '../matching/extension-matcher' import type { DnrResponseHeaders } from '../request-context' /** What a frame's navigation request carried, which its committed document is matched with. */ diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-limits.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-limits.ts index f2dce544d0d..f0c34c3e39c 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-limits.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-limits.ts @@ -1,4 +1,4 @@ -import type { DnrRule } from '../rule-types' +import type { DnrRule } from '../rule-compilation/rule-types' /** chrome.declarativeNetRequest constants (declarative_net_request.webidl, Chromium 150). */ export const DNR_API_CONSTANTS = { diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.test.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.test.ts index 2df5534cedd..3c6f6baeb01 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.test.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from 'vitest' -import type { DnrRequestAction } from '../ruleset-matcher' +import type { DnrRequestAction } from '../matching/ruleset-matcher' import { DnrMatchedRules } from './browser-extension-dnr-matched-rules' const EXT = 'e'.repeat(32) diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.ts index 7d1245cf3a0..1a1d93ba192 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-matched-rules.ts @@ -1,4 +1,4 @@ -import type { DnrRequestAction } from '../ruleset-matcher' +import type { DnrRequestAction } from '../matching/ruleset-matcher' /** getMatchedRules' MatchedRuleInfo. */ export type DnrMatchedRuleInfo = { diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.test.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.test.ts index 0fec7e7570e..61945867460 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.test.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.test.ts @@ -37,7 +37,7 @@ const { attachBrowserExtensionDnr, getDnrSessionRuntime } = await import('./browser-extension-dnr-session') const { observeBrowserExtensionDnrContents } = await import('./browser-extension-dnr-contents') await import('../../actions/browser-extension-action-registry') -await import('../../browser-extension-web-request') +await import('../../requests/browser-extension-web-request-observer') const BLOCKER = 'c'.repeat(32) const PAGE = 'https://site.example' diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.ts index 76fd9ef456b..77c4ec418db 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-requests.ts @@ -1,15 +1,18 @@ import type { BrowserSessionRequestDecider } from '../../../sessions/requests/browser-session-request-decisions' import { createDnrRequestState, type DnrRequestState } from '../request-state' import type { DnrEvaluateOptions } from '../engine' -import { applyDnrRequestHeaderActions, applyDnrResponseHeaderActions } from '../header-modification' +import { + applyDnrRequestHeaderActions, + applyDnrResponseHeaderActions +} from '../header-actions/header-modification' import type { DnrRequestInfo, DnrResponseHeaders } from '../request-context' -import type { DnrRequestAction } from '../ruleset-matcher' +import type { DnrRequestAction } from '../matching/ruleset-matcher' import { getDnrSessionRuntime, type DnrSessionRuntime } from './browser-extension-dnr-session' import { extensionHasPermission } from '../../api-runtime/browser-extension-namespace-registry' import { chromeResourceType, isRequestHiddenFromExtensions -} from '../../browser-extension-web-request' +} from '../../requests/browser-extension-request-classification' type RequestDetails = { id: number diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-static-rulesets.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-static-rulesets.ts index 294812e4037..2eb66ca91ec 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-static-rulesets.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-static-rulesets.ts @@ -1,8 +1,11 @@ import { readFile } from 'node:fs/promises' import { isAbsolute, relative, resolve } from 'node:path' -import { buildDnrRulesetInSlices, type DnrInvalidRulePolicy } from '../ruleset-builder' -import type { DnrRulesetIndex } from '../ruleset-index' -import type { DnrRulesetSource } from '../rule-types' +import { + buildDnrRulesetInSlices, + type DnrInvalidRulePolicy +} from '../rule-compilation/ruleset-builder' +import type { DnrRulesetIndex } from '../indexing/ruleset-index' +import type { DnrRulesetSource } from '../rule-compilation/rule-types' /** One manifest declarative_net_request.rule_resources entry. */ export type DnrManifestRuleset = { diff --git a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-store.ts b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-store.ts index df9c0fea925..a3560b0bf83 100644 --- a/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-store.ts +++ b/src/main/browser/extensions/declarative-net-request/session/browser-extension-dnr-store.ts @@ -1,6 +1,6 @@ import { app } from 'electron' import { z } from 'zod' -import { BrowserExtensionSyncStorageStore } from '../../profile-storage/browser-extension-sync-storage-store' +import { BrowserExtensionProfileDocumentStore } from '../../profile-storage/browser-extension-profile-document-store' import { getBrowserExtensionProfileDataDirectory } from '../../profile-storage/browser-extension-profile-data-directory' /** @@ -19,11 +19,11 @@ const dnrStateSchema = z.object({ export type BrowserExtensionDnrState = z.infer -let store: BrowserExtensionSyncStorageStore | null = null +let store: BrowserExtensionProfileDocumentStore | null = null -export function getBrowserExtensionDnrStore(): BrowserExtensionSyncStorageStore { +export function getBrowserExtensionDnrStore(): BrowserExtensionProfileDocumentStore { if (!store) { - const created = new BrowserExtensionSyncStorageStore( + const created = new BrowserExtensionProfileDocumentStore( getBrowserExtensionProfileDataDirectory(), 'declarative-net-request' ) diff --git a/src/main/browser/extensions/installation/browser-extension-mutation-lock.ts b/src/main/browser/extensions/installation/browser-extension-mutation-lock.ts new file mode 100644 index 00000000000..41d9d6215a9 --- /dev/null +++ b/src/main/browser/extensions/installation/browser-extension-mutation-lock.ts @@ -0,0 +1,27 @@ +const busyExtensionIds = new Set() + +export class BrowserExtensionBusyError extends Error { + constructor() { + super('This extension is already being installed or removed') + } +} + +/** Serializes install, update and uninstall of one extension id. */ +export async function withBrowserExtensionLock( + extensionId: string, + action: () => Promise +): Promise { + if (busyExtensionIds.has(extensionId)) { + throw new BrowserExtensionBusyError() + } + busyExtensionIds.add(extensionId) + try { + return await action() + } finally { + busyExtensionIds.delete(extensionId) + } +} + +export function isBrowserExtensionBusy(extensionId: string): boolean { + return busyExtensionIds.has(extensionId) +} diff --git a/src/main/browser/extensions/installation/browser-extension-webstore-install.test.ts b/src/main/browser/extensions/installation/browser-extension-webstore-install.test.ts index 72a6e9aa4fa..138304fa1c3 100644 --- a/src/main/browser/extensions/installation/browser-extension-webstore-install.test.ts +++ b/src/main/browser/extensions/installation/browser-extension-webstore-install.test.ts @@ -8,6 +8,7 @@ import { extensionIdFromPublicKey, type VerifiedCrx } from './browser-extension- import { generateCrxTestKey, type CrxTestKey } from './browser-extension-crx-fixture' import type * as InstallStoreModule from './browser-extension-install-store' import type * as InstallModule from './browser-extension-webstore-install' +import type * as MutationLockModule from './browser-extension-mutation-lock' const state = vi.hoisted(() => { const enabled: Record = {} @@ -21,7 +22,9 @@ const state = vi.hoisted(() => { }) vi.mock('electron', () => ({ app: { getPath: () => state.userData, on: vi.fn() } })) -vi.mock('../api/runtime', () => ({ openBrowserExtensionOptionsPage: vi.fn() })) +vi.mock('../tabs-and-windows/browser-extension-options-page', () => ({ + openBrowserExtensionOptionsPage: vi.fn() +})) vi.mock('../web-store/browser-extension-webstore-updater', () => ({ reviewHeldBrowserExtensionUpdate: vi.fn() })) @@ -50,6 +53,7 @@ vi.mock('../profile-sessions/browser-extension-session-loader', () => ({ let key: CrxTestKey let extensionId: string let install: typeof InstallModule +let mutationLock: typeof MutationLockModule let installStore: typeof InstallStoreModule beforeAll(() => { @@ -64,6 +68,7 @@ beforeEach(async () => { state.load.mockImplementation(async () => ({})) vi.resetModules() install = await import('./browser-extension-webstore-install') + mutationLock = await import('./browser-extension-mutation-lock') installStore = await import('./browser-extension-install-store') }) afterEach(() => { @@ -111,7 +116,7 @@ describe('store installs', () => { await vi.waitFor(() => expect(state.load).toHaveBeenCalledWith('work', first)) try { await expect( - install.withBrowserExtensionLock(extensionId, () => + mutationLock.withBrowserExtensionLock(extensionId, () => install.commitStagedWebStoreExtension(staged, null) ) ).rejects.toThrow('already being installed') diff --git a/src/main/browser/extensions/installation/browser-extension-webstore-install.ts b/src/main/browser/extensions/installation/browser-extension-webstore-install.ts index 2ed352f13db..095890c8e28 100644 --- a/src/main/browser/extensions/installation/browser-extension-webstore-install.ts +++ b/src/main/browser/extensions/installation/browser-extension-webstore-install.ts @@ -36,33 +36,6 @@ import { const VERSION_PATTERN = /^\d+(\.\d+){0,3}$/ const STAGING_PREFIX = '.staging-' -const busyExtensionIds = new Set() - -export class BrowserExtensionBusyError extends Error { - constructor() { - super('This extension is already being installed or removed') - } -} - -/** Serializes install, update and uninstall of one extension id. */ -export async function withBrowserExtensionLock( - extensionId: string, - action: () => Promise -): Promise { - if (busyExtensionIds.has(extensionId)) { - throw new BrowserExtensionBusyError() - } - busyExtensionIds.add(extensionId) - try { - return await action() - } finally { - busyExtensionIds.delete(extensionId) - } -} - -export function isBrowserExtensionBusy(extensionId: string): boolean { - return busyExtensionIds.has(extensionId) -} export type StagedWebStoreExtension = { extensionId: string diff --git a/src/main/browser/extensions/permissions/browser-extension-request-host-access.ts b/src/main/browser/extensions/permissions/browser-extension-request-host-access.ts index be7c935dcad..ab5cf2b08c4 100644 --- a/src/main/browser/extensions/permissions/browser-extension-request-host-access.ts +++ b/src/main/browser/extensions/permissions/browser-extension-request-host-access.ts @@ -1,4 +1,4 @@ -import type { DnrHostAccess } from '../declarative-net-request/extension-matcher' +import type { DnrHostAccess } from '../declarative-net-request/matching/extension-matcher' import { browserExtensionActiveTabCovers } from './browser-extension-active-tab' import { matchesExtensionMatchPattern } from './browser-extension-match-pattern' import { manifestPermissionSets } from './browser-extension-permission-grants' diff --git a/src/main/browser/extensions/permissions/browser-extension-session-permissions.ts b/src/main/browser/extensions/permissions/browser-extension-session-permissions.ts new file mode 100644 index 00000000000..750309bcd88 --- /dev/null +++ b/src/main/browser/extensions/permissions/browser-extension-session-permissions.ts @@ -0,0 +1,19 @@ +import { getBrowserExtensionProfileIdForSession } from '../api-runtime/browser-extension-api-host' +import { browserExtensionHostPatterns } from './browser-extension-permission-grants' +import { getBrowserExtensionProfileStateStore } from '../profile-storage/browser-extension-profile-state-store' + +export function readBrowserExtensionProfileGrants(profileId: string, extensionId: string) { + return getBrowserExtensionProfileStateStore().read(profileId, extensionId) +} + +/** Manifest host patterns plus origins the user granted in this session's profile. */ +export function browserExtensionHostPatternsInSession( + extension: Electron.Extension, + session: Electron.Session +): string[] { + const profileId = getBrowserExtensionProfileIdForSession(session) + const grants = profileId + ? readBrowserExtensionProfileGrants(profileId, extension.id) + : { grantedPermissions: [], grantedOrigins: [] } + return browserExtensionHostPatterns(extension.manifest, grants) +} diff --git a/src/main/browser/extensions/permissions/browser-extension-settings-prompt-lifetime.test.ts b/src/main/browser/extensions/permissions/browser-extension-settings-prompt-lifetime.test.ts index f074bd911d4..4ac6e47d000 100644 --- a/src/main/browser/extensions/permissions/browser-extension-settings-prompt-lifetime.test.ts +++ b/src/main/browser/extensions/permissions/browser-extension-settings-prompt-lifetime.test.ts @@ -10,7 +10,7 @@ import { } from '../../../../shared/browser/extensions/browser-extension-webstore' import type * as PromptModule from './browser-extension-install-prompt' import type * as ServiceModule from '../web-store/browser-extension-webstore-service' -import type * as InstallModule from '../installation/browser-extension-webstore-install' +import type * as MutationLockModule from '../installation/browser-extension-mutation-lock' const state = vi.hoisted(() => ({ userData: '', @@ -65,7 +65,7 @@ const renderers: EventEmitter[] = [] const installs: Promise[] = [] let service: typeof ServiceModule let prompt: typeof PromptModule -let install: typeof InstallModule +let mutationLock: typeof MutationLockModule function renderer() { const contents = Object.assign(new EventEmitter(), { @@ -138,7 +138,7 @@ beforeEach(async () => { vi.resetModules() service = await import('../web-store/browser-extension-webstore-service') prompt = await import('./browser-extension-install-prompt') - install = await import('../installation/browser-extension-webstore-install') + mutationLock = await import('../installation/browser-extension-mutation-lock') }) afterEach(async () => { @@ -155,14 +155,14 @@ describe('Settings extension install prompt lifetime', () => { const pending = start(contents) const request = await requested(contents) expect(stagedFolders()).toHaveLength(1) - expect(install.isBrowserExtensionBusy(extensionId)).toBe(true) + expect(mutationLock.isBrowserExtensionBusy(extensionId)).toBe(true) contents.emit('did-navigate', {}, 'file:///orca/index.html', 200, 'OK') - await vi.waitFor(() => expect(install.isBrowserExtensionBusy(extensionId)).toBe(false)) + await vi.waitFor(() => expect(mutationLock.isBrowserExtensionBusy(extensionId)).toBe(false)) await expect(pending).resolves.toEqual({ status: 'cancelled' }) expect(stagedFolders()).toEqual([]) - expect(install.isBrowserExtensionBusy(extensionId)).toBe(false) + expect(mutationLock.isBrowserExtensionBusy(extensionId)).toBe(false) expect( prompt.respondToBrowserExtensionInstallPrompt(asContents(contents), { requestId: request.requestId, @@ -181,6 +181,6 @@ describe('Settings extension install prompt lifetime', () => { accepted: true }) await expect(retry).resolves.toEqual({ status: 'installed', name: 'Vault' }) - expect(install.isBrowserExtensionBusy(extensionId)).toBe(false) + expect(mutationLock.isBrowserExtensionBusy(extensionId)).toBe(false) }) }) diff --git a/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts b/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts new file mode 100644 index 00000000000..e0abfe450e3 --- /dev/null +++ b/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts @@ -0,0 +1,36 @@ +import type { BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' +import { getHostSessionState } from '../api-runtime/browser-extension-contexts' +import { getBrowserExtensionContextSignal } from '../api-runtime/browser-extension-context-lifetime' + +const USER_GESTURE_WINDOW_MS = 5_000 + +const recentGestures = new WeakMap() + +/** + * Chrome lets permissions.request run only inside a user gesture. Orca records the gestures it + * delivers to an extension (menu click, command, notification click, toolbar click). + */ +export function noteBrowserExtensionUserGesture( + session: Electron.Session, + extensionId: string +): void { + const state = getHostSessionState(session) + if (!state) { + return + } + const signal = getBrowserExtensionContextSignal(state, extensionId) + recentGestures.set(signal, Date.now()) +} + +export function hasBrowserExtensionUserGesture(caller: BrowserExtensionCaller): boolean { + if (caller.userGesture === true) { + return true + } + const state = getHostSessionState(caller.session) + if (!state) { + return false + } + const signal = getBrowserExtensionContextSignal(state, caller.extension.id) + const at = recentGestures.get(signal) + return at !== undefined && !signal.aborted && Date.now() - at <= USER_GESTURE_WINDOW_MS +} diff --git a/src/main/browser/extensions/profile-sessions/browser-extension-session-services.test.ts b/src/main/browser/extensions/profile-sessions/browser-extension-session-services.test.ts index 62e6027932e..542207de588 100644 --- a/src/main/browser/extensions/profile-sessions/browser-extension-session-services.test.ts +++ b/src/main/browser/extensions/profile-sessions/browser-extension-session-services.test.ts @@ -59,7 +59,7 @@ vi.mock('./browser-extension-session-loader', () => ({ attachHook = hooks } })) -vi.mock('../browser-extension-web-request', () => ({ +vi.mock('../requests/browser-extension-web-request-observer', () => ({ installBrowserExtensionWebRequestForwarding: vi.fn(), observeBrowserExtensionWebRequests: vi.fn(), stopObservingBrowserExtensionWebRequests: vi.fn() diff --git a/src/main/browser/extensions/profile-sessions/browser-extension-session-services.ts b/src/main/browser/extensions/profile-sessions/browser-extension-session-services.ts index 6addfc642d1..c7ecd3215b2 100644 --- a/src/main/browser/extensions/profile-sessions/browser-extension-session-services.ts +++ b/src/main/browser/extensions/profile-sessions/browser-extension-session-services.ts @@ -22,7 +22,7 @@ import { installBrowserExtensionWebRequestForwarding, observeBrowserExtensionWebRequests, stopObservingBrowserExtensionWebRequests -} from '../browser-extension-web-request' +} from '../requests/browser-extension-web-request-observer' // Why once: a session outlives the experimental setting being switched off and on again. const servedSessions = new WeakSet() diff --git a/src/main/browser/extensions/profile-storage/browser-extension-data-removal.test.ts b/src/main/browser/extensions/profile-storage/browser-extension-data-removal.test.ts index 235dbeef7b7..c88feafc4f5 100644 --- a/src/main/browser/extensions/profile-storage/browser-extension-data-removal.test.ts +++ b/src/main/browser/extensions/profile-storage/browser-extension-data-removal.test.ts @@ -51,7 +51,9 @@ const { } = await import('./browser-extension-data-removal') const { getBrowserExtensionProfileStateStore } = await import('./browser-extension-profile-state-store') -const { BrowserExtensionSyncStorageStore, getBrowserExtensionSyncStorageStore } = +const { BrowserExtensionProfileDocumentStore } = + await import('./browser-extension-profile-document-store') +const { getBrowserExtensionSyncStorageStore } = await import('./browser-extension-sync-storage-store') const { getBrowserExtensionInstallStore } = await import('../installation/browser-extension-install-store') @@ -112,7 +114,7 @@ describe('removeUninstalledBrowserExtensionData', () => { expect(Object.keys(states.readProfile('work'))).toEqual([OTHER]) expect(states.read('home', ID).grantedPermissions).toEqual(['tabs']) expect( - new BrowserExtensionSyncStorageStore(getBrowserExtensionProfileDataDirectory()).read( + new BrowserExtensionProfileDocumentStore(getBrowserExtensionProfileDataDirectory()).read( 'work', ID ) diff --git a/src/main/browser/extensions/profile-storage/browser-extension-profile-document-store.ts b/src/main/browser/extensions/profile-storage/browser-extension-profile-document-store.ts new file mode 100644 index 00000000000..9a1f6a8b0bb --- /dev/null +++ b/src/main/browser/extensions/profile-storage/browser-extension-profile-document-store.ts @@ -0,0 +1,158 @@ +import { mkdirSync, readFileSync, readdirSync, rmSync } from 'node:fs' +import { join } from 'node:path' +import { isChromeExtensionId } from '../../../../shared/browser/extensions/browser-extension-url' +import { durableWriteTempPath, writeFileDurableSync } from '../../../filesystem/durable-file-write' + +const SYNC_STORAGE_DIRECTORY = 'sync-storage' + +const PERSIST_DELAY_MS = 250 + +export type BrowserExtensionProfileDocumentItems = Record + +function isItems(value: unknown): value is BrowserExtensionProfileDocumentItems { + return value !== null && typeof value === 'object' && !Array.isArray(value) +} + +export class BrowserExtensionProfileDocumentStore { + /** Profile → extension → items. */ + private readonly cache = new Map>() + /** Profile → extensions whose items are not on disk yet. */ + private readonly dirty = new Map>() + private timer: NodeJS.Timeout | null = null + + constructor( + private readonly rootDirectory: string, + /** declarativeNetRequest state reuses this store under its own directory. */ + private readonly directoryName = SYNC_STORAGE_DIRECTORY + ) {} + + read(profileId: string, extensionId: string): BrowserExtensionProfileDocumentItems { + let byExtension = this.cache.get(profileId) + if (!byExtension) { + byExtension = new Map() + this.cache.set(profileId, byExtension) + } + let items = byExtension.get(extensionId) + if (!items) { + items = this.load(profileId, extensionId) + byExtension.set(extensionId, items) + } + return items + } + + write(profileId: string, extensionId: string, items: BrowserExtensionProfileDocumentItems): void { + this.read(profileId, extensionId) + this.cache.get(profileId)?.set(extensionId, items) + const dirty = this.dirty.get(profileId) ?? new Set() + dirty.add(extensionId) + this.dirty.set(profileId, dirty) + this.schedulePersist() + } + + forgetExtension(extensionId: string): void { + for (const byExtension of this.cache.values()) { + byExtension.delete(extensionId) + } + for (const dirty of this.dirty.values()) { + dirty.delete(extensionId) + } + let profileDirectories: string[] + try { + profileDirectories = readdirSync(join(this.rootDirectory, this.directoryName)) + } catch { + return + } + for (const directory of profileDirectories) { + rmSync(join(this.rootDirectory, this.directoryName, directory, this.fileName(extensionId)), { + force: true + }) + } + } + + forgetProfileExtension(profileId: string, extensionId: string): void { + this.cache.get(profileId)?.delete(extensionId) + this.dirty.get(profileId)?.delete(extensionId) + rmSync(join(this.profileDirectory(profileId), this.fileName(extensionId)), { force: true }) + } + + /** Drops one profile's items of every extension not in `extensionIds`. */ + forgetExtensionsExcept(profileId: string, extensionIds: ReadonlySet): void { + const cached = this.cache.get(profileId) + for (const extensionId of cached?.keys() ?? []) { + if (!extensionIds.has(extensionId)) { + cached?.delete(extensionId) + this.dirty.get(profileId)?.delete(extensionId) + } + } + let files: string[] + try { + files = readdirSync(this.profileDirectory(profileId)) + } catch { + return + } + for (const file of files) { + const extensionId = file.replace(/\.json$/, '') + if (isChromeExtensionId(extensionId) && !extensionIds.has(extensionId)) { + rmSync(join(this.profileDirectory(profileId), file), { force: true }) + } + } + } + + forgetProfile(profileId: string): void { + this.cache.delete(profileId) + this.dirty.delete(profileId) + rmSync(this.profileDirectory(profileId), { recursive: true, force: true }) + } + + flush(): void { + if (this.timer) { + clearTimeout(this.timer) + this.timer = null + } + for (const [profileId, extensionIds] of this.dirty) { + const directory = this.profileDirectory(profileId) + mkdirSync(directory, { recursive: true }) + for (const extensionId of extensionIds) { + const target = join(directory, this.fileName(extensionId)) + const items = this.cache.get(profileId)?.get(extensionId) ?? {} + writeFileDurableSync(durableWriteTempPath(target), target, JSON.stringify(items)) + extensionIds.delete(extensionId) + } + this.dirty.delete(profileId) + } + } + + // Why debounce: extensions often write settings key by key in a burst. + private schedulePersist(): void { + if (this.timer) { + return + } + this.timer = setTimeout(() => { + this.timer = null + try { + this.flush() + } catch (error) { + console.warn('[browser-extensions] Could not save extension sync storage:', error) + } + }, PERSIST_DELAY_MS) + } + + private load(profileId: string, extensionId: string): BrowserExtensionProfileDocumentItems { + try { + const raw: unknown = JSON.parse( + readFileSync(join(this.profileDirectory(profileId), this.fileName(extensionId)), 'utf8') + ) + return isItems(raw) ? raw : {} + } catch { + return {} + } + } + + private profileDirectory(profileId: string): string { + return join(this.rootDirectory, this.directoryName, encodeURIComponent(profileId)) + } + + private fileName(extensionId: string): string { + return `${encodeURIComponent(extensionId)}.json` + } +} diff --git a/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.test.ts b/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.test.ts index 4b87e80c204..fd00df7b1f4 100644 --- a/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.test.ts +++ b/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.test.ts @@ -5,7 +5,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('electron', () => ({ app: { getPath: () => tmpdir(), on: vi.fn() } })) -const { BrowserExtensionSyncStorageStore } = await import('./browser-extension-sync-storage-store') +const { BrowserExtensionProfileDocumentStore } = + await import('./browser-extension-profile-document-store') const EXTENSION = 'a'.repeat(32) const OTHER = 'b'.repeat(32) @@ -18,34 +19,34 @@ afterEach(() => { rmSync(root, { recursive: true, force: true }) }) -describe('BrowserExtensionSyncStorageStore', () => { +describe('BrowserExtensionProfileDocumentStore', () => { it('keeps each profile and extension apart and survives a restart', () => { - const store = new BrowserExtensionSyncStorageStore(root) + const store = new BrowserExtensionProfileDocumentStore(root) store.write('default', EXTENSION, { theme: { dark: true }, list: [1, 'two'] }) store.write('work', EXTENSION, { theme: 'light' }) store.flush() - const reopened = new BrowserExtensionSyncStorageStore(root) + const reopened = new BrowserExtensionProfileDocumentStore(root) expect(reopened.read('default', EXTENSION)).toEqual({ theme: { dark: true }, list: [1, 'two'] }) expect(reopened.read('work', EXTENSION)).toEqual({ theme: 'light' }) expect(reopened.read('default', OTHER)).toEqual({}) }) it('retries acknowledged writes after a failed flush', () => { - const store = new BrowserExtensionSyncStorageStore(root) + const store = new BrowserExtensionProfileDocumentStore(root) store.write('default', EXTENSION, { token: 'saved after recovery' }) writeFileSync(join(root, 'sync-storage'), 'blocks the storage directory') expect(() => store.flush()).toThrow() rmSync(join(root, 'sync-storage')) store.flush() - expect(new BrowserExtensionSyncStorageStore(root).read('default', EXTENSION)).toEqual({ + expect(new BrowserExtensionProfileDocumentStore(root).read('default', EXTENSION)).toEqual({ token: 'saved after recovery' }) }) it('forgets a removed extension in every profile and a deleted profile entirely', () => { - const store = new BrowserExtensionSyncStorageStore(root) + const store = new BrowserExtensionProfileDocumentStore(root) for (const profile of ['default', 'work']) { store.write(profile, EXTENSION, { kept: profile }) store.write(profile, OTHER, { kept: profile }) @@ -58,7 +59,7 @@ describe('BrowserExtensionSyncStorageStore', () => { store.flush() expect(store.read('default', EXTENSION)).toEqual({}) - const reopened = new BrowserExtensionSyncStorageStore(root) + const reopened = new BrowserExtensionProfileDocumentStore(root) expect(reopened.read('default', EXTENSION)).toEqual({}) expect(reopened.read('default', OTHER)).toEqual({ kept: 'default' }) expect(reopened.read('work', OTHER)).toEqual({}) diff --git a/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.ts b/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.ts index 441a023ffd1..569bdd6b60c 100644 --- a/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.ts +++ b/src/main/browser/extensions/profile-storage/browser-extension-sync-storage-store.ts @@ -1,173 +1,19 @@ +import { BrowserExtensionProfileDocumentStore } from './browser-extension-profile-document-store' import { app } from 'electron' -import { mkdirSync, readFileSync, readdirSync, rmSync } from 'node:fs' -import { join } from 'node:path' -import { isChromeExtensionId } from '../../../../shared/browser/extensions/browser-extension-url' -import { durableWriteTempPath, writeFileDurableSync } from '../../../filesystem/durable-file-write' import { getBrowserExtensionProfileDataDirectory } from './browser-extension-profile-data-directory' -const SYNC_STORAGE_DIRECTORY = 'sync-storage' -const PERSIST_DELAY_MS = 250 - -export type BrowserExtensionSyncItems = Record - -function isItems(value: unknown): value is BrowserExtensionSyncItems { - return value !== null && typeof value === 'object' && !Array.isArray(value) -} +let syncStorageStore: BrowserExtensionProfileDocumentStore | null = null /** * chrome.storage.sync items per browser profile and extension, kept on this machine the way * Chrome keeps them for a user who is not signed in. One file per extension, so a write rewrites * only that extension's items (up to 100 KB). */ -export class BrowserExtensionSyncStorageStore { - /** Profile → extension → items. */ - private readonly cache = new Map>() - /** Profile → extensions whose items are not on disk yet. */ - private readonly dirty = new Map>() - private timer: NodeJS.Timeout | null = null - - constructor( - private readonly rootDirectory: string, - /** declarativeNetRequest state reuses this store under its own directory. */ - private readonly directoryName = SYNC_STORAGE_DIRECTORY - ) {} - - read(profileId: string, extensionId: string): BrowserExtensionSyncItems { - let byExtension = this.cache.get(profileId) - if (!byExtension) { - byExtension = new Map() - this.cache.set(profileId, byExtension) - } - let items = byExtension.get(extensionId) - if (!items) { - items = this.load(profileId, extensionId) - byExtension.set(extensionId, items) - } - return items - } - - write(profileId: string, extensionId: string, items: BrowserExtensionSyncItems): void { - this.read(profileId, extensionId) - this.cache.get(profileId)?.set(extensionId, items) - const dirty = this.dirty.get(profileId) ?? new Set() - dirty.add(extensionId) - this.dirty.set(profileId, dirty) - this.schedulePersist() - } - - forgetExtension(extensionId: string): void { - for (const byExtension of this.cache.values()) { - byExtension.delete(extensionId) - } - for (const dirty of this.dirty.values()) { - dirty.delete(extensionId) - } - let profileDirectories: string[] - try { - profileDirectories = readdirSync(join(this.rootDirectory, this.directoryName)) - } catch { - return - } - for (const directory of profileDirectories) { - rmSync(join(this.rootDirectory, this.directoryName, directory, this.fileName(extensionId)), { - force: true - }) - } - } - - forgetProfileExtension(profileId: string, extensionId: string): void { - this.cache.get(profileId)?.delete(extensionId) - this.dirty.get(profileId)?.delete(extensionId) - rmSync(join(this.profileDirectory(profileId), this.fileName(extensionId)), { force: true }) - } - - /** Drops one profile's items of every extension not in `extensionIds`. */ - forgetExtensionsExcept(profileId: string, extensionIds: ReadonlySet): void { - const cached = this.cache.get(profileId) - for (const extensionId of cached?.keys() ?? []) { - if (!extensionIds.has(extensionId)) { - cached?.delete(extensionId) - this.dirty.get(profileId)?.delete(extensionId) - } - } - let files: string[] - try { - files = readdirSync(this.profileDirectory(profileId)) - } catch { - return - } - for (const file of files) { - const extensionId = file.replace(/\.json$/, '') - if (isChromeExtensionId(extensionId) && !extensionIds.has(extensionId)) { - rmSync(join(this.profileDirectory(profileId), file), { force: true }) - } - } - } - - forgetProfile(profileId: string): void { - this.cache.delete(profileId) - this.dirty.delete(profileId) - rmSync(this.profileDirectory(profileId), { recursive: true, force: true }) - } - - flush(): void { - if (this.timer) { - clearTimeout(this.timer) - this.timer = null - } - for (const [profileId, extensionIds] of this.dirty) { - const directory = this.profileDirectory(profileId) - mkdirSync(directory, { recursive: true }) - for (const extensionId of extensionIds) { - const target = join(directory, this.fileName(extensionId)) - const items = this.cache.get(profileId)?.get(extensionId) ?? {} - writeFileDurableSync(durableWriteTempPath(target), target, JSON.stringify(items)) - extensionIds.delete(extensionId) - } - this.dirty.delete(profileId) - } - } - - // Why debounce: extensions often write settings key by key in a burst. - private schedulePersist(): void { - if (this.timer) { - return - } - this.timer = setTimeout(() => { - this.timer = null - try { - this.flush() - } catch (error) { - console.warn('[browser-extensions] Could not save extension sync storage:', error) - } - }, PERSIST_DELAY_MS) - } - - private load(profileId: string, extensionId: string): BrowserExtensionSyncItems { - try { - const raw: unknown = JSON.parse( - readFileSync(join(this.profileDirectory(profileId), this.fileName(extensionId)), 'utf8') - ) - return isItems(raw) ? raw : {} - } catch { - return {} - } - } - - private profileDirectory(profileId: string): string { - return join(this.rootDirectory, this.directoryName, encodeURIComponent(profileId)) - } - - private fileName(extensionId: string): string { - return `${encodeURIComponent(extensionId)}.json` - } -} - -let syncStorageStore: BrowserExtensionSyncStorageStore | null = null - -export function getBrowserExtensionSyncStorageStore(): BrowserExtensionSyncStorageStore { +export function getBrowserExtensionSyncStorageStore(): BrowserExtensionProfileDocumentStore { if (!syncStorageStore) { - const store = new BrowserExtensionSyncStorageStore(getBrowserExtensionProfileDataDirectory()) + const store = new BrowserExtensionProfileDocumentStore( + getBrowserExtensionProfileDataDirectory() + ) app.on('will-quit', () => store.flush()) syncStorageStore = store } diff --git a/src/main/browser/extensions/requests/browser-extension-request-classification.ts b/src/main/browser/extensions/requests/browser-extension-request-classification.ts new file mode 100644 index 00000000000..1d974b43e40 --- /dev/null +++ b/src/main/browser/extensions/requests/browser-extension-request-classification.ts @@ -0,0 +1,57 @@ +import { CHROME_WEB_STORE_ORIGIN } from '../../../../shared/browser/extensions/browser-extension-webstore' + +type ChromeResourceType = + | 'main_frame' + | 'sub_frame' + | 'stylesheet' + | 'script' + | 'image' + | 'font' + | 'object' + | 'xmlhttprequest' + | 'ping' + | 'csp_report' + | 'media' + | 'websocket' + | 'other' + +const CHROME_RESOURCE_TYPES = new Map([ + ['mainFrame', 'main_frame'], + ['subFrame', 'sub_frame'], + ['stylesheet', 'stylesheet'], + ['script', 'script'], + ['image', 'image'], + ['font', 'font'], + ['object', 'object'], + ['xhr', 'xmlhttprequest'], + ['ping', 'ping'], + ['cspReport', 'csp_report'], + ['media', 'media'], + ['webSocket', 'websocket'] +]) + +/** Electron's resourceType → Chrome's, shared by webRequest and declarativeNetRequest. */ +export function chromeResourceType(resourceType: string): ChromeResourceType { + return CHROME_RESOURCE_TYPES.get(resourceType) ?? 'other' +} + +const WEB_URL = /^(?:https?|wss?):/i + +/** WebRequestPermissions::HideRequest: requests extensions never see or act on. */ +export function isRequestHiddenFromExtensions(details: { + url: string + webContentsId?: number + initiatorOrigin?: string +}): boolean { + if (!WEB_URL.test(details.url)) { + return true + } + // Orca's own main-process requests carry neither a webContents nor an initiator. + if (details.webContentsId === undefined && details.initiatorOrigin === undefined) { + return true + } + return ( + details.url.startsWith(`${CHROME_WEB_STORE_ORIGIN}/`) || + details.url.startsWith('https://chrome.google.com/webstore') + ) +} diff --git a/src/main/browser/extensions/requests/browser-extension-web-request-details.ts b/src/main/browser/extensions/requests/browser-extension-web-request-details.ts new file mode 100644 index 00000000000..90964d4ab29 --- /dev/null +++ b/src/main/browser/extensions/requests/browser-extension-web-request-details.ts @@ -0,0 +1,101 @@ +import { chromeResourceType } from './browser-extension-request-classification' +import type { ObservedWebRequestDetails } from '../../sessions/requests/browser-session-web-request-observer' +import { chromeFrameDocument } from '../tabs-and-windows/browser-extension-frame-projection' + +// Chrome hides these unless a listener asks for 'extraHeaders'. +const HIDDEN_REQUEST_HEADERS = new Set(['accept-language', 'accept-encoding', 'referer', 'cookie']) + +const HIDDEN_RESPONSE_HEADERS = new Set(['set-cookie']) + +export type ChromeWebRequestDetails = { + requestId: string + url: string + method: string + frameId: number + parentFrameId: number + tabId: number + windowId: number + type: string + timeStamp: number + frameType?: 'outermost_frame' | 'sub_frame' + documentId?: string + parentDocumentId?: string + documentLifecycle?: 'active' + requestHeaders?: { name: string; value: string }[] + responseHeaders?: { name: string; value: string }[] + statusCode?: number + statusLine?: string + fromCache?: boolean + ip?: string + redirectUrl?: string + error?: string +} + +function requestHeaderList(headers: Record): { name: string; value: string }[] { + return Object.entries(headers) + .filter(([name]) => !HIDDEN_REQUEST_HEADERS.has(name.toLowerCase())) + .map(([name, value]) => ({ name, value })) +} + +function responseHeaderList(headers: Record): { name: string; value: string }[] { + return Object.entries(headers) + .filter(([name]) => !HIDDEN_RESPONSE_HEADERS.has(name.toLowerCase())) + .flatMap(([name, values]) => values.map((value) => ({ name, value }))) +} + +type WebRequestFrameFields = Pick< + ChromeWebRequestDetails, + | 'frameId' + | 'parentFrameId' + | 'frameType' + | 'documentId' + | 'parentDocumentId' + | 'documentLifecycle' +> + +/** A navigation request has no document yet; any other request belongs to its frame's current one. */ +export function browserExtensionRequestFrameFields( + details: ObservedWebRequestDetails +): WebRequestFrameFields { + if (details.resourceType === 'mainFrame') { + return { + frameId: 0, + parentFrameId: -1, + frameType: 'outermost_frame', + documentLifecycle: 'active' + } + } + const frame = details.frame && !details.frame.isDestroyed() ? details.frame : null + return frame + ? chromeFrameDocument(frame, details.resourceType !== 'subFrame') + : { frameId: 0, parentFrameId: -1 } +} + +export function toChromeWebRequestDetails( + details: ObservedWebRequestDetails, + tab: { tabId: number; windowId: number }, + frame: WebRequestFrameFields +): ChromeWebRequestDetails { + const type = chromeResourceType(details.resourceType) + return { + requestId: String(details.id), + url: details.url, + method: details.method, + ...frame, + ...tab, + type, + timeStamp: details.timestamp, + ...(details.requestHeaders + ? { requestHeaders: requestHeaderList(details.requestHeaders) } + : {}), + ...(details.responseHeaders + ? { responseHeaders: responseHeaderList(details.responseHeaders) } + : {}), + ...(details.statusCode !== undefined ? { statusCode: details.statusCode } : {}), + ...(details.statusLine !== undefined ? { statusLine: details.statusLine } : {}), + ...(details.fromCache !== undefined ? { fromCache: details.fromCache } : {}), + ...(details.ip ? { ip: details.ip } : {}), + ...(details.redirectURL ? { redirectUrl: details.redirectURL } : {}), + ...(details.error ? { error: details.error } : {}) + } +} diff --git a/src/main/browser/extensions/requests/browser-extension-web-request-filter.ts b/src/main/browser/extensions/requests/browser-extension-web-request-filter.ts new file mode 100644 index 00000000000..fdce0232e65 --- /dev/null +++ b/src/main/browser/extensions/requests/browser-extension-web-request-filter.ts @@ -0,0 +1,32 @@ +import type { ChromeWebRequestDetails } from './browser-extension-web-request-details' +import { matchesExtensionMatchPattern } from '../permissions/browser-extension-match-pattern' + +/** Main-side pre-filter against one listener's chrome.webRequest.RequestFilter. */ +export function webRequestFilterMatches( + filter: unknown, + details: ChromeWebRequestDetails +): boolean { + if (filter === null || typeof filter !== 'object') { + return true + } + const fields = new Map(Object.entries(filter)) + const urls = fields.get('urls') + if ( + Array.isArray(urls) && + !urls.some( + (pattern) => typeof pattern === 'string' && matchesExtensionMatchPattern(pattern, details.url) + ) + ) { + return false + } + const types = fields.get('types') + if (Array.isArray(types) && !types.includes(details.type)) { + return false + } + const tabId = fields.get('tabId') + const windowId = fields.get('windowId') + return ( + (typeof tabId !== 'number' || tabId === details.tabId) && + (typeof windowId !== 'number' || windowId === details.windowId) + ) +} diff --git a/src/main/browser/extensions/requests/browser-extension-web-request-observer.ts b/src/main/browser/extensions/requests/browser-extension-web-request-observer.ts new file mode 100644 index 00000000000..bd47fdd2ae8 --- /dev/null +++ b/src/main/browser/extensions/requests/browser-extension-web-request-observer.ts @@ -0,0 +1,127 @@ +import { webRequestFilterMatches } from './browser-extension-web-request-filter' +import { + browserExtensionRequestFrameFields, + toChromeWebRequestDetails +} from './browser-extension-web-request-details' +import { isRequestHiddenFromExtensions } from './browser-extension-request-classification' +import { + installBrowserSessionHeadersReceived, + installBrowserSessionRequestLifecycle, + removeBrowserSessionHeadersReceived, + removeBrowserSessionRequestLifecycle +} from '../../sessions/requests/browser-session-request-decisions' +import { + setBrowserSessionWebRequestObserver, + type ObservedWebRequestDetails, + type ObservedWebRequestEvent +} from '../../sessions/requests/browser-session-web-request-observer' +import { browserExtensionHostPatternsInSession } from '../permissions/browser-extension-session-permissions' +import { + emitBrowserExtensionEvent, + getBrowserExtensionProfileIdForSession +} from '../api-runtime/browser-extension-api-host' +import { browserExtensionEventHasListener } from '../api-runtime/browser-extension-contexts' +import { extensionHasPermission } from '../api-runtime/browser-extension-namespace-registry' +import { hasBrowserExtensionHostPermission } from '../permissions/browser-extension-permission-grants' +import { canExtensionAccessRequest } from '../permissions/browser-extension-request-host-access' +import { resolveBrowserExtensionTab } from '../tabs-and-windows/browser-extension-tab-model' + +/** + * chrome.webRequest, observed rather than blocking. Electron 43 delivers no webRequest events to + * MV3 workers, and Orca's own session handlers switch Electron's extension webRequest off anyway, + * so Orca forwards what its handlers see. MV3 store extensions cannot block requests in Chrome + * either (webRequestBlocking is policy-only), so nothing they could do is lost. + */ + +function forward( + session: Electron.Session, + event: ObservedWebRequestEvent, + details: ObservedWebRequestDetails +): void { + if ( + isRequestHiddenFromExtensions(details) || + !getBrowserExtensionProfileIdForSession(session) || + !browserExtensionEventHasListener(session, 'webRequest', event) + ) { + return + } + const entry = + details.webContentsId === undefined ? null : resolveBrowserExtensionTab(details.webContentsId) + const chromeDetails = toChromeWebRequestDetails( + details, + entry ? { tabId: entry.contents.id, windowId: entry.windowId } : { tabId: -1, windowId: -1 }, + browserExtensionRequestFrameFields(details) + ) + const request = { + url: details.url, + initiator: details.initiatorOrigin ?? null, + tabId: chromeDetails.tabId, + type: chromeDetails.type + } + emitBrowserExtensionEvent( + { session }, + 'webRequest', + event, + (extension) => { + const patterns = browserExtensionHostPatternsInSession(extension, session) + return canExtensionAccessRequest(extension.id, request, (url) => + hasBrowserExtensionHostPermission(patterns, url) + ) + ? [chromeDetails] + : null + }, + (filter) => webRequestFilterMatches(filter, chromeDetails) + ) +} + +const observedSessions = new WeakSet() +const watchedSessions = new WeakSet() +const OBSERVE_ALL = { urls: ['http://*/*', 'https://*/*', 'ws://*/*', 'wss://*/*'] } + +/** The events Orca does not otherwise handle, installed once a webRequest extension loads. */ +function observeRemainingEvents(session: Electron.Session): void { + if (observedSessions.has(session)) { + return + } + observedSessions.add(session) + const { webRequest } = session + webRequest.onSendHeaders(OBSERVE_ALL, (details) => forward(session, 'onSendHeaders', details)) + installBrowserSessionHeadersReceived(session) + webRequest.onResponseStarted(OBSERVE_ALL, (details) => + forward(session, 'onResponseStarted', details) + ) + webRequest.onBeforeRedirect(OBSERVE_ALL, (details) => + forward(session, 'onBeforeRedirect', details) + ) + installBrowserSessionRequestLifecycle(session) +} + +export function observeBrowserExtensionWebRequests(session: Electron.Session): void { + const maybeObserve = (extension: Electron.Extension): void => { + if (extensionHasPermission(extension, 'webRequest', session)) { + observeRemainingEvents(session) + } + } + if (!watchedSessions.has(session)) { + watchedSessions.add(session) + session.extensions.on('extension-ready', (_event, extension) => maybeObserve(extension)) + } + session.extensions.getAllExtensions().forEach(maybeObserve) +} + +/** Extensions turned off: hand the session's request path back to Orca's own handlers alone. */ +export function stopObservingBrowserExtensionWebRequests(session: Electron.Session): void { + removeBrowserSessionHeadersReceived(session) + removeBrowserSessionRequestLifecycle(session) + if (!observedSessions.delete(session)) { + return + } + const { webRequest } = session + webRequest.onSendHeaders(null) + webRequest.onResponseStarted(null) + webRequest.onBeforeRedirect(null) +} + +export function installBrowserExtensionWebRequestForwarding(): void { + setBrowserSessionWebRequestObserver(forward) +} diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts index 318f8fbe940..d24ec8647ce 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts @@ -99,7 +99,7 @@ vi.mock('./browser-extension-window-model', () => ({ presentBrowserExtensionWindow: vi.fn() })) vi.mock('../api/tabs-capture', () => ({ captureVisibleTabMethod: {} })) -vi.mock('../api/permissions', () => ({ +vi.mock('../permissions/browser-extension-session-permissions', () => ({ browserExtensionHostPatternsInSession: (_extension: unknown, sess: unknown) => sess === mocks.sessions.default ? mocks.grantedOrigins : [] })) @@ -109,7 +109,7 @@ const discardedTabs = await import('./browser-extension-discarded-tabs') const { recordActiveBrowserPage } = await import('./browser-extension-tab-model') const { registerTabsNamespace } = await import('../api/tabs') const { announceRegisteredBrowserExtensionTab, observeBrowserExtensionTabLifecycle } = - await import('../api/tabs-lifecycle') + await import('./browser-extension-tab-lifecycle') await import('../../../../preload/browser-extension-document-targets') function page(id: string, workspaceId: string, overrides: Partial = {}): BrowserPage { diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-frame-projection.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-frame-projection.ts new file mode 100644 index 00000000000..58732fa864f --- /dev/null +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-frame-projection.ts @@ -0,0 +1,27 @@ +import { getBrowserExtensionDocumentId } from './browser-extension-document-ids' + +/** + * Chrome frame ids: 0 for a tab's main frame, the frame-tree-node id otherwise. Electron's own + * content-script `sender.frameId` uses the same numbering, so tabs.sendMessage targets line up. + */ +export function chromeFrameId(frame: Electron.WebFrameMain): number { + return frame.parent ? frame.frameTreeNodeId : 0 +} + +function chromeParentFrameId(frame: Electron.WebFrameMain): number { + return frame.parent ? chromeFrameId(frame.parent) : -1 +} + +/** Chrome's frame fields; `documentId` is left out where the document does not exist yet. */ +export function chromeFrameDocument(frame: Electron.WebFrameMain, withDocument: boolean) { + return { + frameId: chromeFrameId(frame), + parentFrameId: chromeParentFrameId(frame), + ...(withDocument ? { documentId: getBrowserExtensionDocumentId(frame.frameTreeNodeId) } : {}), + ...(frame.parent + ? { parentDocumentId: getBrowserExtensionDocumentId(frame.parent.frameTreeNodeId) } + : {}), + frameType: frame.parent ? ('sub_frame' as const) : ('outermost_frame' as const), + documentLifecycle: 'active' as const + } +} diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts new file mode 100644 index 00000000000..541ab745a2f --- /dev/null +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts @@ -0,0 +1,23 @@ +import { getChromeExtensionOrigin } from '../../../../shared/browser/extensions/browser-extension-url' +import { BrowserExtensionApiError } from '../api-runtime/browser-extension-namespace-registry' +import { openBrowserExtensionTab } from './browser-extension-tab-model' + +function optionsPagePath(extension: Electron.Extension): string | null { + const manifest: { options_ui?: { page?: unknown }; options_page?: unknown } = + extension.manifest ?? {} + const page = manifest.options_ui?.page ?? manifest.options_page + return typeof page === 'string' && page.length > 0 ? page : null +} + +/** Opens the extension's options page as an Orca tab in the given browser profile. */ +export async function openBrowserExtensionOptionsPage( + browserProfileId: string, + extension: Electron.Extension +): Promise { + const page = optionsPagePath(extension) + if (!page) { + throw new BrowserExtensionApiError('This extension has no options page') + } + const url = new URL(page.replace(/^\/+/, ''), `${getChromeExtensionOrigin(extension.id)}/`) + await openBrowserExtensionTab(browserProfileId, url.toString(), true) +} diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-requested-navigation.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-requested-navigation.ts new file mode 100644 index 00000000000..912591a34be --- /dev/null +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-requested-navigation.ts @@ -0,0 +1,21 @@ +import { resolveBrowserExtensionRequestedUrl } from '../../../../shared/browser/extensions/browser-extension-url' +import { + BrowserExtensionApiError, + type BrowserExtensionCaller +} from '../api-runtime/browser-extension-namespace-registry' + +/** Resolves an extension-requested URL through Orca's rules; extension pages must be loaded in `sess`. */ +export function requireOpenableUrl( + caller: BrowserExtensionCaller, + rawUrl: string | undefined, + sess: Electron.Session +): string { + const resolved = resolveBrowserExtensionRequestedUrl(rawUrl, caller.extension.id) + if ( + !resolved || + (resolved.kind === 'extension' && !sess.extensions.getExtension(resolved.extensionId)) + ) { + throw new BrowserExtensionApiError(`Cannot open URL: ${rawUrl ?? ''}`) + } + return resolved.url +} diff --git a/src/main/browser/extensions/api/tabs-lifecycle.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-lifecycle.ts similarity index 98% rename from src/main/browser/extensions/api/tabs-lifecycle.ts rename to src/main/browser/extensions/tabs-and-windows/browser-extension-tab-lifecycle.ts index afbeb6c8ff4..743584daf4b 100644 --- a/src/main/browser/extensions/api/tabs-lifecycle.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-lifecycle.ts @@ -11,14 +11,14 @@ import { emitDiscardedBrowserTabRemoved, noteBrowserPageGuestGone, takeDiscardedBrowserTabId -} from '../tabs-and-windows/browser-extension-discarded-tabs' +} from './browser-extension-discarded-tabs' import { isBrowserExtensionSupportEnabled } from '../browser-extension-support' import { canExtensionSeeTabUrl, describeBrowserExtensionTab, replaceActivatedBrowserTabId, resolveBrowserExtensionTab -} from '../tabs-and-windows/browser-extension-tab-model' +} from './browser-extension-tab-model' /** Tabs announced through onCreated or onReplaced, with their window, so onRemoved matches them. */ const announcedTabWindowIds = new Map() diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts index 9cdc971f5e2..9db54215557 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts @@ -13,7 +13,7 @@ import { type BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' import { hasBrowserExtensionHostPermission } from '../permissions/browser-extension-permission-grants' -import { browserExtensionHostPatternsInSession } from '../api/permissions' +import { browserExtensionHostPatternsInSession } from '../permissions/browser-extension-session-permissions' import { closeBrowserExtensionPopup } from '../actions/browser-extension-popup-host' import { hasBrowserExtensionActiveTab } from '../permissions/browser-extension-active-tab' import { diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-resolution.test.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-resolution.test.ts index 7c82b7a10a8..36408158c1a 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-resolution.test.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-resolution.test.ts @@ -90,7 +90,7 @@ vi.mock('../permissions/browser-extension-active-tab', () => ({ hasBrowserExtensionActiveTab: (_extensionId: string, tabId: number) => mocks.activeGrants.has(tabId) })) -vi.mock('../api/permissions', () => ({ +vi.mock('../permissions/browser-extension-session-permissions', () => ({ browserExtensionHostPatternsInSession: () => mocks.hostPatterns })) vi.mock('../api/tabs-capture', () => ({ captureVisibleTabMethod: {} })) diff --git a/src/main/browser/extensions/web-store/browser-extension-webstore-service.ts b/src/main/browser/extensions/web-store/browser-extension-webstore-service.ts index 2e209ad717f..454fe682ab4 100644 --- a/src/main/browser/extensions/web-store/browser-extension-webstore-service.ts +++ b/src/main/browser/extensions/web-store/browser-extension-webstore-service.ts @@ -26,10 +26,12 @@ import { import { commitStagedWebStoreExtension, discardStagedWebStoreExtension, - isBrowserExtensionBusy, - stageWebStoreExtension, - withBrowserExtensionLock + stageWebStoreExtension } from '../installation/browser-extension-webstore-install' +import { + isBrowserExtensionBusy, + withBrowserExtensionLock +} from '../installation/browser-extension-mutation-lock' import { ChromeWebStoreInstallFlow, InvalidIconUrlError, diff --git a/src/main/browser/extensions/web-store/browser-extension-webstore-updater.test.ts b/src/main/browser/extensions/web-store/browser-extension-webstore-updater.test.ts index d2d55516bd5..dc36c26bdff 100644 --- a/src/main/browser/extensions/web-store/browser-extension-webstore-updater.test.ts +++ b/src/main/browser/extensions/web-store/browser-extension-webstore-updater.test.ts @@ -22,7 +22,7 @@ vi.mock('./browser-extension-webstore-download', async (importOriginal) => ({ ...(await importOriginal()), downloadWebStoreCrx: mocks.download })) -vi.mock('../installation/browser-extension-webstore-install', () => ({ +vi.mock('../installation/browser-extension-mutation-lock', () => ({ withBrowserExtensionLock: (_id: string, action: () => Promise) => action(), isBrowserExtensionBusy: () => false })) diff --git a/src/main/browser/extensions/web-store/browser-extension-webstore-updater.ts b/src/main/browser/extensions/web-store/browser-extension-webstore-updater.ts index 5ffee7341c2..127282b88f6 100644 --- a/src/main/browser/extensions/web-store/browser-extension-webstore-updater.ts +++ b/src/main/browser/extensions/web-store/browser-extension-webstore-updater.ts @@ -24,11 +24,13 @@ import { downloadWebStoreCrx, isAllowedCrxDownloadUrl } from './browser-extensio import { commitStagedWebStoreExtension, discardStagedWebStoreExtension, - isBrowserExtensionBusy, stageWebStoreExtension, - withBrowserExtensionLock, type StagedWebStoreExtension } from '../installation/browser-extension-webstore-install' +import { + isBrowserExtensionBusy, + withBrowserExtensionLock +} from '../installation/browser-extension-mutation-lock' /** * Background updates for store installs. Request shape follows electron-chrome-web-store's diff --git a/src/main/ipc/browser/browser.ts b/src/main/ipc/browser/browser.ts index de5ad39ab89..55f722092e5 100644 --- a/src/main/ipc/browser/browser.ts +++ b/src/main/ipc/browser/browser.ts @@ -26,7 +26,7 @@ import type { } from '../../../shared/browser-page-prompt' import { respondToBrowserPagePrompt } from '../../browser/guest-lifecycle/prompts/browser-page-prompts' import { recordActiveBrowserPage } from '../../browser/extensions/tabs-and-windows/browser-extension-tab-model' -import { announceRegisteredBrowserExtensionTab } from '../../browser/extensions/api/tabs-lifecycle' +import { announceRegisteredBrowserExtensionTab } from '../../browser/extensions/tabs-and-windows/browser-extension-tab-lifecycle' import { registerBrowserExtensionHandlers } from './browser-extensions' import { registerBrowserExtensionContextMenuHandlers } from './browser-extension-context-menu' import { diff --git a/src/main/managed-server/activation/orcad-exited-own-fence.test.ts b/src/main/managed-server/activation/orcad-exited-own-fence.test.ts index ac11f8bbd2c..61fa46ae2b0 100644 --- a/src/main/managed-server/activation/orcad-exited-own-fence.test.ts +++ b/src/main/managed-server/activation/orcad-exited-own-fence.test.ts @@ -202,20 +202,6 @@ describe.skipIf(process.platform === 'win32')('the held fence token record', () expect(held(store)).toContain('t-lost') }) - it('drops a token whose lock was never taken', async () => { - const { fence, store, options } = hostWithFence('t-foreign', []) - utimesSync(fence, new Date(), new Date()) - expect( - await withOrcadActivationLock( - options, - async () => 'ran', - () => 'held', - 't-busy' - ) - ).toBe('held') - expect(held(store)).not.toContain('t-busy') - }, 20_000) - it('never lets an unwritable record fail a fence operation', async () => { const { home, store, options } = hostWithFence('unused', []) rmSync(join(home, '.orca-remote'), { recursive: true }) diff --git a/src/main/managed-server/activation/orcad-exited-own-install-lock.test.ts b/src/main/managed-server/activation/orcad-exited-own-install-lock.test.ts index 30e2b4005ac..c05929b38ad 100644 --- a/src/main/managed-server/activation/orcad-exited-own-install-lock.test.ts +++ b/src/main/managed-server/activation/orcad-exited-own-install-lock.test.ts @@ -84,13 +84,6 @@ function versionDirWithLock(owner: string, heldToken: string) { describe.skipIf(process.platform === 'win32')( 'an install lock this desktop’s exited process left', () => { - it('never writes to the lock while proving its holder exited', async () => { - const { lock, proof } = versionDirWithLock('t-exited', 't-exited') - const before = statSync(lock).mtimeMs - await expect(proof.find(lock)).resolves.toBe('t-exited') - expect(statSync(lock).mtimeMs).toBe(before) - }) - // A successor replacing the proven-exited lock must never be aged or stolen. it('leaves a successor that replaced the lock after the proof alone', async () => { const { lock, store, proof, acquire } = versionDirWithLock('t-exited', 't-exited') diff --git a/src/main/managed-server/activation/orcad-exited-own-lock-windows.test.ts b/src/main/managed-server/activation/orcad-exited-own-lock-windows.test.ts index 2e33ab43be0..17c329bf1c0 100644 --- a/src/main/managed-server/activation/orcad-exited-own-lock-windows.test.ts +++ b/src/main/managed-server/activation/orcad-exited-own-lock-windows.test.ts @@ -53,36 +53,10 @@ function find(guardsStateMutation: boolean): Promise { return findExitedOwnLockToken(target, lockDir, { baseDir, guardsStateMutation }) } -const checkOps = (): string[] => remote.commands.filter((c) => c.includes('fence-exited-owner')) - describe('reclaiming this desktop’s exited lock on a Windows host', () => { - it('asks the host script about exited holders only', async () => { - hold({ token: 't-exited', pid: EXITED_PID }, { token: 't-live', pid: process.pid }) - remote.reply = 'EXITED_OWNER t-exited\n' - await expect(find(true)).resolves.toBe('t-exited') - const [op] = checkOps() - expect(op).toContain('t-exited') - expect(op).not.toContain('t-live') - expect(op).toMatch(/fence-exited-owner"? "?[^ ]*\.install-lock"? "?1"?/u) - }) - it('ignores an answer naming a token it did not offer', async () => { hold({ token: 't-exited', pid: EXITED_PID }) remote.reply = 'EXITED_OWNER t-foreign\n' await expect(find(false)).resolves.toBeNull() }) - - it('falls back to the stale window when the check would not fit cmd.exe', async () => { - hold({ token: 't-exited', pid: EXITED_PID }) - remote.reply = 'EXITED_OWNER t-exited\n' - const deep = `C:/Users/me/${'nested-folder/'.repeat(200)}.orca-remote` - const target = { conn: unusedOrcadHostTransport(), host } - await expect( - findExitedOwnLockToken(target, `${deep}/.install-lock`, { - baseDir: deep, - guardsStateMutation: true - }) - ).resolves.toBeNull() - expect(remote.commands).toEqual([]) - }) }) diff --git a/src/main/ssh/authentication/ssh-agent-identity-filter.test.ts b/src/main/ssh/authentication/ssh-agent-identity-filter.test.ts index 1a09e41f8a0..5efe234e2b1 100644 --- a/src/main/ssh/authentication/ssh-agent-identity-filter.test.ts +++ b/src/main/ssh/authentication/ssh-agent-identity-filter.test.ts @@ -85,30 +85,6 @@ describe('createIdentityFilteredAgent', () => { expect(mocks.readFileSync).toHaveBeenCalledWith(testHomePath('.ssh', 'work_key.pub')) }) - it('expands Windows-style configured identity file paths before filtering', async () => { - const allowedKey = makeKey('allowed') - mocks.readFileSync.mockReturnValue('ssh-ed25519 AAAA allowed') - mocks.parseKey.mockReturnValue(allowedKey) - mocks.createAgent.mockReturnValue({ - getIdentities: vi.fn((callback) => callback(undefined, [allowedKey])), - sign: vi.fn() - }) - - const agent = createIdentityFilteredAgent('/tmp/agent.sock', ['~\\.ssh\\work_key']) - const identities = await new Promise((resolve, reject) => { - agent?.getIdentities((error, keys) => { - if (error) { - reject(error) - return - } - resolve(keys ?? []) - }) - }) - - expect(identities).toEqual([allowedKey]) - expect(mocks.readFileSync).toHaveBeenCalledWith(testHomePath('.ssh', 'work_key.pub')) - }) - it('filters nested public key entries returned by ssh2 agents', async () => { const allowedKey = makeKey('allowed') const otherKey = makeKey('other') diff --git a/src/main/ssh/authentication/ssh-keyboard-interactive.test.ts b/src/main/ssh/authentication/ssh-keyboard-interactive.test.ts index f01c46b5871..43e4f362d3e 100644 --- a/src/main/ssh/authentication/ssh-keyboard-interactive.test.ts +++ b/src/main/ssh/authentication/ssh-keyboard-interactive.test.ts @@ -42,23 +42,6 @@ describe('isKeyboardInteractivePasswordPrompt', () => { }) describe('collectKeyboardInteractiveResponses', () => { - it('forwards verification prompts with instructions and the echo flag', async () => { - const requestCredential = vi.fn(async () => '1') - const session = createSession({ requestCredential }) - - const responses = await collectKeyboardInteractiveResponses(session, 'Choose an option:', [ - { prompt: 'Passcode or option (1-2):', echo: true } - ]) - - expect(responses).toEqual(['1']) - expect(requestCredential).toHaveBeenCalledWith( - 'target-1', - 'keyboard-interactive', - 'Choose an option:\nPasscode or option (1-2):', - true - ) - }) - it('answers a password prompt from the cache without prompting', async () => { const requestCredential = vi.fn(async () => 'unused') const session = createSession({ @@ -108,29 +91,6 @@ describe('collectKeyboardInteractiveResponses', () => { expect(session.isCancelled()).toBe(true) }) - it('short-circuits every later round once cancelled', async () => { - const requestCredential = vi.fn(async () => 'answer') - const session = createSession({ requestCredential }) - session.markCancelled() - - const responses = await collectKeyboardInteractiveResponses(session, '', [ - { prompt: 'Duo passcode:', echo: false } - ]) - - expect(responses).toBeNull() - expect(requestCredential).not.toHaveBeenCalled() - }) - - it('returns null when no credential prompter is available', async () => { - const session = createSession({ requestCredential: undefined }) - - const responses = await collectKeyboardInteractiveResponses(session, '', [ - { prompt: 'Password: ', echo: false } - ]) - - expect(responses).toBeNull() - }) - it('discards a password resolved after the attempt is cancelled', async () => { let answer: (value: string) => void = () => {} const requestCredential = vi @@ -153,17 +113,4 @@ describe('collectKeyboardInteractiveResponses', () => { expect(setCachedPassword).not.toHaveBeenCalled() expect(requestCredential).toHaveBeenCalledTimes(1) }) - - it('accepts an empty response without caching it as a password', async () => { - const requestCredential = vi.fn(async () => '') - const setCachedPassword = vi.fn() - const session = createSession({ requestCredential, setCachedPassword }) - - const responses = await collectKeyboardInteractiveResponses(session, '', [ - { prompt: 'Password: ', echo: false } - ]) - - expect(responses).toEqual(['']) - expect(setCachedPassword).not.toHaveBeenCalled() - }) }) diff --git a/src/main/ssh/connection/ssh-connection-direct-startup.test.ts b/src/main/ssh/connection/ssh-connection-direct-startup.test.ts deleted file mode 100644 index 7d2f9878b22..00000000000 --- a/src/main/ssh/connection/ssh-connection-direct-startup.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { describe, expect, it, vi, beforeEach } from 'vitest' -import { EventEmitter } from 'node:events' -import { - resetSshConnectionMocks, - spawnSystemSshCommandMock -} from './testing/ssh-connection-test-harness' -import { - createCallbacks, - createResolvedConfig, - createTarget -} from './testing/ssh-connection-test-fixtures' -import type { createSystemCommandChannel } from './testing/ssh-connection-test-fixtures' -import { SshConnection } from './ssh-connection' -import { resolveWithSshG } from '../config/ssh-g-config-resolution' - -vi.mock('../authentication/ssh2-module-load', async () => - (await import('./testing/ssh-connection-test-client')).createSsh2LoaderModule() -) -vi.mock('../system-transport/system-ssh-binary', async () => - (await import('./testing/ssh-connection-test-harness')).createSystemSshBinaryModule() -) -vi.mock('../system-transport/system-ssh-args', async (importOriginal) => - (await import('./testing/ssh-connection-test-harness')).withSystemSshArgsMocks( - await importOriginal() - ) -) -vi.mock('../system-transport/system-ssh-command', async (importOriginal) => - (await import('./testing/ssh-connection-test-harness')).withSystemSshCommandMocks( - await importOriginal() - ) -) -vi.mock( - '../system-transport/file-transfer/system-ssh-file-binary-transfer', - async (importOriginal) => - (await import('./testing/ssh-connection-test-harness')).withSystemSshFileBinaryTransferMocks( - await importOriginal() - ) -) -vi.mock('../system-transport/file-transfer/system-ssh-file-transfer', async (importOriginal) => - (await import('./testing/ssh-connection-test-harness')).withSystemSshFileTransferMocks( - await importOriginal() - ) -) -vi.mock('../system-transport/ssh-control-socket', async () => - (await import('./testing/ssh-connection-test-harness')).createControlSocketModule() -) -vi.mock('../config/ssh-config-parser', async () => - (await import('./testing/ssh-connection-test-harness')).createSshConfigParserModule() -) -vi.mock('../config/ssh-g-config-resolution', async (importOriginal) => - (await import('./testing/ssh-connection-test-harness')).withSshGConfigResolutionMocks( - await importOriginal() - ) -) - -describe('SshConnection', () => { - beforeEach(() => { - resetSshConnectionMocks() - }) - - it('removes system SSH probe listeners after timeout', async () => { - vi.useFakeTimers() - const channel = new EventEmitter() as ReturnType - channel.stdin = { end: vi.fn(), write: vi.fn() } - channel.stderr = new EventEmitter() - channel.close = vi.fn() - spawnSystemSshCommandMock.mockReturnValueOnce(channel) - vi.mocked(resolveWithSshG).mockResolvedValueOnce(createResolvedConfig()) - const conn = new SshConnection(createTarget({ configHost: 'fdpass-host' }), createCallbacks()) - - try { - const connect = expect(conn.connect()).rejects.toThrow('System SSH connection timed out') - await vi.advanceTimersByTimeAsync(30_000) - - await connect - expect(channel.close).toHaveBeenCalled() - expect(channel.listenerCount('data')).toBe(0) - // Lifetime observers remain until physical close, even after the probe timed out. - expect(channel.listenerCount('error')).toBe(2) - expect(channel.listenerCount('close')).toBe(2) - expect(channel.stderr.listenerCount('data')).toBe(0) - expect( - (conn as unknown as { systemCommandChannels: Set }).systemCommandChannels.size - ).toBe(0) - channel.emit('close', 0) - expect(channel.listenerCount('close')).toBe(0) - expect(channel.listenerCount('error')).toBe(1) - } finally { - vi.useRealTimers() - } - }) -}) diff --git a/src/main/ssh/legacy-relay/bridge/legacy-relay-client.test.ts b/src/main/ssh/legacy-relay/bridge/legacy-relay-client.test.ts index af7b544c86c..18964298bb7 100644 --- a/src/main/ssh/legacy-relay/bridge/legacy-relay-client.test.ts +++ b/src/main/ssh/legacy-relay/bridge/legacy-relay-client.test.ts @@ -67,7 +67,7 @@ vi.mock('../../protocol/ssh-channel-multiplexer', () => ({ } })) -import { LegacyRelayClient, LegacyRelayOwnerHeldError } from './legacy-relay-client' +import { LegacyRelayClient } from './legacy-relay-client' const GRANT = { protocolVersion: 1, @@ -103,14 +103,6 @@ beforeEach(() => { }) describe('the legacy relay client', () => { - it('never displaces an attached owner', async () => { - fake.respond = () => { - throw rpcError(-32044) - } - await expect(open()).rejects.toBeInstanceOf(LegacyRelayOwnerHeldError) - expect(fake.requests).toHaveLength(1) - }) - it('talks to a relay that predates owner claims', async () => { fake.respond = (method) => { if (method === 'pty.openClient') { @@ -120,9 +112,4 @@ describe('the legacy relay client', () => { } await expect(open()).resolves.toMatchObject({ ownerClaim: null }) }) - - it('refuses a grant that is not the owner role', async () => { - fake.respond = () => ({ ...GRANT, role: 'subscriber' }) - await expect(open()).rejects.toThrow('did not grant the terminal owner role') - }) }) diff --git a/src/main/ssh/legacy-relay/bridge/legacy-relay-hidden-output.test.ts b/src/main/ssh/legacy-relay/bridge/legacy-relay-hidden-output.test.ts index 7b181ff9b13..e0d5d71ad36 100644 --- a/src/main/ssh/legacy-relay/bridge/legacy-relay-hidden-output.test.ts +++ b/src/main/ssh/legacy-relay/bridge/legacy-relay-hidden-output.test.ts @@ -20,19 +20,6 @@ beforeEach(() => { }) describe('a bridged pane no visible view shows', () => { - it('sends none of its output to the window, and one fresh screen once shown', async () => { - subject.setPaused(PTY, true) - for (let i = 0; i < 1000; i++) { - subject.onData(PTY, 'x'.repeat(4096)) - } - expect(sent).toEqual([]) - subject.setPaused(PTY, false) - await vi.waitFor(() => expect(sent).toEqual(['replay:SCREEN'])) - expect(reread).toHaveBeenCalledTimes(1) - subject.onData(PTY, 'live') - expect(sent).toEqual(['replay:SCREEN', 'data:live']) - }) - it('keeps output printed after the snapshot behind the screen it follows', async () => { let snapshot: (replay: string) => void = () => {} let finish: () => void = () => {} @@ -80,12 +67,6 @@ describe('a bridged pane no visible view shows', () => { expect(sent.at(-1)).toBe('data:c') }) - it('still sends kitty, window-size and capability queries', () => { - subject.setPaused(PTY, true) - subject.onData(PTY, 'a\x1b[?ub\x1b[18tc\x1bP+q544e\x1b\\d') - expect(sent).toEqual(['data:\x1b[?u\x1b[18t\x1bP+q544e\x1b\\']) - }) - it('reopens an oversized OSC a reveal cut, so its payload never prints', async () => { reread.mockRejectedValueOnce(new Error('ssh down')) subject.setPaused(PTY, true) @@ -101,22 +82,6 @@ describe('a bridged pane no visible view shows', () => { expect(sent).toEqual(['data:shown\x1b[', 'data:\x1b[c']) }) - it('carries a query split across chunks until it completes', () => { - subject.setPaused(PTY, true) - subject.onData(PTY, 'bulk\x1b[') - subject.onData(PTY, 'c') - expect(sent).toEqual(['data:\x1b[c']) - }) - - it('still sends command marks, titles and bells, but not an oversized OSC', () => { - subject.setPaused(PTY, true) - subject.onData(PTY, 'out\x1b]133;D;0\x07text\x1b]0;') - subject.onData(PTY, 'title\x1b\\ding\x07') - subject.onData(PTY, `\x1b]1337;File=${'x'.repeat(5000)}`) - subject.onData(PTY, 'y\x07after\x07') - expect(sent).toEqual(['data:\x1b]133;D;0\x07', 'data:\x1b]0;title\x1b\\\x07', 'data:\x07']) - }) - it('re-reads output a hidden pane printed after its reattach’s screen', async () => { subject.setPaused(PTY, true) subject.screenSent(PTY) diff --git a/src/main/ssh/legacy-relay/bridge/legacy-relay-move-retry.test.ts b/src/main/ssh/legacy-relay/bridge/legacy-relay-move-retry.test.ts index 5d710c43870..100259656c6 100644 --- a/src/main/ssh/legacy-relay/bridge/legacy-relay-move-retry.test.ts +++ b/src/main/ssh/legacy-relay/bridge/legacy-relay-move-retry.test.ts @@ -73,17 +73,6 @@ afterEach(() => { }) describe('the last bridged relay terminal', () => { - it('reports once when the relay proves the only pane exited', async () => { - startHost() - await startLegacyRelayBridge(TARGET) - - relay.ptys.delete('pty-1') - relay.clients[0].emitExit('pty-1', 0) - - expect(lastExits).toEqual([TARGET]) - expect(legacyRelayBridgedPanes()).toEqual([]) - }) - it('never reports lost contact as exit', async () => { startHost() await startLegacyRelayBridge(TARGET) diff --git a/src/main/ssh/legacy-relay/endpoints/ssh-host-relay-windows-inventory.test.ts b/src/main/ssh/legacy-relay/endpoints/ssh-host-relay-windows-inventory.test.ts index 35a329449d9..10f04e478e8 100644 --- a/src/main/ssh/legacy-relay/endpoints/ssh-host-relay-windows-inventory.test.ts +++ b/src/main/ssh/legacy-relay/endpoints/ssh-host-relay-windows-inventory.test.ts @@ -5,7 +5,6 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import { - windowsPipesNotRunHere, parseWindowsRelayInventory, WINDOWS_RELAY_INVENTORY_JS } from './ssh-host-relay-windows-inventory' @@ -59,13 +58,4 @@ describe('the Windows relay inventory', () => { ) ).toBeNull() }) - - it("sets aside only pipes the host proved another account's or gone", () => { - expect( - windowsPipesNotRunHere( - JSON.stringify({ '\\\\.\\pipe\\A': 'EACCES', '\\\\.\\pipe\\B': 'ok', C: 'ENOENT' }) - ) - ).toEqual(new Set(['\\\\.\\pipe\\a', 'c'])) - expect(windowsPipesNotRunHere('garbage')).toEqual(new Set()) - }) }) diff --git a/src/renderer/src/app-shell/use-runtime-graph-sync.performance.test.tsx b/src/renderer/src/app-shell/use-runtime-graph-sync.performance.test.tsx new file mode 100644 index 00000000000..4e34214bf13 --- /dev/null +++ b/src/renderer/src/app-shell/use-runtime-graph-sync.performance.test.tsx @@ -0,0 +1,58 @@ +// @vitest-environment happy-dom +import { act, cleanup, renderHook } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { useAppStore } from '../store' +import { makeAgentStatusEntry } from '../runtime/sync-runtime-graph/testing/sync-runtime-graph-test-harness' +import { scheduleRuntimeGraphSync } from '../runtime/sync-runtime-graph/graph-sync-scheduler' +import { useRuntimeGraphSync } from './use-runtime-graph-sync' + +vi.mock('../runtime/sync-runtime-graph/graph-sync-scheduler', () => ({ + scheduleRuntimeGraphSync: vi.fn(), + setRuntimeGraphStoreStateGetter: vi.fn(), + setRuntimeGraphSyncEnabled: vi.fn() +})) +const initialState = useAppStore.getInitialState() +afterEach(() => { + cleanup() + useAppStore.setState(initialState, true) + vi.clearAllMocks() +}) +// Same-bucket heartbeats change no mobile data; a state transition requires one publication. +describe('runtime graph heartbeat publication contract', () => { + it.each([50, 500])('does not schedule heartbeats at %i panes', (count) => { + useAppStore.setState(initialState, true) + const entries = Object.fromEntries( + Array.from({ length: count }, (_, index) => { + const paneKey = `tab-${index}:11111111-1111-4111-8111-111111111111` + return [ + paneKey, + makeAgentStatusEntry({ paneKey, updatedAt: 1_800_000, stateStartedAt: 1_800_000 }) + ] + }) + ) + useAppStore.setState({ agentStatusByPaneKey: entries }) + renderHook(() => useRuntimeGraphSync()) + vi.mocked(scheduleRuntimeGraphSync).mockClear() + const key = Object.keys(entries)[9] + for (let tick = 1; tick <= 20; tick += 1) { + act(() => + useAppStore.setState({ + agentStatusByPaneKey: { + ...useAppStore.getState().agentStatusByPaneKey, + [key]: { ...entries[key], updatedAt: 1_800_000 + tick * 1_000 } + } + }) + ) + expect(scheduleRuntimeGraphSync).toHaveBeenCalledTimes(0) + } + act(() => + useAppStore.setState({ + agentStatusByPaneKey: { + ...useAppStore.getState().agentStatusByPaneKey, + [key]: { ...useAppStore.getState().agentStatusByPaneKey[key], state: 'blocked' } + } + }) + ) + expect(scheduleRuntimeGraphSync).toHaveBeenCalledTimes(1) + }) +}) diff --git a/src/renderer/src/components/activity/use-agent-pane-threads.performance.test.tsx b/src/renderer/src/components/activity/use-agent-pane-threads.performance.test.tsx new file mode 100644 index 00000000000..16b9950055c --- /dev/null +++ b/src/renderer/src/components/activity/use-agent-pane-threads.performance.test.tsx @@ -0,0 +1,98 @@ +// @vitest-environment happy-dom +import { act, cleanup, renderHook } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { AgentStatusEntry } from '../../../../shared/agent-status/contracts/agent-status-types' +import { makePaneKey } from '../../../../shared/workspace-layout/stable-pane-id' +import { useAppStore } from '@/store' +import { + LEAF_ID, + makeRepo, + makeTabWithIds, + makeWorktree +} from './ActivityPrototypePage-test-fixtures' + +import { useAgentPaneThreads } from './use-agent-pane-threads' + +vi.mock('@/store', async () => { + const { createTestStore } = await import('@/store/testing/app-store-test-fixture') + return { useAppStore: createTestStore() } +}) + +afterEach(() => { + cleanup() + vi.restoreAllMocks() +}) + +function installPanes(count: number): string[] { + vi.spyOn(Date, 'now').mockReturnValue(100_000) + const repo = makeRepo() + const worktree = makeWorktree() + const tabs = Array.from({ length: count }, (_, index) => + makeTabWithIds(`tab-${index}`, worktree.id) + ) + const entries: Record = {} + for (const tab of tabs) { + const paneKey = makePaneKey(tab.id, LEAF_ID) + entries[paneKey] = { + paneKey, + state: 'working', + prompt: `Task ${tab.id}`, + agentType: 'claude', + updatedAt: 100_000, + stateStartedAt: 99_000, + stateHistory: [] + } + } + useAppStore.setState({ + agentStatusByPaneKey: entries, + retainedAgentsByPaneKey: {}, + migrationUnsupportedByPtyId: {}, + runtimeAgentOrchestrationByPaneKey: {}, + acknowledgedAgentsByPaneKey: {}, + activityClearedAtByPaneKey: {}, + repos: [repo], + worktreesByRepo: { [repo.id]: [worktree] }, + folderWorkspaces: [], + detectedWorktreesByRepo: {}, + tabsByWorktree: { [worktree.id]: tabs }, + unifiedTabsByWorktree: {}, + agentsVisibleHostIds: null, + agentsFilterRepoIds: [] + }) + return Object.keys(entries) +} + +describe('agent thread list reuse', () => { + it.each([40, 400])('rebuilds only the pinged thread among %i panes', (count) => { + const panes = installPanes(count) + const activity = renderHook(() => + useAgentPaneThreads({ + query: '', + readFilter: 'all', + groupBy: 'none', + selectedPaneKey: null, + showChildAgents: true + }) + ) + const before = activity.result.current.allThreads + expect(before).toHaveLength(count) + const pinged = panes[9] + + act(() => { + const entries = useAppStore.getState().agentStatusByPaneKey + useAppStore.setState({ + agentStatusByPaneKey: { + ...entries, + [pinged]: { ...entries[pinged], prompt: 'Follow-up prompt' } + } + }) + }) + + const after = activity.result.current.allThreads + expect(after.map((thread) => thread.paneKey)).toEqual(before.map((thread) => thread.paneKey)) + // Memoized rows compare thread identity; only the pinged pane's row may see a new object. + const rebuilt = after.filter((thread, index) => thread !== before[index]) + expect(rebuilt.map((thread) => thread.paneKey)).toEqual([pinged]) + expect(rebuilt[0]?.currentAgentEntry?.prompt).toBe('Follow-up prompt') + }) +}) diff --git a/src/renderer/src/components/native-chat/markdown/NativeChatMarkdown.streaming.performance.test.tsx b/src/renderer/src/components/native-chat/markdown/NativeChatMarkdown.streaming.performance.test.tsx new file mode 100644 index 00000000000..809968ac7ce --- /dev/null +++ b/src/renderer/src/components/native-chat/markdown/NativeChatMarkdown.streaming.performance.test.tsx @@ -0,0 +1,66 @@ +// @vitest-environment happy-dom +import { cleanup, render } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type * as ReactMarkdownModule from 'react-markdown' + +const markdownInput = vi.hoisted(() => ({ chars: 0 })) +vi.mock('react-markdown', async (importOriginal) => { + const actual = await importOriginal() + const { createElement } = await import('react') + return { + ...actual, + default: (props: Parameters[0]) => { + markdownInput.chars += typeof props.children === 'string' ? props.children.length : 0 + return createElement(actual.default, props) + } + } +}) +vi.mock('@/store/index', () => ({ + useAppStore: (selector: (state: { settings: { theme: string } }) => unknown) => + selector({ settings: { theme: 'light' } }) +})) + +import { NativeChatMarkdown } from './NativeChatMarkdown' +import { TooltipProvider } from '@/components/ui/tooltip' + +const BLOCK_CHARS = 200 +const TOKEN = ' tick' + +function paragraphs(count: number): string { + return Array.from({ length: count }, (_, index) => + `Paragraph ${index} ${'lorem '.repeat(BLOCK_CHARS)}`.slice(0, BLOCK_CHARS) + ).join('\n\n') +} + +function reply(content: string): React.JSX.Element { + return ( + + + + ) +} + +afterEach(cleanup) + +describe('streaming native chat markdown parse budget', () => { + it.each([10, 100])( + 'hands react-markdown only the tail of a %i-block reply per append', + (blocks) => { + let content = paragraphs(blocks) + const { container, rerender } = render(reply(content)) + const firstParagraph = container.querySelector('p') + expect(firstParagraph).not.toBeNull() + let widestAppend = 0 + for (let tick = 0; tick < 20; tick += 1) { + content += TOKEN + markdownInput.chars = 0 + rerender(reply(content)) + widestAppend = Math.max(widestAppend, markdownInput.chars) + } + // Only the unsettled tail: two 200-char blocks, the last grown by 20 x 5-char tokens, plus a separator. + expect(widestAppend).toBeLessThanOrEqual(505) + expect(container.querySelector('p')).toBe(firstParagraph) + expect(container.textContent).toContain(TOKEN.repeat(20).trim()) + } + ) +}) diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.composer-typing.performance.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.composer-typing.performance.test.tsx new file mode 100644 index 00000000000..45c9f87c2d2 --- /dev/null +++ b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.composer-typing.performance.test.tsx @@ -0,0 +1,166 @@ +// @vitest-environment happy-dom + +import type * as LaunchRegistry from '@/lib/agent-session/launch/structured-agent-session-launch-registry' +import type * as LaunchOptions from '@/lib/agent-session/launch/structured-agent-session-launch-options' +import { act, cleanup, render, screen } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { AgentJournalRenderItem } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' + +const { mocks, moduleFactories, resetStructuredSessionMocks, launchModule } = await vi.hoisted( + async () => { + const setup = ( + await import('./NativeChatStructuredSession.test-harness') + ).createStructuredSessionMocks() + return { + ...setup, + launchModule: setup.moduleFactories.structuredAgentSessionLaunch() + } + } +) + +vi.mock('@/lib/agent-session/launch/structured-agent-session-launch', () => ({ + retryStructuredAgentSessionLaunch: launchModule.retryStructuredAgentSessionLaunch, + relaunchFailedStructuredAgentSessionForMessage: + launchModule.relaunchFailedStructuredAgentSessionForMessage +})) +vi.mock( + '@/lib/agent-session/launch/structured-agent-session-launch-registry', + async (importOriginal) => ({ + ...(await importOriginal()), + getStructuredAgentSessionLaunchLifecycle: launchModule.getStructuredAgentSessionLaunchLifecycle, + getStructuredAgentSessionLaunchResumes: launchModule.getStructuredAgentSessionLaunchResumes, + useStructuredAgentSessionLaunchLifecycle: launchModule.useStructuredAgentSessionLaunchLifecycle, + useStructuredAgentSessionLaunchFailure: launchModule.useStructuredAgentSessionLaunchFailure + }) +) +vi.mock( + '@/lib/agent-session/launch/structured-agent-session-launch-options', + async (importOriginal) => ({ + ...(await importOriginal()), + useStructuredAgentSessionLaunchSelection: launchModule.useStructuredAgentSessionLaunchSelection + }) +) +vi.mock('@/lib/agent-session/launch/structured-agent-session-launch-message', () => + moduleFactories.structuredAgentSessionLaunchMessage() +) +vi.mock('../../../../runtime/native-chat/requests/structured-agent-session-call', () => + moduleFactories.structuredAgentSessionCall() +) +vi.mock('../../../../runtime/native-chat/requests/structured-agent-session-subscriptions', () => + moduleFactories.structuredAgentSessionSubscriptions() +) +vi.mock('@/components/machine-identity/use-published-machine-name', () => ({ + usePublishedMachineName: () => null +})) +vi.mock('../../session/structured/use-structured-agent-session', () => + moduleFactories.useStructuredAgentSession() +) +vi.mock('../../appearance/use-native-chat-font-size', () => moduleFactories.useNativeChatFontSize()) +vi.mock('../../links/actions/use-native-chat-file-link-context', () => + moduleFactories.useNativeChatFileLinkContext() +) +vi.mock('../../pane/ownership/use-native-chat-tab-owner', () => + moduleFactories.useNativeChatTabOwner() +) +vi.mock('../../links/actions/use-native-chat-file-link-click', () => + moduleFactories.useNativeChatFileLinkClick() +) +vi.mock('../../NativeChatMessageList', () => moduleFactories.nativeChatMessageList()) +vi.mock('../../host-access/NativeChatEmptyState', () => moduleFactories.nativeChatEmptyState()) +vi.mock('../../prompts/NativeChatApprovalCard', () => moduleFactories.nativeChatApprovalCard()) +vi.mock('../../prompts/NativeChatQuestionCard', () => moduleFactories.nativeChatQuestionCard()) +vi.mock('@/runtime/hosts/installation/use-managed-runtime-install', () => ({ + useManagedRuntimeInstall: () => ({ kind: 'inactive' }) +})) + +import { NativeChatStructuredSession } from '../../NativeChatStructuredSession' +import { clearNativeChatComposerDraftsForTests } from '../../drafts/native-chat-draft-reset' +import { hydrateNativeChatComposerDrafts } from '../../drafts/native-chat-composer-draft-load' +import { updateNativeChatComposerDraft } from '../../drafts/native-chat-composer-draft-store' +import { structuredAgentSessionDraftScopeKey } from '../../drafts/native-chat-draft-scope' +import { TooltipProvider } from '@/components/ui/tooltip' +import { promptTextContent } from '../../composer/editor/native-chat-prompt-document-text' +import { + createMemoryNativeChatComposerDraftStorage, + setNativeChatComposerDraftStorageForTests +} from '../../drafts/native-chat-composer-draft-storage' +import { useAppStore } from '@/store' + +afterEach(() => { + cleanup() + resetStructuredSessionMocks() + clearNativeChatComposerDraftsForTests() +}) + +function journalMessage(index: number): AgentJournalRenderItem { + return { + itemId: `message-${index}`, + revision: 1, + sequence: index + 1, + observedAt: index + 1, + body: { + kind: 'message', + role: index % 2 === 0 ? 'user' : 'assistant', + blocks: [{ type: 'text', text: `Message ${index}` }] + } + } +} + +function session(): React.JSX.Element { + return ( + + + + ) +} + +describe('structured session composer typing', () => { + it.each([50, 500])( + 'renders no transcript while typing beside %i journal items', + async (count) => { + clearNativeChatComposerDraftsForTests() + setNativeChatComposerDraftStorageForTests(createMemoryNativeChatComposerDraftStorage()) + await hydrateNativeChatComposerDrafts() + mocks.journalItems = Array.from({ length: count }, (_, index) => journalMessage(index)) + mocks.messages = mocks.journalItems.map((item) => ({ + id: item.itemId, + role: 'assistant', + source: 'transcript', + timestamp: item.observedAt, + blocks: [{ type: 'text', text: item.itemId }] + })) + render(session()) + await screen.findByRole('textbox') + const scopeKey = structuredAgentSessionDraftScopeKey('session-1') + let storePublications = 0 + const unsubscribe = useAppStore.subscribe(() => { + storePublications += 1 + }) + const rendersBefore = mocks.messageListRenders + expect(rendersBefore).toBeGreaterThan(0) + let text = '' + for (let keystroke = 0; keystroke < 20; keystroke += 1) { + text += 'a' + act(() => { + updateNativeChatComposerDraft( + scopeKey, + { text, document: promptTextContent(text) }, + 'deferred' + ) + }) + } + unsubscribe() + // Typing reached the composer, through its own scope store; the transcript never heard it. + expect(screen.getByRole('textbox').textContent).toContain('a'.repeat(20)) + expect(mocks.messageListRenders - rendersBefore).toBe(0) + expect(storePublications).toBe(0) + } + ) +}) diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test-harness.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test-harness.tsx index 26cee219bc9..3d014c8deaa 100644 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test-harness.tsx +++ b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test-harness.tsx @@ -93,6 +93,7 @@ export function createStructuredSessionMocks() { readRefusal: absent(), messages: null as null | unknown[], messageListProps: initialMessageListProps, + messageListRenders: 0, composerProps: nullable<{ launchSeed?: NativeChatLaunchSeed structuredTransport?: Record & { @@ -308,6 +309,7 @@ export function createStructuredSessionMocks() { nativeChatMessageList: () => ({ NativeChatMessageList: (props: typeof mocks.messageListProps) => { mocks.messageListProps = props + mocks.messageListRenders += 1 useImperativeHandle(props?.ref, () => ({ revealLatest: mocks.revealLatest, readerAtEnd: () => true, @@ -352,6 +354,7 @@ export function createStructuredSessionMocks() { mocks.readRefusal = undefined mocks.messages = null mocks.messageListProps = null + mocks.messageListRenders = 0 mocks.composerProps = null mocks.approvalCardProps = null mocks.questionCardProps = null diff --git a/src/renderer/src/components/native-chat/viewport/virtualization/NativeChatMessageList.windowing.performance.test.tsx b/src/renderer/src/components/native-chat/viewport/virtualization/NativeChatMessageList.windowing.performance.test.tsx new file mode 100644 index 00000000000..be16ba53a87 --- /dev/null +++ b/src/renderer/src/components/native-chat/viewport/virtualization/NativeChatMessageList.windowing.performance.test.tsx @@ -0,0 +1,50 @@ +// @vitest-environment happy-dom + +import { cleanup } from '@testing-library/react' +import { render } from '../../messages/actions/native-chat-tooltip-test-harness' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { list, marker, stubLayout, windowState } from './native-chat-windowing-test-harness' + +let restoreLayout = (): void => {} +beforeEach(() => { + restoreLayout = stubLayout() +}) +afterEach(() => { + cleanup() + restoreLayout() +}) + +function mountedRows(container: HTMLElement): Map { + return new Map( + Array.from(container.querySelectorAll('[data-index]'), (row) => [ + Number(row.dataset.index), + row + ]) + ) +} + +// ceil(600px viewport / 44px row pitch) = 14 visible, 2 x 6 overscan, and the pinned latest row. +const MOUNTED_ROW_BOUND = 27 + +describe('windowed transcript row budget', () => { + it.each([200, 2_000])('mounts a bounded row set from a %i-message transcript', (count) => { + const transcript = Array.from({ length: count }, (_, index) => marker(index)) + const { container, rerender } = render(list(transcript)) + const opened = windowState(container) + expect(opened.indexes.length).toBeLessThanOrEqual(MOUNTED_ROW_BOUND) + expect(opened.indexes).toContain(count - 1) + const before = mountedRows(container) + + rerender(list([...transcript, marker(count)])) + const appended = windowState(container) + expect(appended.indexes.length).toBeLessThanOrEqual(MOUNTED_ROW_BOUND) + expect(appended.indexes).toContain(count) + expect(container.textContent).toContain(`marker-${count}`) + const after = mountedRows(container) + const kept = [...before].filter(([index]) => after.has(index)) + expect(kept.length).toBeGreaterThan(0) + for (const [index, row] of kept) { + expect(after.get(index)).toBe(row) + } + }) +}) diff --git a/src/renderer/src/components/sidebar/markdown-parse-meter.test-fixture.ts b/src/renderer/src/components/sidebar/markdown-parse-meter.test-fixture.ts deleted file mode 100644 index 8c4d72be00e..00000000000 --- a/src/renderer/src/components/sidebar/markdown-parse-meter.test-fixture.ts +++ /dev/null @@ -1,51 +0,0 @@ -import type { Nodes, Root } from 'mdast' - -type ParseWork = { - discovery: string[] - render: string[] - shifts: number - cloneNodes: number - cloneBytes: number - scans: { source: string; marker: string }[] - blocks: number -} - -export function createParseWork(): ParseWork { - return { - discovery: [], - render: [], - shifts: 0, - cloneNodes: 0, - cloneBytes: 0, - scans: [], - blocks: 0 - } -} - -export function nodeCount(node: Nodes): number { - return 1 + ('children' in node ? node.children.reduce((n, child) => n + nodeCount(child), 0) : 0) -} - -// Meter actual coordinate writes, leaving the parser's values and clone semantics unchanged. -export function meterPositions(tree: Root, onWrite: () => void): void { - function visit(node: Nodes): void { - if (node.position) { - for (const point of [node.position.start, node.position.end]) { - let offset = point.offset - Object.defineProperty(point, 'offset', { - enumerable: true, - configurable: true, - get: () => offset, - set: (value: number) => { - onWrite() - offset = value - } - }) - } - } - if ('children' in node) { - node.children.forEach(visit) - } - } - visit(tree) -} diff --git a/src/renderer/src/components/sidebar/markdown-top-level-blocks-legacy.test-fixture.ts b/src/renderer/src/components/sidebar/markdown-top-level-blocks-legacy.test-fixture.ts deleted file mode 100644 index fb50574b17a..00000000000 --- a/src/renderer/src/components/sidebar/markdown-top-level-blocks-legacy.test-fixture.ts +++ /dev/null @@ -1,103 +0,0 @@ -// Frozen splitter: differential tests compare every streaming prefix with the previous renderer. -// Reparse the last two blocks on append; cross-block definitions or HTML fall back to the whole document. - -import type { Nodes } from 'mdast' -import remarkGfm from 'remark-gfm' -import remarkParse from 'remark-parse' -import { remarkChatMath } from './chat-markdown-math' -import { chatMathCodeFences } from './chat-markdown-math-nodes' -import { unified } from 'unified' - -export type MarkdownBlock = { - /** Source offset, retained for settled blocks on append. */ - start: number - /** One-based source line. */ - line: number - text: string - literalMath?: boolean - /** Parser-owned end, excluding separators held for the next block. */ - end?: number -} - -export type MarkdownBlockSplit = { - source: string - blocks: readonly MarkdownBlock[] - /** Cross-block content requires whole-document rendering. */ - whole: boolean -} - -/** The last block, which is still growing, and the one it may yet join. */ -const UNSETTLED_BLOCKS = 2 - -const blockParser = unified().use(remarkParse).use(remarkGfm) -const mathBlockParser = blockParser().use(remarkChatMath) - -/** Tags that cannot hold anything, so cannot hold a later block. */ -const VOID_TAG_PATTERN = /^<(?:br|hr|wbr|img)\b[^>]*>$/i - -/** References and non-void HTML can affect other blocks and require whole-document parsing. */ -function reachesAcrossBlocks(node: Nodes): boolean { - if (node.type === 'definition' || node.type === 'footnoteDefinition') { - return true - } - if (node.type === 'html') { - return !VOID_TAG_PATTERN.test(node.value.trim()) - } - return 'children' in node && node.children.some(reachesAcrossBlocks) -} - -function hasLiteralMath(node: Nodes | undefined): boolean { - if (!node) { - return false - } - return ( - node.data?.mathComplete === false || - node.data?.mathLiteral === true || - ('children' in node && node.children.some(hasLiteralMath)) - ) -} - -function wholeDocument(source: string, literalMath = false): MarkdownBlockSplit { - return { source, blocks: [{ start: 0, line: 1, text: source, literalMath }], whole: true } -} - -export function splitMarkdownTopLevelBlocks( - source: string, - previous: MarkdownBlockSplit | null, - math = false -): MarkdownBlockSplit { - const grewFromPrevious = previous !== null && source.startsWith(previous.source) - if (grewFromPrevious && previous.whole && !math) { - return wholeDocument(source) - } - const settled = grewFromPrevious ? previous.blocks.slice(0, -UNSETTLED_BLOCKS) : [] - const reparsed = grewFromPrevious ? previous.blocks.at(-UNSETTLED_BLOCKS) : undefined - const base = reparsed?.start ?? 0 - const baseLine = reparsed?.line ?? 1 - const tail = source.slice(base) - const tree = (math ? mathBlockParser : blockParser).parse(tail) - if (math) { - chatMathCodeFences(tree, tail) - } - if (reachesAcrossBlocks(tree)) { - return wholeDocument(source, math && hasLiteralMath(tree)) - } - // Each block runs to the next one's start, so the blank lines between stay in the source. - const points = tree.children.map((child) => - 'position' in child ? child.position?.start : undefined - ) - const starts = points.map((point) => point?.offset ?? 0) - starts[0] = 0 - const blocks = starts.map((start, index) => ({ - start: base + start, - line: baseLine + (index === 0 ? 0 : (points[index]?.line ?? 1) - 1), - text: tail.slice(start, starts[index + 1]), - ...(math - ? { - literalMath: hasLiteralMath(tree.children[index]), - end: base + (tree.children[index]?.position?.end.offset ?? tail.length) - } - : {}) - })) - return { source, blocks: [...settled, ...blocks], whole: false } -} diff --git a/src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-activity-status.performance.test.tsx b/src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-activity-status.performance.test.tsx new file mode 100644 index 00000000000..d7ecad94d71 --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-activity-status.performance.test.tsx @@ -0,0 +1,76 @@ +// @vitest-environment happy-dom +import { useEffect } from 'react' +import { act, cleanup, render } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { useAppStore } from '@/store' +import { buildAgentStatusScaleFixture } from '@/store/slices/agent-status/agent-status-scale.test-fixture' +import { useWorktreeActivityStatus } from './use-worktree-activity-status' +import { useWorktreeActivityStatuses } from './use-worktree-activity-statuses' + +const initialState = useAppStore.getInitialState() + +afterEach(() => { + cleanup() + useAppStore.setState(initialState, true) + vi.useRealTimers() +}) + +// One permission transition changes one card; sidebar sizing changes no status output. +describe('sidebar activity status subscription contract', () => { + it.each([40, 400])('isolates a status transition among %i cards', (count) => { + vi.useFakeTimers() + vi.setSystemTime(2_000_000_000) + const fixture = buildAgentStatusScaleFixture(undefined, count) + useAppStore.setState(initialState, true) + useAppStore.setState({ + ...fixture.state, + agentStatusByPaneKey: fixture.agentStatusByPaneKey, + refreshGitHubForWorktreeIfStale: vi.fn().mockResolvedValue(undefined) + }) + const ids = Object.keys(fixture.state.tabsByWorktree) + let commits = 0 + let picker: ReturnType | undefined + const statuses = new Map() + function Probe({ worktreeId }: { worktreeId: string }) { + const status = useWorktreeActivityStatus(worktreeId) + useEffect(() => { + commits += 1 + statuses.set(worktreeId, status) + }) + return null + } + function Picker() { + picker = useWorktreeActivityStatuses(ids) + return null + } + render( + <> + + {ids.map((id) => ( + + ))} + + ) + commits = 0 + const index = fixture.owners.findIndex((owner) => owner.worktreeId === 'wt-17') + act(() => + useAppStore + .getState() + .setAgentStatus( + fixture.paneKeys[index], + { state: 'blocked', agentType: 'claude', prompt: 'permission requested' }, + undefined, + { updatedAt: 2_000_000_001 }, + fixture.owners[index] + ) + ) + expect(commits).toBe(1) + expect(statuses.get('wt-17')).toBe('permission') + expect(picker?.get('wt-17')).toBe('permission') + const previousPicker = picker + commits = 0 + act(() => useAppStore.setState({ sidebarWidth: 333 })) + expect(commits).toBe(0) + expect(picker).toBe(previousPicker) + }) +}) diff --git a/src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-agent-rows.performance.test.tsx b/src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-agent-rows.performance.test.tsx new file mode 100644 index 00000000000..ef457c4dcf9 --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-card/activity/use-worktree-agent-rows.performance.test.tsx @@ -0,0 +1,63 @@ +// @vitest-environment happy-dom +import { useEffect } from 'react' +import { act, cleanup, render } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { useAppStore } from '@/store' +import { buildAgentStatusScaleFixture } from '@/store/slices/agent-status/agent-status-scale.test-fixture' +import { useWorktreeAgentRows } from './useWorktreeAgentRows' + +const initialState = useAppStore.getInitialState() + +afterEach(() => { + cleanup() + useAppStore.setState(initialState, true) + vi.useRealTimers() +}) + +// Only the card owning the changed prompt has new rows; a same-freshness epoch changes none. +describe('worktree agent row subscription contract', () => { + it.each([40, 400])('isolates a status ping among %i cards', (count) => { + vi.useFakeTimers() + vi.setSystemTime(2_000_000_000) + const fixture = buildAgentStatusScaleFixture(undefined, count) + useAppStore.setState(initialState, true) + useAppStore.setState({ ...fixture.state, agentStatusByPaneKey: fixture.agentStatusByPaneKey }) + let commits = 0 + const prompts = new Map() + function Probe({ worktreeId }: { worktreeId: string }) { + const rows = useWorktreeAgentRows(worktreeId, true) + useEffect(() => { + commits += 1 + prompts.set(worktreeId, rows[0]?.entry.prompt ?? '') + }) + return null + } + render( + <> + {Object.keys(fixture.state.tabsByWorktree).map((id) => ( + + ))} + + ) + commits = 0 + const index = fixture.owners.findIndex((owner) => owner.worktreeId === 'wt-17') + act(() => + useAppStore + .getState() + .setAgentStatus( + fixture.paneKeys[index], + { state: 'working', agentType: 'claude', prompt: 'changed prompt' }, + undefined, + { updatedAt: 2_000_000_001 }, + fixture.owners[index] + ) + ) + expect(commits).toBe(1) + expect(prompts.get('wt-17')).toBe('changed prompt') + commits = 0 + act(() => + useAppStore.setState({ agentStatusEpoch: useAppStore.getState().agentStatusEpoch + 1 }) + ) + expect(commits).toBe(0) + }) +}) diff --git a/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-live-index-patch.ts b/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-live-index-patch.ts index ca86090a00a..fbcac1c47c0 100644 --- a/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-live-index-patch.ts +++ b/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-live-index-patch.ts @@ -14,15 +14,6 @@ export type LiveEntriesByWorktreeCache = { entriesByWorktree: Map } -// Count full rebuilds to verify same-key status pings take the incremental path. -let liveEntriesFullRebuildCount = 0 -export function getLiveEntriesFullRebuildCountForTests(): number { - return liveEntriesFullRebuildCount -} -export function recordLiveEntriesFullRebuild(): void { - liveEntriesFullRebuildCount += 1 -} - // Local completed orphans stay hidden; remote rows await host retraction, not tab hydration. export function liveEntryWorktreeId( paneKey: string, diff --git a/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-row-selectors.ts b/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-row-selectors.ts index ea6312970a3..12b6f915d6c 100644 --- a/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-row-selectors.ts +++ b/src/renderer/src/components/sidebar/workspace-card/activity/worktree-agent-row-selectors.ts @@ -7,8 +7,7 @@ import { parsePaneKey } from '../../../../../../shared/workspace-layout/stable-p import { type LiveEntriesByWorktreeCache, liveEntryWorktreeId, - patchLiveEntriesByWorktree, - recordLiveEntriesFullRebuild + patchLiveEntriesByWorktree } from './worktree-agent-live-index-patch' import './worktree-agent-orchestration-index' import { createWorktreeRecordSelector } from '@/store/terminals/worktree-terminal-record-selector' @@ -150,7 +149,6 @@ function getLiveEntriesByWorktree(state: WorktreeAgentRowsState): Map() for (const [paneKey, entry] of Object.entries(agentStatusByPaneKey)) { diff --git a/src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.performance.test.tsx b/src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.performance.test.tsx new file mode 100644 index 00000000000..30c2a8cf139 --- /dev/null +++ b/src/renderer/src/components/sidebar/worktree-list/listing/use-visible-worktrees.performance.test.tsx @@ -0,0 +1,97 @@ +// @vitest-environment happy-dom +import { useEffect } from 'react' +import { act, cleanup, render } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { useAppStore } from '@/store' +import { buildAgentStatusScaleFixture } from '@/store/slices/agent-status/agent-status-scale.test-fixture' +import { forgetWorktreeSleepIntent, markWorktreeSleepIntent } from '@/lib/worktree-sleep-intent' +import { LOCAL_EXECUTION_HOST_ID } from '../../../../../../shared/execution-host' +import { useVisibleSidebarWorktrees } from './use-visible-worktrees' + +const initialState = useAppStore.getInitialState() + +afterEach(() => { + cleanup() + forgetWorktreeSleepIntent('wt-18') + useAppStore.setState(initialState, true) + vi.useRealTimers() +}) + +// Heartbeats change no visible membership; ending the deliberately slept row's last agent removes one row. +describe('visible sidebar worktree subscription contract', () => { + it.each([40, 400])('retains the visible list through heartbeats at %i worktrees', (count) => { + vi.useFakeTimers() + vi.setSystemTime(2_000_000_000) + const fixture = buildAgentStatusScaleFixture(undefined, count) + useAppStore.setState(initialState, true) + useAppStore.setState({ + ...fixture.state, + agentStatusByPaneKey: fixture.agentStatusByPaneKey, + refreshGitHubForWorktreeIfStale: vi.fn().mockResolvedValue(undefined) + }) + const args: Parameters[0] = { + filterState: { + showSleepingWorkspaces: false, + filterRepoIds: [], + hideDefaultBranchWorkspace: false, + hideAutomationGeneratedWorkspaces: false, + hideCliCreatedWorkspaces: false, + hideDetachedHeadWorkspaces: false, + hideWorkspacesFromOtherDevices: false, + alwaysShowDefaultBranchWorkspace: false, + visibleWorkspaceHostIds: null, + workspaceHostScope: 'all' + }, + sortBy: 'recent', + sortedIds: Object.keys(fixture.state.tabsByWorktree), + repoMap: new Map(fixture.state.repos.map((repo) => [repo.id, repo])), + worktreeLineageById: {}, + defaultHostId: LOCAL_EXECUTION_HOST_ID, + agentSendTargetWorktreeId: null + } + let commits = 0 + let visible: ReturnType['visibleWorktrees'] = [] + function Probe() { + const result = useVisibleSidebarWorktrees(args) + useEffect(() => { + commits += 1 + visible = result.visibleWorktrees + }) + return null + } + render() + expect(visible).toHaveLength(count) + const baseline = visible + commits = 0 + const index = fixture.owners.findIndex((owner) => owner.worktreeId === 'wt-18') + for (let tick = 1; tick <= 20; tick += 1) { + act(() => + useAppStore + .getState() + .setAgentStatus( + fixture.paneKeys[index], + { state: 'working', agentType: 'claude', prompt: 'initial prompt' }, + undefined, + { updatedAt: 2_000_000_000 + tick }, + fixture.owners[index] + ) + ) + expect(commits).toBe(0) + expect(visible).toBe(baseline) + } + markWorktreeSleepIntent('wt-18') + act(() => + useAppStore + .getState() + .setAgentStatus( + fixture.paneKeys[index], + { state: 'done', agentType: 'claude', prompt: 'initial prompt' }, + undefined, + { updatedAt: 2_000_000_021 }, + fixture.owners[index] + ) + ) + expect(commits).toBe(1) + expect(visible.some((worktree) => worktree.id === 'wt-18')).toBe(false) + }) +}) diff --git a/src/renderer/src/components/tab-bar/tab-agent-types-by-tab-id.performance.test.ts b/src/renderer/src/components/tab-bar/tab-agent-types-by-tab-id.performance.test.ts new file mode 100644 index 00000000000..66aca792cf7 --- /dev/null +++ b/src/renderer/src/components/tab-bar/tab-agent-types-by-tab-id.performance.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, it } from 'vitest' +import type { AgentStatusEntry } from '../../../../shared/agent-status/contracts/agent-status-types' +import type { TerminalLayoutSnapshot } from '../../../../shared/workspace-layout/terminal-tab-types' +import { + makeAgentEntry, + makeManyTabState, + LEAF_ID +} from '../worktree-jump/worktree-jump-palette-test-fixtures' +import { createTabBarAgentProjectionSelector } from './tab-agent-types-by-tab-id' + +// Same-agent pings change neither strip projection; a different agent must update the owning tab. +describe('tab bar agent projection identity contract', () => { + it.each([20, 200])('retains projection identities through pings at %i tabs', (count) => { + const tabs = makeManyTabState(count).tabsByWorktree!['wt-many'] + let statuses: Record = Object.fromEntries( + tabs.map((tab) => { + const entry = makeAgentEntry(tab.id, 'working', 2_000_000_000, { agentType: 'claude' }) + return [entry.paneKey, entry] + }) + ) + const layouts: Record = Object.fromEntries( + tabs.map((tab) => [ + tab.id, + { + root: { + type: 'split', + direction: 'horizontal', + first: { type: 'leaf', leafId: LEAF_ID }, + second: { type: 'leaf', leafId: '22222222-2222-4222-8222-222222222222' } + }, + activeLeafId: LEAF_ID, + expandedLeafId: null + } satisfies TerminalLayoutSnapshot + ]) + ) + const select = createTabBarAgentProjectionSelector() + const baseline = select({ agentStatusByPaneKey: statuses, terminalLayoutsByTabId: layouts }) + for (let ping = 1; ping <= 50; ping += 1) { + statuses = Object.fromEntries( + Object.entries(statuses).map(([key, entry]) => [ + key, + { ...entry, updatedAt: 2_000_000_000 + ping } + ]) + ) + const next = select({ agentStatusByPaneKey: statuses, terminalLayoutsByTabId: layouts }) + expect(next.tabAgentTypesByTabId).toBe(baseline.tabAgentTypesByTabId) + expect(next.nativeChatTabWideFallbackUnsafeTabsById).toBe( + baseline.nativeChatTabWideFallbackUnsafeTabsById + ) + } + const changedKey = `term-5:${LEAF_ID}` + statuses = { ...statuses, [changedKey]: { ...statuses[changedKey], agentType: 'codex' } } + const changed = select({ agentStatusByPaneKey: statuses, terminalLayoutsByTabId: layouts }) + expect(changed.tabAgentTypesByTabId).not.toBe(baseline.tabAgentTypesByTabId) + expect(changed.tabAgentTypesByTabId).toEqual({ + ...baseline.tabAgentTypesByTabId, + 'term-5': 'codex' + }) + expect(changed.nativeChatTabWideFallbackUnsafeTabsById).toBe( + baseline.nativeChatTabWideFallbackUnsafeTabsById + ) + }) +}) diff --git a/src/renderer/src/components/tab-bar/use-terminal-tab-activity.performance.test.tsx b/src/renderer/src/components/tab-bar/use-terminal-tab-activity.performance.test.tsx new file mode 100644 index 00000000000..3f4e5ed56f5 --- /dev/null +++ b/src/renderer/src/components/tab-bar/use-terminal-tab-activity.performance.test.tsx @@ -0,0 +1,77 @@ +// @vitest-environment happy-dom +import { useEffect } from 'react' +import { act, cleanup, render } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { useAppStore } from '@/store' +import { + makeAgentEntry, + makeManyTabState +} from '../worktree-jump/worktree-jump-palette-test-fixtures' +import { useTerminalTabActivity } from './use-terminal-tab-activity' + +const initialState = useAppStore.getInitialState() + +afterEach(() => { + cleanup() + useAppStore.setState(initialState, true) + vi.useRealTimers() +}) + +// Only the completed tab's glyph changes; other workspaces' titles and sidebar sizing change none. +describe('terminal tab activity subscription contract', () => { + it.each([20, 200])('isolates one completion among %i tabs', (count) => { + vi.useFakeTimers() + vi.setSystemTime(2_000_000_000) + useAppStore.setState(initialState, true) + useAppStore.setState(makeManyTabState(count)) + const tabs = useAppStore.getState().tabsByWorktree['wt-many'] + const entries = tabs.map((tab) => + makeAgentEntry(tab.id, 'working', 2_000_000_000, { agentType: 'claude' }) + ) + useAppStore.setState({ + refreshGitHubForWorktreeIfStale: vi.fn().mockResolvedValue(undefined), + agentStatusByPaneKey: Object.fromEntries(entries.map((entry) => [entry.paneKey, entry])) + }) + let commits = 0 + const statuses = new Map() + function Probe({ tab }: { tab: (typeof tabs)[number] }) { + const activity = useTerminalTabActivity(tab) + useEffect(() => { + commits += 1 + statuses.set(tab.id, activity.activityStatus) + }) + return null + } + render( + <> + {tabs.map((tab) => ( + + ))} + + ) + commits = 0 + act(() => + useAppStore + .getState() + .setAgentStatus( + entries[5].paneKey, + { state: 'done', agentType: 'claude', prompt: '' }, + undefined, + { updatedAt: 2_000_000_001 }, + { tabId: tabs[5].id, worktreeId: 'wt-many' } + ) + ) + expect(commits).toBe(1) + expect(statuses.get('term-5')).toBe('done') + commits = 0 + for (let tick = 0; tick < 20; tick += 1) { + act(() => + useAppStore.setState({ + runtimePaneTitlesByTabId: { 'other-worktree-tab': { 0: `title ${tick}` } }, + sidebarWidth: 300 + tick + }) + ) + expect(commits).toBe(0) + } + }) +}) diff --git a/src/renderer/src/runtime/sync-runtime-graph/agent-status-projection.ts b/src/renderer/src/runtime/sync-runtime-graph/agent-status-projection.ts index 727ccf8df3e..c2b40336453 100644 --- a/src/renderer/src/runtime/sync-runtime-graph/agent-status-projection.ts +++ b/src/renderer/src/runtime/sync-runtime-graph/agent-status-projection.ts @@ -103,16 +103,3 @@ export function buildRuntimeMobileAgentStatusProjection( } return projection } - -export function buildRuntimeMobileAgentStatusProjectionForTests( - agentStatusByPaneKey: AppState['agentStatusByPaneKey'] -): string { - return buildRuntimeMobileAgentStatusProjection(agentStatusByPaneKey) -} - -export const AGENT_STATUS_SYNC_UPDATED_AT_BUCKET_MS_FOR_TESTS = - AGENT_STATUS_SYNC_UPDATED_AT_BUCKET_MS - -export function resetRuntimeMobileAgentStatusProjectionCacheForTests(): void { - cachedAgentStatusProjection = null -} diff --git a/src/renderer/src/store/slices/agent-status/agent-status-batch.performance.test.ts b/src/renderer/src/store/slices/agent-status/agent-status-batch.performance.test.ts new file mode 100644 index 00000000000..64f0e56110f --- /dev/null +++ b/src/renderer/src/store/slices/agent-status/agent-status-batch.performance.test.ts @@ -0,0 +1,34 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { AgentStatusBatchUpdate } from './agent-status-contract' +import { buildAgentStatusScaleFixture } from './agent-status-scale.test-fixture' + +// One burst has one externally visible snapshot, irrespective of how many panes it changes. +describe('agent status burst publication contract', () => { + afterEach(() => vi.useRealTimers()) + + it.each([8, 80])('publishes %i updates once and retains untouched entries', (count) => { + vi.useFakeTimers() + vi.setSystemTime(2_000_000_000) + const { store, paneKeys, owners, agentStatusByPaneKey } = buildAgentStatusScaleFixture() + store.setState({ agentStatusByPaneKey }) + const before = store.getState().agentStatusByPaneKey + const published = vi.fn() + const stop = store.subscribe(published) + const updates: AgentStatusBatchUpdate[] = paneKeys.slice(0, count).map((paneKey, index) => ({ + paneKey, + payload: { state: 'working', agentType: 'claude', prompt: 'new prompt' }, + timing: { updatedAt: 2_000_000_001 }, + routing: owners[index] + })) + store.getState().setAgentStatuses(updates) + stop() + expect(published).toHaveBeenCalledTimes(1) + const after = store.getState().agentStatusByPaneKey + for (const paneKey of paneKeys.slice(0, count)) { + expect(after[paneKey].prompt).toBe('new prompt') + } + for (const paneKey of paneKeys.slice(count)) { + expect(after[paneKey]).toBe(before[paneKey]) + } + }) +}) diff --git a/src/renderer/src/store/slices/agent-status/agent-status-scale.test-fixture.ts b/src/renderer/src/store/slices/agent-status/agent-status-scale.test-fixture.ts new file mode 100644 index 00000000000..76cd7f36a36 --- /dev/null +++ b/src/renderer/src/store/slices/agent-status/agent-status-scale.test-fixture.ts @@ -0,0 +1,91 @@ +import { tmpdir } from 'node:os' +import type { AppState } from '@/store/types' +import type { AgentStatusEntry } from '../../../../../shared/agent-status/contracts/agent-status-types' +import { createTestStore } from '@/store/testing/app-store-test-fixture' +import { makeTab } from '@/store/terminals/terminal-tab.test-fixture' +import { makeUnifiedTab } from '@/store/slices/tabs/tab-group.test-fixture' +import { makeWorktree } from '@/store/slices/worktrees/worktree.test-fixture' +import { TEST_REPO } from '@/store/repos/repo.test-fixture' +import { makePaneKey } from '../../../../../shared/workspace-layout/stable-pane-id' +import { getDefaultSettings } from '../../../../../shared/constants' + +const LEAF_ID = '11111111-1111-4111-8111-111111111111' + +export function buildAgentStatusScaleFixture( + onTabVisit: (() => void) | undefined = undefined, + worktreeCount = 423 +) { + /** Tab list whose iteration is observable, so the nested-loop resolver's comparisons are countable. */ + class CountingTabList extends Array { + [Symbol.iterator](): ArrayIterator { + const iterator = super[Symbol.iterator]() + const next = iterator.next.bind(iterator) + iterator.next = (...args) => { + const result = next(...args) + if (!result.done) { + onTabVisit?.() + } + return result + } + return iterator + } + } + + const store = createTestStore() + const settings = store.getState().settings ?? getDefaultSettings(tmpdir()) + const tabsByWorktree: AppState['tabsByWorktree'] = {} + const unifiedTabsByWorktree: AppState['unifiedTabsByWorktree'] = {} + const worktrees: ReturnType[] = [] + const paneKeys: string[] = [] + const owners: { tabId: string; worktreeId: string }[] = [] + for (let index = 0; index < worktreeCount; index += 1) { + const worktreeId = `wt-${index}` + worktrees.push(makeWorktree({ id: worktreeId, repoId: TEST_REPO.id })) + const tabs: ReturnType[] = [] + const unified: ReturnType[] = [] + for (let tab = 0; tab < (index % 2 === 0 ? 1 : 2); tab += 1) { + const tabId = `tab-${index}-${tab}` + tabs.push(makeTab({ id: tabId, worktreeId, title: `Terminal ${index}-${tab}` })) + unified.push( + makeUnifiedTab({ + id: tabId, + worktreeId, + groupId: `group-${index}`, + label: `Label ${index}` + }) + ) + paneKeys.push(makePaneKey(tabId, LEAF_ID)) + owners.push({ tabId, worktreeId }) + } + tabsByWorktree[worktreeId] = onTabVisit ? CountingTabList.from(tabs) : tabs + unifiedTabsByWorktree[worktreeId] = unified + } + const state = { + repos: [TEST_REPO], + worktreesByRepo: { [TEST_REPO.id]: worktrees }, + tabsByWorktree, + unifiedTabsByWorktree, + terminalLayoutsByTabId: {}, + settings: { ...settings, tabAutoGenerateTitle: false } + } satisfies Partial + store.setState(state) + const agentStatusByPaneKey = Object.fromEntries( + paneKeys + .map((paneKey, index): [string, AgentStatusEntry] => [ + paneKey, + { + paneKey, + ...owners[index], + state: 'working', + agentType: 'claude', + prompt: 'initial prompt', + updatedAt: 2_000_000_000, + stateStartedAt: 2_000_000_000, + firstStateStartedAt: 2_000_000_000, + stateHistory: [] + } satisfies AgentStatusEntry + ]) + .filter((_, index) => owners[index].tabId.endsWith('-0')) + ) + return { store, state, paneKeys, owners, agentStatusByPaneKey } +}