# Static bundles for embedded extensions

[日本語版](ja/extension-bundles.md)

When a newly opened SB3 contains several unsandboxed extensions, TurboWarp asks for permission to
run each extension. A static bundle keeps the individual extensions in the expanded source but
transforms only the generated SB3 into one composite extension, reducing the permission unit to
one extension.

## Overview

The default `classic` mode wraps completed scripts. To share their dependencies too, opt in to the
module build described below. The remaining classic runtime and recovery documentation also applies
to module bundles unless noted.

## Module builds (off by default)

Every member must provide an installed npm JavaScript entry exporting `createExtension(scratch)` and
`bundleModuleMetadata` (`formatVersion: 1`, `id`, `unsandboxed`). Importing it must not activate an
extension, register one, or connect listeners. The factory synchronously returns a fresh instance
using the supplied member Scratch proxy. The composite calls each factory once and registers once.

Add a `module` declaration to each member's existing npm `source`. Replace the digest placeholders
with the SHA-256 of the respective files:

```json
{
  "provider": "npm",
  "package": "@example/extension-one",
  "version": "1.0.0",
  "artifact": "dist/extension-one.js",
  "integrity": "sha256-<base64 classic digest>",
  "module": {
    "artifact": "lib-module/src/bundle-module.js",
    "formatVersion": 1,
    "integrity": "sha256-<base64 module digest>",
    "unsandboxed": "required"
  }
}
```

Select the build and exact shared versions on the bundle:

```json
{
  "id": "projectbundle",
  "name": "Project Extension Bundle",
  "members": ["extensionone", "extensiontwo"],
  "buildMode": "module",
  "sharedDependencies": {"yaml": "2.9.1"},
  "blockEditorRegistrations": true
}
```

Install the pinned owner packages and shared dependencies in the project's `node_modules` first,
retaining either `pnpm-lock.yaml` or `package-lock.json`. Parent directories of the expanded source
are searched. The toolchain's own yaml is not a shared input. Builds do not install packages, clone
repositories, execute owner code, or load project build configurations or plugins. The initial module
scope is installed npm artifacts; GitHub sources continue to use classic mode.

```bash
sb3-toolchain extensions bundle app --id projectbundle --name "Project Extension Bundle" \
  extensionone extensiontwo --build-mode module --share yaml@2.9.1
# Review the plan, then repeat with --yes to save it.
sb3-toolchain build app --output dist/project.sb3
```

`bundleExtensions` and `planExtensionBundle` also accept `buildMode` and `sharedDependencies`.
Repeat `--share` for additional packages. GUI editor adaptation remains an explicit manifest option.

The bundle's optional `metadata` describes the complete bundle, rather than its first member:

```json
{
  "description": "The complete application extension.",
  "descriptions": {"ja": "アプリ全体の機能拡張。"},
  "author": "Application Author",
  "authorURL": "https://example.com/author",
  "version": "1.2.3",
  "repository": "https://github.com/example/application",
  "docsURL": "https://example.com/docs/",
  "banner": "https://example.com/banner.png",
  "package": "@example/application"
}
```

Put this object in `extensionBundles[].metadata`, pass `metadata` to `bundleExtensions` or
`planExtensionBundle`, or use `--metadata-json '{"version":"1.2.3"}'` with the bundle command.
The exported `ExtensionBundleMetadata` type also accepts `updatedAt`. Fields are single lines of at
most 1000 characters. Locale keys are lowercase language tags. Repository and documentation URLs
use HTTPS; author URLs allow HTTP or HTTPS and require an explicit author in the same object.
Banners allow HTTPS or image data URLs for PNG, JPEG, GIF, WebP, or SVG; the data URL prefix is
normalized to lowercase. Invalid values, credentials, and unknown fields are rejected.

These fields become comment headers in both classic and module bundles, without executing members
to obtain display information. Name and ID remain bundle settings; licenses and sandbox requirements
remain derived from members. No package or author URL is inferred from a member. Omitting `metadata`
retains the existing generated bytes; removing it restores that output for future builds. Previously
frozen release artifacts must retain their original bytes.

The resolver checks the selected version against each importer's declared range and uses one real
package under browser conditions. yaml must use its browser entry; duplicate copies or mixed Node
and browser graphs fail. Missing entries, mismatched package versions, classic artifacts, module
digests, and unsupported contracts also fail. A required sandbox guard runs before any library
evaluation or factory activation; without permission only the existing notice extension registers.
All member metadata is checked before any factory activates. If a later factory fails, prior
instances are disposed. The composite's `dispose()` cleans up each member once.

The `moduleBuild` report on bundle plans and `createDeterministicSb3(...).bundlePlans[]` records the
bundler version, browser / ES2020 settings, lock digest, member entries, loaded files and package
manifest digests, and shared package copy counts. `Module build inputs` in the generated JS hashes
this record; absolute paths are omitted. The lock is hashed as an input alongside the actual installed
files, so install from the lock when reproducing a build. When updating an owner, update both classic
and module integrity pins. A stale module pin rejects the candidate while retaining the source.

Library code and module evaluation are shared per bundle load. Parser, Composer, Document, and other
parse state remain independent for each input. Other bundles and separate GUI graphs are outside
this scope. Existing owner feature flags, IDs, opcodes, and parse options stay intact.

### Verification and rollback

Regular tests deliberately install private copies of the same yaml version under multiple members.
To verify the actual unpublished owner entries, build their clean checkouts first, then run:

```bash
node scripts/prepare-module-owner-fixture.mjs /tmp/new-module-owners \
  /path/to/turbowarp-structured-data /path/to/turbowarp-yaml-json /path/to/turbowarp-3d-scene-runtime
SB3_MODULE_OWNER_FIXTURE=/tmp/new-module-owners/app pnpm exec vitest run test/extension-module-owners.test.ts
```

The preparation script packs local artifacts and installs production dependencies with lifecycle
scripts disabled. The owner test runs only with the environment variable; ordinary CI skips it.
It writes `module-owner-results.json` (including owner commits and input digests), `shared-yaml.js`,
and `shared-yaml.sb3` beside `app`. Evaluation and factory counters are injected only into the test
build; size and reproducibility measurements use uninstrumented output. Tests also cover parsing,
runtime Schema validation, editor registration and cleanup, sandbox handling, and recovery.

To roll back, omit `buildMode` or set it to `classic`, remove `sharedDependencies`, and rebuild from
the retained classic scripts. That path needs neither installed module dependencies nor a lock.
Source-based unbundling is unchanged. Direct unbundling from an SB3 still requires the separate
`recoveryCapsule: true` option. The capsule contains original classic scripts as data, increasing
artifact size even though the executing shared library has one copy. Measure sizes with it disabled.
Actual browser permission dialogs and GUI interaction still need the manual checks below.

## Classic overview

```mermaid
flowchart LR
  subgraph Source["Expanded source (authoritative)"]
    Project["project.source.json<br/>original opcodes"]
    Alpha["extensions/alpha.js"]
    Beta["extensions/beta.js"]
  end

  Build["sb3-toolchain build"]

  subgraph Output["Generated bundled SB3"]
    BundledProject["project.json<br/>namespaced opcodes"]
    Composite["one composite extension<br/>one register() call"]
    Capsule["optional recovery capsule<br/>original extension data"]
  end

  TurboWarp["TurboWarp Editor<br/>one permission prompt"]

  Project --> Build
  Alpha --> Build
  Beta --> Build
  Build --> BundledProject
  Build --> Composite
  Build --> Capsule
  BundledProject --> TurboWarp
  Composite --> TurboWarp
```

The expanded source always retains the individual extensions. Bundling changes only the generated
SB3. Recovery data is opt-in when direct restoration from that generated artifact is required.

## What problem it solves

TurboWarp grants unsandboxed execution permission per loaded custom extension. A project with three
embedded extensions therefore presents three prompts even when all three are maintained and shipped
as one project. The bundle changes the loading boundary, not the security decision itself.

```mermaid
flowchart LR
  subgraph Before["Without a bundle"]
    BeforeSB3["SB3"] --> PromptA["Allow extension A?"]
    PromptA --> PromptB["Allow extension B?"]
    PromptB --> PromptC["Allow extension C?"]
    PromptC --> RunBefore["Run project"]
  end

  subgraph After["With a static bundle"]
    AfterSB3["SB3"] --> BundlePrompt["Allow project bundle?"]
    BundlePrompt --> Members["Extension A + B + C"]
    Members --> RunAfter["Run project"]
  end
```

The user still reviews and authorizes unsandboxed JavaScript. The difference is that the generated
SB3 exposes one composite extension ID and one embedded extension URL, so TurboWarp has one custom
extension to authorize and load.

### What it is and what it is not

| This feature does                                                 | This feature does not                                        |
| ----------------------------------------------------------------- | ------------------------------------------------------------ |
| Create one composite runtime extension from several members       | Disable or bypass TurboWarp's permission prompt              |
| Rewrite generated project references to collision-free namespaces | Rewrite the authoritative source project in place            |
| Preserve and delegate each member's original implementation       | Flatten all member classes into one manually merged class    |
| Optionally embed recovery data for direct SB3 unbundling          | Guarantee compatibility for arbitrary dynamic extension code |
| Reject transformations that cannot be classified safely           | Execute extension JavaScript during the build                |

## Internal architecture

Bundling has separate build-time and runtime layers. Build time validates and rewrites data without
executing extension JavaScript. Runtime uses a generated wrapper to capture each member registration,
combine its metadata, and delegate calls.

```mermaid
flowchart TB
  subgraph BuildTime["Build time: Node.js toolchain"]
    Config["extensionBundles configuration"]
    Sources["Individual member JavaScript"]
    SourceProject["Original project.source.json"]
    Validator["Static contract validator"]
    Generator["Bundle source generator"]
    Rewriter["Project reference rewriter"]
    BundleJS["Generated composite JavaScript"]
    BundledProject["Generated project.json"]
    Archive["Deterministic SB3 archive"]

    Config --> Validator
    Sources --> Validator
    Validator --> Generator
    SourceProject --> Rewriter
    Config --> Rewriter
    Generator --> BundleJS
    Rewriter --> BundledProject
    BundleJS --> Archive
    BundledProject --> Archive
  end

  subgraph Runtime["Runtime: TurboWarp"]
    Manager["Extension manager"]
    Wrapper["Generated bundle wrapper"]
    Captured["Captured member instances"]
    Composite["StaticExtensionBundle"]
    Palette["One palette category"]
    VM["Scratch VM execution"]

    Manager -->|"load one data URL"| Wrapper
    Wrapper -->|"execute members with Scratch proxy"| Captured
    Captured -->|"compose getInfo() and delegates"| Composite
    Composite -->|"register once"| Manager
    Manager --> Palette
    VM -->|"namespaced opcode"| Composite
    Composite -->|"delegate"| Captured
  end

  Archive --> Manager
```

### Anatomy of the generated JavaScript

The generated data URL is a self-contained classic extension script with these layers:

```text
projectbundle.js
├── human-readable bundle header
│   └── member name, ID, author, description, and license
├── runtime adapter
│   ├── Scratch proxy for each member
│   ├── synchronous register() capture
│   ├── opcode, menu, custom-field, and hat namespaces
│   └── global and target storage aliases
├── original member source A
├── original member source B
├── StaticExtensionBundle
│   ├── combined getInfo()
│   ├── generated palette headings and separators
│   └── handlers that delegate to captured member instances
├── one real Scratch.extensions.register() call
└── optional SB3-Toolchain-Reversible-Bundle-v1 recovery capsule
```

The original scripts still execute their normal initialization code at runtime. Their calls to
`Scratch.extensions.register()` are intercepted by a member-specific Scratch proxy instead of being
sent to TurboWarp. After every member registers exactly once, the wrapper registers the composite
extension with the real API.

### Runtime call flow

```mermaid
sequenceDiagram
  participant VM as TurboWarp VM
  participant W as Bundle wrapper
  participant A as Member alpha
  participant B as Member beta
  participant C as Composite extension

  VM->>W: Load projectbundle data URL
  W->>A: Execute with proxied Scratch
  A-->>W: register(alpha instance) captured
  W->>B: Execute with proxied Scratch
  B-->>W: register(beta instance) captured
  W->>C: Construct combined getInfo() and delegates
  W->>VM: register(composite) once

  VM->>C: Call alpha__doSomething(args, util)
  C->>A: Call original doSomething(args, util)
  A-->>C: Result
  C-->>VM: Result

  A->>W: startHats("alpha_whenReady")
  W->>VM: startHats("projectbundle_alpha__whenReady")

  A->>W: getOpcodeFunction("beta_echo")
  W->>VM: getOpcodeFunction("projectbundle_beta__echo")
  VM-->>A: Bundled opcode handler
  A->>C: Call handler(args, util)
  C->>B: Call original echo(args, util)
  B-->>A: Result or Promise
```

### Data transformations

| Concern                     | Before bundling                           | Generated bundled SB3                         |
| --------------------------- | ----------------------------------------- | --------------------------------------------- |
| Loaded extension IDs        | `alpha`, `beta`                           | `projectbundle`                               |
| Embedded data URLs          | One per member                            | One composite data URL                        |
| Member `getInfo()` opcode   | `doSomething`                             | `alpha__doSomething`                          |
| Stored project opcode       | `alpha_doSomething`                       | `projectbundle_alpha__doSomething`            |
| Menu and custom-field names | Member-local names                        | `memberId__` namespace                        |
| `startHats()` opcode        | `alpha_whenReady`                         | `projectbundle_alpha__whenReady`              |
| `getOpcodeFunction()`       | `beta_echo`                               | `projectbundle_beta__echo`                    |
| Extension storage           | `storage.alpha`, `storage.beta`           | `storage.projectbundle.components.alpha/beta` |
| Block IDs and graph links   | Original IDs and `next`/`parent`/`inputs` | Unchanged                                     |

This split is why existing scripts continue to call their original methods while the saved project
uses the composite extension ID expected by TurboWarp.

## Compatibility and reversibility

Bundle configuration is added only to `embedded-extensions.json`. The following authoritative
sources are not modified:

- Original JavaScript in `extensions/<extensionId>.js`
- Individual extension entries and `source` provenance in `embedded-extensions.json`
- Original extension IDs, block and monitor opcodes, and extension storage in `project.source.json`

`check` and `extensions status|sync|update` continue to validate and update individual extensions.
Only `build` transforms the in-memory `project.json` and embedded JavaScript into their bundled
representation. There is no mode that deletes the original files.

An extension that cannot be transformed safely is rejected instead of being bundled with changed
behavior. The current static-bundle contract requires all of the following:

- Every member has Extension Gallery-style `Name`, `ID`, `Description`, `By`, and `License` headers
- The header ID, manifest ID, and runtime `getInfo().id` are identical
- Every member is a classic script that calls `Scratch.extensions.register` exactly once and synchronously
- No member uses XML blocks
- No unclassified opcode references remain outside blocks, monitors, or dynamic block metadata

The bundle namespaces ordinary commands, reporters, booleans, events, buttons, labels, static and
dynamic menus, custom fields, and global and target extension storage. Calls from child extensions
to `runtime.startHats` are also translated to bundled opcodes. When a member calls
`runtime.getOpcodeFunction()`, an opcode beginning with any member ID in the same bundle is
translated at runtime. This supports both self references and calls to another member, such as an
Asset Manager member resolving an Animated Text member's handler.

Only the opcode passed to `getOpcodeFunction()` is adapted. The function returned by the VM is
passed through unchanged, so its binding, handler arguments, synchronous result, or Promise result
are preserved. Core, external-extension, and unknown opcodes whose prefix is not a bundle member ID
are passed to the VM unchanged. Other APIs that carry opcode strings are outside this compatibility
contract unless documented separately.

The bundle preserves the `members` order from the manifest and the block-definition order within
each member's `getInfo().blocks`. In the TurboWarp Editor palette, each group begins with a decorated
LABEL heading in this form:

```text
◆ <name> [<memberId>] ◆
```

Putting the member name first keeps it visible in TurboWarp's narrow palette. Two consecutive `---`
separators divide member groups. A generated LABEL also carries the following machine-readable
metadata:

```js
{
  sb3Toolchain: {
    kind: 'bundle-member-heading',
    memberId,
  },
}
```

For every ordinary block that does not already define its own `blockIconURI`, the bundle copies the
member's top-level `getInfo().blockIconURI` onto the transformed block. TurboWarp supports
`blockIconURI` at block level, so blocks keep their originating extension's icon even though the
bundle is registered as one extension. A block-specific icon always wins, and members without an
icon keep the previous icon-free output. Generated headings, documentation buttons, member LABELs,
and member BUTTONs are not given an inherited icon.

When a member's `getInfo()` returns a non-empty string [`docsURI`](https://docs.turbowarp.org/development/extensions/assorted-apis#docsuri),
the bundle inserts an `Open Documentation` button immediately after that member's heading. It uses TurboWarp's native
`OPEN_EXTENSION_DOCS` callback, so it preserves the normal `docsURI` behavior instead of delegating
to extension code. The generated button carries separate metadata and safely XML-escapes the URI:

```js
{
  sb3Toolchain: {
    docsURI,
    kind: 'bundle-member-docs',
    memberId,
  },
}
```

No button is inserted for a member without `docsURI`. Member-provided XML blocks remain unsupported;
the tool generates only this narrowly defined documentation button XML.

LABEL entries and separators originally defined by a member are preserved unchanged and in their
original positions. The decorated bundle heading and wider double separator therefore remain
visually distinct from an original heading or ordinary separator. A tool can identify the generated
heading by its `sb3Toolchain` metadata and treat the two separators immediately before it as the
bundle group boundary.

The resulting palette has this visual hierarchy:

```text
┌─ Project Extension Bundle ─────────────────────────────┐
│ ◆ Alpha Tools [alpha] ◆                               │ ← generated heading
│   Open Documentation                                   │ ← generated docsURI button
│   alpha block 1                                        │
│   Alpha original heading                               │ ← original LABEL
│                                                       │ ← original separator
│   alpha block 2                                        │
│                                                       │
│                                                       │ ← generated double separator
│ ◆ Beta Tools [beta] ◆                                 │ ← generated heading
│   beta block 1                                         │
└────────────────────────────────────────────────────────┘
```

## Configure a bundle

Start with a dry run to validate the selected members, header metadata, collisions, and opcode
references.

```bash
sb3-toolchain extensions bundle app \
  --id projectbundle \
  --name "Project Extension Bundle" \
  extensionone extensiontwo
```

If member IDs are omitted, all embedded extensions not already assigned to another bundle are
selected. A bundle requires at least two members. Review the result before applying it.

```bash
sb3-toolchain extensions bundle app \
  --id projectbundle \
  --name "Project Extension Bundle" \
  extensionone extensiontwo \
  --yes
sb3-toolchain check app
sb3-toolchain build app --output dist/project.sb3
```

Member opcodes are transformed as follows in the generated SB3:

```text
Original extension getInfo() opcode:  doSomething
Bundle getInfo() opcode:              extensionone__doSomething
Original project.json opcode:         extensionone_doSomething
Bundled project.json opcode:          projectbundle_extensionone__doSomething
```

The bundle adds `memberId + "__"` to each original opcode, so identical opcodes from different
members cannot collide. Member IDs are restricted to `[a-z0-9]+`, making the delimiter unambiguous.
Loading the bundle is rejected if transformed opcodes still collide within the same member.

Only `projectbundle` appears in `extensions` and `extensionURLs`, and the generated JavaScript calls
`Scratch.extensions.register` once. The initial comments in that JavaScript list every member's
name, ID, author, description, and license so the user can inspect them before granting permission.

By default, the JavaScript omits duplicate recovery data. First confirm that every member license
permits modification and combination, and keep all required notices and corresponding source
available. The bundle header, member code, block icons, opcode namespaces, and storage behavior are
unchanged.

Set `"recoveryCapsule": true` on a bundle, or pass `--include-recovery-capsule` while configuring it,
to append an `SB3-Toolchain-Reversible-Bundle-v1` capsule. It records each original member data URL,
member order, and the original `extensions` and `extensionURLs` order, at the cost of a larger output.

### Palette groups

By default the bundle is one block group: TurboWarp Plus shows and hides it as a whole. With
`--palette-groups members` (`paletteGroups: "members"` on the bundle, or the `paletteGroups` option of
`bundleExtensions` and `planExtensionBundle`), each member becomes a group of its own, so a sprite can show
one member's blocks and hide another's.

```bash
sb3-toolchain extensions bundle app --id projectbundle --name "Project Extension Bundle" \
  extensionone extensiontwo --palette-groups members
```

- The bundle's `getInfo()` declares `groups` with one group per member, named after the member, and marks
  each block with `paletteGroup`. A group a member declares itself becomes `MEMBER-GROUP`, and keeps the
  member's display name for it: one text, or texts by locale such as `{en: 'Speech', ja: 'セリフ'}`, which may
  hold `\n` line breaks. The ID stays the same in every language.
- Opcodes do not change. Standard TurboWarp and Scratch ignore `groups` and `paletteGroup` and show the bundle
  as one category, as before.
- `single` (the default) is not written to the manifest, and the generated JavaScript stays byte for byte as
  before. The bundle declares no `groups`, so it stays one group even if a member marks its blocks.

#### Parent groups

With member palette groups, `memberParentGroups` on the bundle (`--member-parent-groups-json JSON`, or the
`memberParentGroups` option of the API) puts chosen members under one parent group, such as the library
members under "Libraries":

```bash
sb3-toolchain extensions bundle app --id projectbundle --name "Project Extension Bundle" \
  extensionone extensiontwo extensionthree --palette-groups members \
  --member-parent-groups-json '[{"id":"libraries","name":{"en":"Libraries","ja":"ライブラリ"},"members":["extensiontwo","extensionthree"]}]'
```

- Each entry has `id` (`[A-Za-z0-9_-]+`, not a member ID and not starting with `MEMBER-`), `name` (one text or
  texts by locale), optional `menuIconURI` and `color1`–`color3` (`#RRGGBB`), and `members`: bundle members, each
  in at most one parent. It requires `paletteGroups: "members"`.
- `getInfo().groups` declares each parent once, just before its first member, and each of those members' groups
  carries `parent: '<parent id>'`. A parent holds no blocks. A group a member declares itself always carries
  `parent: 'MEMBER'`, so it nests under its member, with or without parent groups.
- Editors that do not know `parent` show the groups flat, as before, and standard TurboWarp and Scratch ignore
  `groups` entirely. Without `memberParentGroups` nothing is written to the manifest.

### Member colours

By default every block of the bundle is drawn in the bundle's colours. With `--member-colors member`
(`memberColors: "member"` on the bundle, or the `memberColors` option of the API), each member's blocks keep
the member's own `color1`–`color3`. This works in every editor, since it sets the colours on the blocks. With
member palette groups too, each member group carries the member's colours, and a group the member declares
keeps its own. A block that sets its own colour keeps it either way. `bundle` (the default) is not written to
the manifest.

### Declare unsandboxed loading

Each extension can declare, in its own header, whether the user must check TurboWarp's "Run
without sandbox" when allowing it:

```js
// Name: Extension One
// ID: extensionone
// By: Example Author
// License: MPL-2.0
// Unsandboxed: required
```

| Value        | Meaning                                                    |
| ------------ | ---------------------------------------------------------- |
| `required`   | The extension cannot work sandboxed                        |
| `optional`   | It works sandboxed, but some features need unsandboxed use |
| `not-needed` | It works fully sandboxed                                   |

Any other value fails the build. The declaration travels with the file, so it holds whether the
extension is bundled or loaded on its own; extensions without the line behave exactly as before.

A bundle derives its requirement from its members: `required` if any member is, otherwise
`optional` if any member is, and `not-needed` only when every member says so. The generated header
records the result as `// Unsandboxed:` along with each member's own value. TurboWarp's permission
dialog itself cannot be changed by an extension, so the bundle acts on what happens after the user
answers it:

- `required` and loaded sandboxed: no member runs. The bundle registers a placeholder under its
  ID whose palette shows the bundle name and how to reload the project unsandboxed, and logs the
  same text as a console error. Without it, a member that throws because it is sandboxed stops the
  whole bundle: in TurboWarp's sandboxed iframe the project never finishes loading, and in a worker
  the user sees only a generic load failure. With the placeholder the project loads, its bundle
  blocks stay in the scripts, and loading the project again unsandboxed restores them.
- `optional` and loaded sandboxed: the members run, and the palette starts with a notice that some
  features are unavailable, also logged as a console warning.
- `not-needed`, no declaration, or loaded unsandboxed: nothing is added.

The notices have built-in English and Japanese text and follow `Scratch.translate.language`,
falling back to the primary language subtag and then to English. Override or add locales with the
bundle's `sandboxedNotice`, an object mapping lowercase language tags to messages; `\n` starts a
new palette label. It is accepted only when the bundle derives `required` or `optional`:

```json
{
  "id": "projectbundle",
  "name": "Project Extension Bundle",
  "members": ["extensionone", "extensiontwo"],
  "sandboxedNotice": {"de": "Projekt erneut laden\nund ohne Sandbox ausführen."}
}
```

## Restore from expanded source

Choose the restoration path based on which artifact is available:

```mermaid
flowchart TD
  Start{"What do you have?"}
  Source["Expanded source directory"]
  SB3["Bundled SB3 only"]
  SourceCommand["extensions unbundle SOURCE_DIR"]
  ArchiveCommand["extensions unbundle INPUT.sb3<br/>--output OUTPUT.sb3"]
  Rebuild["Rebuild from preserved<br/>project.source.json and member JS"]
  Recover["Validate recovery capsule<br/>and reverse project transformations"]
  Restored["SB3 with individual extensions"]

  Start -->|"authoritative source"| Source
  Start -->|"distribution artifact"| SB3
  Source --> SourceCommand --> Rebuild --> Restored
  SB3 --> ArchiveCommand --> Recover --> Restored
```

First inspect the unbundle plan with a dry run, then pass `--yes` to remove the bundle configuration.

```bash
sb3-toolchain extensions unbundle app projectbundle
sb3-toolchain extensions unbundle app projectbundle --yes
sb3-toolchain check app
sb3-toolchain build app --output dist/project.sb3 --yes
```

Because the original JavaScript and project representation are preserved, the next build restores
the individual IDs and data URLs and regenerates block, dynamic block metadata, and monitor opcodes
with their original values. This form of unbundle does not attempt to reverse-engineer a distribution
SB3. It regenerates the project from the saved `project.source.json` and individual JavaScript, so no
information is lost through an inferred opcode reversal. If `extensions update` ran while the bundle
was configured, the project is restored using the updated individual JavaScript.

Treat the expanded source as authoritative. Do not re-import a generated bundled SB3 into that same
authoritative source. Importing it into a new directory expands the distribution composite extension
as one ordinary extension and cannot recover the individual provenance. The normal Git-difference
protection and replacement confirmation still apply if an existing authoritative source is selected
accidentally.

## Restore a bundled SB3 directly

A bundled SB3 containing an explicitly enabled recovery capsule can be unbundled without the expanded
source. The default compact bundle must instead be restored from its authoritative expanded source.
The command is a dry run unless `--yes` is supplied.

```bash
sb3-toolchain extensions unbundle \
  dist/project.sb3 projectbundle \
  --output dist/project.unbundled.sb3
sb3-toolchain extensions unbundle \
  dist/project.sb3 projectbundle \
  --output dist/project.unbundled.sb3 \
  --yes
```

Direct unbundling restores all of the following:

- Bundle opcodes to `memberId_originalOpcode`, while leaving block IDs, `next`, `parent`, and `inputs` unchanged
- Opcodes in ordinary blocks, dynamic block metadata, and monitors
- The bundle storage `components` as storage keyed by the original member IDs
- Individual member data URLs and the original order of `extensions` and `extensionURLs`
- Asset entries and their byte contents

Blocks may be added, deleted, or moved in TurboWarp after bundling. An added block can still be
restored when its opcode has the `bundleId_memberId__originalOpcode` form. An SB3 containing several
bundles can be unbundled one bundle at a time in any order when every bundle has a valid recovery
capsule and their member sets do not overlap.

### Conditions that make direct unbundling impossible

```mermaid
flowchart TD
  Input["Bundled SB3"]
  Capsule{"Valid recovery capsule?"}
  Order{"Extension ID set and<br/>order still match?"}
  Mapping{"Every bundle opcode maps<br/>to exactly one member?"}
  Storage{"Storage uses reversible<br/>format 1 without collisions?"}
  Write["Write unbundled SB3"]
  Refuse["Refuse safely<br/>leave output unchanged"]

  Input --> Capsule
  Capsule -->|"yes"| Order
  Capsule -->|"no"| Refuse
  Order -->|"yes"| Mapping
  Order -->|"no"| Refuse
  Mapping -->|"yes"| Storage
  Mapping -->|"no"| Refuse
  Storage -->|"yes"| Write
  Storage -->|"no"| Refuse
```

The tool refuses the operation without modifying the output instead of guessing when any of the
following conditions apply:

- The target data URL has no `SB3-Toolchain-Reversible-Bundle-v1` capsule because recovery was not
  explicitly enabled, the bundle was produced before recovery capsules were introduced, or it was
  produced by another tool or by hand
- The bundle data URL or capsule was removed, corrupted, or duplicated; its format version is not
  supported; or its bundle ID, member ID, and original JavaScript header ID do not agree
- The ID set or order in `project.extensions` or `extensionURLs` changed after bundling. For example,
  adding or removing another extension makes the exact insertion positions of the original members
  unsafe to infer
- An opcode beginning with `bundleId_` cannot be assigned to a member's `memberId__` namespace, or
  an unclassified bundle opcode reference remains outside ordinary blocks, dynamic block metadata,
  and monitors
- Bundle storage is not `{formatVersion: 1, components: {...}}`, contains an unknown member or an
  additional bundle-level field, or collides with storage already using a restored member ID
- Active reversible bundles contain overlapping members
- The ZIP is empty, `project.json` is missing or invalid, an entry name is unsafe, or entry order
  cannot be preserved safely

If the expanded source is still available, these conditions do not prevent the source-based
`extensions unbundle SOURCE_DIR` workflow from removing the bundle configuration and rebuilding the
project. An older generated SB3 without a recovery capsule cannot be reversed directly from that SB3
alone.

Direct unbundling preserves asset bytes but recompresses the ZIP deterministically. It does not
preserve byte-for-byte identity of input ZIP metadata, entry timestamps, or archive comments.

## Verification

Static validation cannot prove compatibility with browser APIs, cameras, the renderer, or implicit
dependencies between extensions. Run the same project-level automated tests before and after
bundling, then open the generated SB3 in a fresh TurboWarp session and verify all of the following:

- The unsandboxed execution prompt appears only once
- Green flag behavior and block, reporter, hat, monitor, and menu results are unchanged
- Permission paths used by the project, such as camera, network, file picker, and renderer access, work
- An SB3 saved again by TurboWarp retains extension storage and still works after reloading

Exclude a member from the bundle and keep it as an individual extension when its compatibility
cannot be verified.

## Block editor registrations (off by default)

Set `blockEditorRegistrations: true` on a bundle in `embedded-extensions.json` to adapt GUI
registration identities to the bundled opcode namespace. This is a manifest option, not a new CLI flag.
The generated runtime uses `Scratch.gui.blockEditors` only when `apiVersion === 1` and preserves
which registration capabilities the host exposes: `registerSchema`, `registerSchemaBundle`,
`registerSourceEditor`, and `registerFixedInputEditor`.

`extensionId` becomes the bundle ID, `editorId` gains a `memberId__` prefix, and each full member
opcode becomes `bundleId_memberId__opcode`. Schema, bindings, callbacks, validation context, saved
values, and returned disposers retain their identity. Callbacks still receive the GUI's actual block
snapshot, so callbacks that inspect opcodes must accept the bundled opcode. Registrations for a
foreign member are rejected. Frozen host API objects are supported without mutating them.

An absent or false option preserves existing behavior. GUI-less projects, absent capabilities, and
unsupported API versions continue to work. No form implementation, Schema compiler, Ajv, YAML, or
React is added to the bundle. To roll back, disable the option and rebuild, or disable the GUI's editor
feature flags and reload. No data migration is needed. See the [Japanese guide](ja/extension-bundles.md).
