Skip to content

build(xterm): make the xterm patch reproducible from a pinned upstream build - #11894

Closed
nwparker wants to merge 3 commits into
nwparker/ime-designfrom
nwparker/ime-xterm-patch-build
Closed

nwparker wants to merge 3 commits into
nwparker/ime-designfrom
nwparker/ime-xterm-patch-build

Conversation

@nwparker

@nwparker nwparker commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Makes the vendored xterm patch reproducible instead of hand-maintained.

Orca patches @xterm/xterm to add composition-session events that the terminal IME path depends on. The patch was previously edited by hand against built output, so nobody could tell which upstream commit it applied to, whether it still applied cleanly, or what it actually changed semantically. That is a bad foundation for the IME fixes in this stack, which all reason about the patched CompositionHelper.

Changes:

  • config/patches/xterm-upstream.json pins the exact upstream revision the patch is generated from.
  • config/scripts/regenerate-xterm-patches.mjs rebuilds both the src patch and the dist patch from that pinned build, so the diff is derived, not authored.
  • Splits out @[email protected] so the TypeScript source changes are reviewable — this is what makes the CompositionHelper behavior in fix(ime): stop dropping and splitting composition commits in the terminal #11896/fix(ime): let Escape reach the shell when the composition flag has drifted #11897 auditable at all.
  • docs/reference/xterm-patch-regeneration.md documents the workflow.
  • A PR CI job regenerates and verifies the patch is byte-identical, so drift fails loudly.
  • Second commit: the generated patch deletes the bundle sourcemaps rather than carrying them, cutting it from 7,334,810 to 1,529,822 bytes (−79.1%).
  • Third commit: the generator now maintains every place pnpm records the patch hash, not just one of them. See below — this was a real CI failure on the PRs above, not a hypothetical.

Why delete the sourcemaps

The patch rewrites lib/xterm.js and lib/xterm.mjs, so a retained .map would have to move with them. Simply omitting the map hunks — what the two addon patches in this repo do today — ships maps whose offsets point at the wrong code, which is worse than having none. Deleting them is the honest form of the same size saving:

before after
config/patches/@[email protected] 7,334,810 1,529,822
…/xterm-src/@[email protected] 32,583 32,583

sourcemaps.policy in the manifest controls this, and assertSourcemapPolicy rejects any value the generator does not implement — without it a typo would silently fall back to including the maps and re-inflate the patch by 5.8 MB.

Verified against a real install, not a scratch project: pnpm install --frozen-lockfile succeeds cold, the maps are absent from node_modules/@xterm/xterm/lib/, zero sourceMappingURL references remain, and the two bundles are byte-identical to a clean build after stripping the trailing comment (−34 B / −36 B). --check reports in sync (1529822 bytes) from an empty work directory, so the reduction is reproducible from nothing.

.gitattributes

/config/patches/@xterm__xterm@*.patch is marked -diff -text. -text is load-bearing, not cosmetic: pnpm hashes the patch byte-for-byte, so a CRLF checkout on Windows changes the hash and breaks pnpm install. The rule lands in the same commit that introduces the patch — parking it higher in the stack would leave every intermediate PR broken for Windows contributors.

pnpm writes the patch hash in two places

--check and --write previously managed one of them, and the gap cost this stack a full CI failure. Worth writing down, because the failure mode is specifically designed to look like success.

pnpm keys a patched package by the sha256 of the patch file. It records that hash under patchedDependencies:, and again inside every resolution key that depends on the patched package — 23 of them for @xterm/xterm, in two different spellings:

patchedDependencies:
  '@xterm/[email protected]':
    hash: ec0d5d6e…              # ← the generator maintained this one

snapshots:
  '@xterm/[email protected](patch_hash=ec0d5d6e…)':   # ← and not these
    dependencies:
      '@xterm/xterm': 6.1.0-beta.287(patch_hash=ec0d5d6e…)

Regenerating the patch therefore produced a lockfile with a correct patchedDependencies hash and 23 stale resolution keys. On a warm store pnpm install resolves from cache, reports success, and leaves the stale keys in place; pnpm install --frozen-lockfile then passes locally. On a cold store — which is CI — the same lockfile drifts and the install step fails. pnpm install --force makes it worse: it rewrites the resolution keys back to the cached value, so the obvious remedy silently reintroduces the bug.

That is what took out ~40 checks on the two PRs above this one. The install job's diff read -d396b2ad / +ec0d5d6e, and shasum -a 256 of the patch file agreed with CI, not with the lockfile.

The fix is in regenerate-xterm-patches.mjs:

  • readLockfileResolutionHashes finds both spellings (name@version(patch_hash=…) and the bare : version(patch_hash=…)).
  • lockfilePatchHashIsStale is now the staleness predicate for both --write and --check, so a lockfile stale in its resolution keys alone is a --check failure rather than a clean bill of health.
  • --check's failure message prints all three values — patchedDependencies, the distinct stale resolution hashes, and the patch's own sha256 — because the useful diagnostic is which of the two sites disagrees.
  • Two new tests, one per direction. The fixture lockfile carries both spellings; the second test starts from a lockfile whose patchedDependencies is already correct and asserts --check still fails, which is exactly the case the old predicate passed.
  • Rule 6 in docs/reference/xterm-patch-regeneration.md states the operational consequence: --check is the authority on the lockfile, not pnpm install. Always finish on step 4.

Both fix PRs' lockfiles were repaired per-commit, so each commit's patchedDependencies, its 23 resolution keys, and the sha256 of the patch file it carries all agree at that commit — verified individually rather than only at the tip.

