Files
orca/src/main/plugins/plugin-discovery.ts
T
NeilandOrca 97e4776dfe feat(plugins): Orca plugin system — kernel, content packs, panels, workers, marketplace v0 (experimental) (#8549)
* feat(plugins): Orca plugin system — kernel, content packs, panels, workers, marketplace v0 (experimental)

Adds Orca's experimental plugin system behind a settings flag: a
supervised kernel, declarative content packs (VM recipes, commands and
keybindings, language packs), sandboxed iframe panels, forked worker
hosts, and a Git-backed marketplace v0 with consent, provenance and
kill-list enforcement.

Theme, icon-theme and terminal-theme contributions are deferred to a
follow-up pass.

* fix(plugins): make unsupported marketplace listings unreachable by key

findPlugin() backs preview/install/previewInstalledUpdate via
requireListing(), so filtering only listPlugins() hid the catalog card
while leaving the dead install path reachable one click later.

* fix(plugins): fan Pi session-only status out to plugin subscribers

The providerSessionOnly early-return in applyNormalizedStatus emitted to
onAgentStatus (main-window fanout) but skipped enrichedStatusListeners, so
plugins subscribed to agent.status.changed silently missed every Pi
session_start event. Route both emit sites through one helper so a future
early return cannot drop the plugin tap again.

Co-authored-by: Orca <help@stably.ai>

* plugins: drop dead code and hoist duplicated trust-boundary patterns

Cleanup pass over the P1 diff, no behavior change:

- Delete `readPluginTreeSnapshot`/`readSnapshotFile` and their types, plus
  the now-vestigial `directories`/`signal` plumbing in `collectFiles`.
- Delete `resolveContainedPluginDirectory` (no callers).
- Delete `plugin-content-load-pool.ts`; it reimplemented the existing
  `mapWithConcurrency`, whose index arg also removes the pairing wrapper
  in `buildPluginList`.
- Hoist `PLUGIN_CONTENT_HASH_PATTERN` and `PLUGIN_COMMIT_PATTERN` into
  the install-lockfile module; 11 sites hand-rolled these identically.
- Point the new reliability gate at the PR instead of gitignored docs
  paths, matching every other gate's link form.

* fix(plugins): retry plugin state renames on Windows AV/EPERM locks

Six plugin write paths (lockfile, provenance, current pointer, kill
list, marketplace cache, staged install dir) did a plain rename, so an
antivirus or indexer holding the target open surfaced as a failed
install. The repo already retries this hazard for issue #1507, but only
through a sync helper; these paths are all async.

Adds one bounded async retry + atomic write used by all six, and trims a
consent-provenance header that restated its own JSX.

* test(plugins): cover the Windows rename retry path

The retry loop shipped untested: both existing cases hit the non-retry path,
and the temp-cleanup test passed identically with the `finally` removed.
Mock `rename` to queue errno codes so CI can exercise locks it cannot provoke.

Co-authored-by: Orca <help@stably.ai>

* fix(plugins): pin bundled plugin resources to LF

Windows CI checks out with autocrlf, so the byte-hashed launch tree arrived
as CRLF and verify-packaged-plugin-resources rejected it — the packaged build
could never pass on Windows. Reproduced locally: CRLF yields the exact CI
error, LF verifies clean. Files are already LF, so nothing renormalizes.

Co-authored-by: Orca <help@stably.ai>

* test: guard the bundled-plugin LF pin against a CRLF checkout

The byte-hash mismatch only surfaced in Windows packaging CI. Assert the
.gitattributes pin and that a CRLF tree is rejected, so a regression fails
on any platform instead of waiting for a packaged Windows build.

Co-authored-by: Orca <help@stably.ai>

* ci: trigger packaged-build check on bundled plugin resource changes

The launch tree is byte-hashed during packaging, but no trigger path covered
it — so the CRLF fix for that check would not have re-run the check. Add the
resources, verifier and .gitattributes paths that can break packaging.

Co-authored-by: Orca <help@stably.ai>

* perf(plugins): rebuild the panel frame only when its baked theme values change

The revision keys the panel iframe, so every bump destroys the sandboxed
frame and its in-panel state. It counted root attribute mutations, but
--workspace-sidebar-live-width is written every rAF of a sidebar drag, so
dragging with a panel open blanked it ~60x/sec. Compare the two values the
shell actually bakes in instead.

Co-authored-by: Orca <help@stably.ai>

* test: stop pinning a plugin name in the CRLF guard

The CRLF case rewrites every launch file, so the reported mismatch is
whichever plugin sorts first. P2 adds theme plugins that sort ahead of
orca-navigation-shortcuts, which broke the assertion there.

Co-authored-by: Orca <help@stably.ai>

* style: drop stray blank lines left by the rebase resolutions

Both sides of the agent-hooks and orca-runtime conflicts contributed a
trailing blank, which oxfmt rejects. Whitespace only.

Co-authored-by: Orca <help@stably.ai>

* test(plugins): stop the startup budget failing on machine load

P95 runs 16-34ms idle but exceeds the 50ms bound under full-suite
parallelism, so the gate flaked. Widen it to catch an order-of-magnitude
regression instead; the no-worker/no-plugin-code assertions are the real
guarantee. Verified a 400ms regression still fails.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-27 01:14:33 -07:00

255 lines
7.9 KiB
TypeScript

import { readdir } from 'node:fs/promises'
import type { Dirent } from 'node:fs'
import { join } from 'node:path'
import { PLUGIN_CONTENT_HASH_PATTERN } from '../../shared/plugins/plugin-install-lockfile'
import {
PLUGIN_MANIFEST_FILENAME,
isQualifiedPluginKey,
parsePluginManifest,
qualifiedPluginKey,
satisfiesOrcaEngineRange,
type PluginManifest
} from '../../shared/plugins/plugin-manifest'
import {
fingerprintPluginConsent,
hasInstructionalPluginContributions
} from '../../shared/plugins/plugin-consent-fingerprint'
import { validateDeclaredPluginArtifacts } from './plugin-artifact-validation'
import { readPluginManifestText } from './plugin-manifest-file'
import { readPluginCurrentPointer } from './plugin-current-pointer'
import { hashPluginTree } from './plugin-content-hash'
export { PLUGIN_CURRENT_POINTER_FILENAME } from './plugin-current-pointer'
const INSTALLED_PLUGIN_DISCOVERY_CONCURRENCY = 8
/**
* Discovery over the hash-addressed install layout:
*
* <userData>/plugins/<publisher>.<id>/current ← text file naming the hash
* <userData>/plugins/<publisher>.<id>/<hash>/ ← immutable install tree
*
* plus dev-mode plugins loaded straight from arbitrary local directories.
* Discovery reads manifests and checks only their declared artifact paths —
* never plugin bytes or whole trees. Full content hashing stays lazy so
* startup cost is bounded by installed plugins plus declared entries.
*/
export type ValidDiscoveredPlugin = {
/** Qualified `<publisher>.<id>` key. */
pluginKey: string
rootDir: string
manifest: PluginManifest
/** Fingerprint of the capabilities and trusted-worker execution tier. */
consentFingerprint: string
/** Immutable tree identity included in consent for instructional packs. */
consentContentHash?: string | null
/** Content hash the install dir is named by; null for dev plugins. */
contentHash: string | null
isDev: boolean
}
export type InvalidDiscoveredPlugin = {
pluginKey?: string
rootDir: string
error: string
isDev: boolean
}
export type DiscoveredPlugin = ValidDiscoveredPlugin | InvalidDiscoveredPlugin
export function isInvalidDiscoveredPlugin(
plugin: DiscoveredPlugin
): plugin is InvalidDiscoveredPlugin {
return 'error' in plugin
}
export function getUserPluginsDir(userDataPath: string): string {
return join(userDataPath, 'plugins')
}
export function getPluginsDataDir(userDataPath: string): string {
return join(userDataPath, 'plugins-data')
}
async function readManifestDir(
rootDir: string,
hostVersion: string,
isDev: boolean,
installedContentHash?: string
): Promise<DiscoveredPlugin> {
let rawText: string
try {
rawText = await readPluginManifestText(rootDir)
} catch (error) {
return {
rootDir,
error:
error instanceof Error && error.message.includes('exceeds')
? error.message
: `missing ${PLUGIN_MANIFEST_FILENAME}`,
isDev
}
}
let raw: unknown
try {
raw = JSON.parse(rawText)
} catch (error) {
return {
rootDir,
error: `invalid JSON in ${PLUGIN_MANIFEST_FILENAME}: ${error instanceof Error ? error.message : String(error)}`,
isDev
}
}
const parsed = parsePluginManifest(raw)
if (!parsed.ok) {
return { rootDir, error: `invalid manifest: ${parsed.error}`, isDev }
}
const manifest = parsed.manifest
const pluginKey = qualifiedPluginKey(manifest)
if (!satisfiesOrcaEngineRange(hostVersion, manifest.engines.orca)) {
return {
pluginKey,
rootDir,
error: `requires Orca ${manifest.engines.orca} (this is ${hostVersion})`,
isDev
}
}
const artifacts = await validateDeclaredPluginArtifacts(rootDir, manifest)
if (!artifacts.ok) {
return {
pluginKey,
rootDir,
error: `invalid declared artifact: ${artifacts.error}`,
isDev
}
}
let consentContentIdentity: string | undefined
if (installedContentHash && hasInstructionalPluginContributions(manifest)) {
consentContentIdentity = installedContentHash
}
if (isDev && hasInstructionalPluginContributions(manifest)) {
const treeHash = await hashPluginTree(rootDir)
if (!treeHash.ok) {
return { pluginKey, rootDir, error: treeHash.error, isDev }
}
consentContentIdentity = treeHash.hash
}
return {
pluginKey,
rootDir,
manifest,
consentFingerprint: fingerprintPluginConsent(manifest, consentContentIdentity),
consentContentHash: consentContentIdentity ?? null,
contentHash: null,
isDev
}
}
async function readInstalledPlugin(
pluginDir: string,
dirName: string,
hostVersion: string
): Promise<DiscoveredPlugin> {
let contentHash: string
try {
contentHash = (await readPluginCurrentPointer(pluginDir)) ?? ''
} catch {
return {
pluginKey: dirName,
rootDir: pluginDir,
error: 'missing current-version pointer',
isDev: false
}
}
// The pointer names a sibling directory; refuse anything path-like so a
// corrupted pointer cannot address content outside the plugin dir.
if (!PLUGIN_CONTENT_HASH_PATTERN.test(contentHash)) {
return {
pluginKey: dirName,
rootDir: pluginDir,
error: 'corrupt current-version pointer',
isDev: false
}
}
const versionDir = join(pluginDir, contentHash)
const discovered = await readManifestDir(versionDir, hostVersion, false, contentHash)
if (isInvalidDiscoveredPlugin(discovered)) {
return { ...discovered, pluginKey: dirName }
}
// Why: the directory name is the install key (and the uninstall target); a
// mismatched manifest identity would let two dirs claim the same plugin.
if (discovered.pluginKey !== dirName) {
return {
pluginKey: dirName,
rootDir: versionDir,
error: `manifest identity "${discovered.pluginKey}" does not match install directory "${dirName}"`,
isDev: false
}
}
return { ...discovered, contentHash }
}
async function readInstalledPlugins(
pluginsDir: string,
entries: readonly Dirent[],
hostVersion: string
): Promise<DiscoveredPlugin[]> {
const results = Array.from({ length: entries.length }) as DiscoveredPlugin[]
let nextIndex = 0
const readers = Array.from(
{ length: Math.min(INSTALLED_PLUGIN_DISCOVERY_CONCURRENCY, entries.length) },
async () => {
while (nextIndex < entries.length) {
const index = nextIndex++
const entry = entries[index]!
results[index] = await readInstalledPlugin(
join(pluginsDir, entry.name),
entry.name,
hostVersion
)
}
}
)
await Promise.all(readers)
return results
}
export async function discoverPlugins(options: {
pluginsDir: string
devPluginPaths: readonly string[]
hostVersion: string
}): Promise<DiscoveredPlugin[]> {
const discovered: DiscoveredPlugin[] = []
let entries: Dirent[] = []
try {
entries = await readdir(options.pluginsDir, { withFileTypes: true })
} catch {
// A missing plugins dir just means no plugins are installed yet.
}
const installedEntries = entries.filter(
(entry) => entry.isDirectory() && isQualifiedPluginKey(entry.name)
)
// Installed manifests are independent immutable trees. Read them in
// a bounded pool so startup latency stays low without exhausting handles.
discovered.push(
...(await readInstalledPlugins(options.pluginsDir, installedEntries, options.hostVersion))
)
for (const devPath of options.devPluginPaths) {
const plugin = await readManifestDir(devPath, options.hostVersion, true)
// A dev path that duplicates an installed plugin's identity wins — that
// is the point of dev mode — but two dev paths must not collide.
if (!isInvalidDiscoveredPlugin(plugin)) {
const collision = discovered.find(
(existing) =>
!isInvalidDiscoveredPlugin(existing) && existing.pluginKey === plugin.pluginKey
)
if (collision) {
discovered.splice(discovered.indexOf(collision), 1)
}
}
discovered.push(plugin)
}
return discovered
}