Screenshots

No visual change.

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test — regenerate-xterm-patches.test.mjs (new, 27 tests) and pr-workflow-parallelism.test.mjs pass
  • node config/scripts/regenerate-xterm-patches.mjs --check against an empty work dir — full clone, npm ci, two builds, byte-identical
  • pnpm install --frozen-lockfile cold, then the terminal-pane suite (219 files / 2,902 tests) against the resulting install
  • pnpm build — not run locally; the CI job exercises the regeneration path
  • Added or updated high-quality tests that would catch regressions

On test assurance, stated plainly: the unit tests cover the pure pieces — diff normalization, hunk splitting, round-trip stability, the commit and build-order assertions, the sourcemap-policy guard, and lockfile coupling. They cannot prove the rebuild reproduces upstream; only the CI job that actually clones and builds does that. The runtime test in this PR asserts the version banner the patched bundle emits, which reads the bundle itself; provenance against the pinned commit stays the generator's job, since src/common/Version.ts is the one source file the generator deliberately exempts from comparison (upstream's publish step rewrites it).

AI Review Report

Reviewed for cross-platform correctness of the regeneration script, which is the main risk here since it shells out and manipulates paths.

  • Path handling uses Node path utilities rather than assuming /.
  • The script is invoked through pnpm/Node, not a shell-specific pipeline, so it runs on Windows as well as macOS/Linux.
  • The -text attribute above is the Windows-specific correctness fix, not a formatting preference.
  • No hardcoded platform separators or metaKey-style assumptions (no UI surface in this PR).

Risk checked and accepted: pinning an upstream beta means a future upstream bump is a deliberate, reviewable step rather than an implicit one. That is the intent.

Risk checked and accepted: nothing in this repo consumes the xterm bundle sourcemaps at build or run time, so deleting them costs no debuggability that was actually available — the maps that shipped before were already only as good as their offsets.

Security Audit

  • Dependency: pins a specific upstream @xterm/xterm revision and verifies the generated patch byte-for-byte in CI, which narrows the supply-chain surface compared to a hand-edited patch.
  • Command execution: the regeneration script runs a pinned upstream build; it takes no user-controlled input.
  • No auth, secrets, or IPC surface touched. pnpm-lock.yaml changes are the patch-hash update and its peer-suffix propagation only.

Notes

Base: #11893. This PR exists to make the two fix PRs above it verifiable — reviewers of #11896/#11897 can read the actual patched CompositionHelper.ts source.

…m build

The xterm patch is 7.3 MB of diff over minified bundles with no recorded
provenance, so no one could regenerate it or tell whether the source hunks and
the bundle hunks still agree. Any IME fix that touches CompositionHelper had to
be hand-applied to four generated files.

Adds config/patches/xterm-src/ as the source of truth (832 lines over four
files), a pinned upstream manifest, and a generator that rebuilds the bundles
from that commit with the toolchain its lockfile pins, then derives the full
patch. `--check` runs it in CI as the new xterm_patch_sync job.

The committed patch is regenerated to match pnpm's own diff output (-U3 rather
than the hand-made -U6). This changes the patch bytes but no installed byte:
both patches applied to pristine tarballs produce recursively identical trees,
and all 8 patched files hash-identical to the installed package. pnpm's
patchedDependencies hash is sha256 of the patch file — verified for all five of
Orca's patches — so pnpm-lock.yaml carries the matching one-line update.
@nwparker
nwparker force-pushed the nwparker/ime-xterm-patch-build branch from 4afd75c to 8692b6f Compare August 1, 2026 23:44
The patch carried 5.8MB of sourcemaps for bundles it rewrites, so the maps it
shipped described the wrong offsets anyway. The manifest's sourcemaps.policy was
documentation the generator never read; it is now the switch that drives this.

Deleting the maps rather than omitting their hunks is what keeps the patch
honest: omission saves the same bytes but leaves a map pointing into code that
moved. Nothing here consumes them - sourcemaps are off in every build config and
the package is a devDependency, so it never reaches a packaged app.

Also moves the .gitattributes -text rule down to the commit that introduces the
patch. It was added at the top of the stack, which left every commit in between
shipping a byte-hashed artifact that a CRLF checkout would corrupt.

Co-authored-by: Orca <[email protected]>
pnpm records the hash in patchedDependencies and again in every resolution key
that depends on the patched package. The generator only maintained the first, so
a regenerated patch left the rest stale: installs succeeded on a warm store and
the lockfile drifted on a cold one, which is CI.

Co-authored-by: Orca <[email protected]>
@nwparker

nwparker commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Leaving this open — not superseded. Build reproducibility for the xterm patch is independent of who owns composition, and main still carries a hand-maintained patch (config/patches/@[email protected], now 355 lines) with no pinned-upstream provenance.

That patch grew during this work and is load-bearing: it reworks the _inputEvent / _keyDownSeen gate, which is the reason the text system's output reaches the terminal at all. A 355-line patch we cannot regenerate from a known upstream build is a real maintenance liability at the next xterm bump.

One file here (terminal-ime-xterm-transaction-events.test.ts) went away with #13128, so it needs a rebase, but the build change stands on its own.

@nwparker

Copy link
Copy Markdown
Contributor Author

Closing as superseded after checking current main.

Superseded by merged #15223, which restored the pinned-upstream xterm patch regeneration harness and added its CI gate. Main now has the upstream pin, source patches, regeneration tooling, and later terminal dependency upgrades. The earlier comment that this was not superseded predates that merge.

Closing this PR only; its branch is being retained.

@nwparker nwparker closed this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant