# npm package releases

This repository publishes an allowlisted package when an authorized
tag under `npm/` reaches GitHub. The tag is a release instruction; the package
manifest at the tagged commit remains authoritative for the package name and
version.

The current release mapping is:

| Release key | Package | Directory |
| --- | --- | --- |
| `silvermoon` | `silvermoon` | `.` |

A stable tag such as `npm/silvermoon/v0.2.0` publishes with the npm `latest`
dist-tag. A named prerelease such as `npm/silvermoon/v0.2.0-rc.1` publishes
with the `rc` dist-tag. Numeric-only prerelease channels are intentionally
rejected. Preparing a manifest candidate does not authorize tag creation or
publication.

## One-time configuration

### npm trusted publisher

In the npm package settings, add a GitHub Actions trusted publisher with these
exact values:

- Organization or user: `shazhou-ww`
- Repository: `silvermoon`
- Workflow filename: `publish-npm.yml`
- Environment: `npm`
- Allowed action: direct `npm publish`

The workflow filename is identity-sensitive on npm. If it changes, update the
npm trusted-publisher configuration before attempting another release. Do not
create an npm automation token or store `NPM_TOKEN` or `NODE_AUTH_TOKEN` in
GitHub secrets; the workflow uses GitHub OIDC with `id-token: write`.

### GitHub environment

Create an environment named `npm`. Limit deployments to protected tags in the
`npm/**` namespace. Add required reviewers when the repository needs a manual
release approval; the workflow itself does not require a repository secret.

The npm trusted publisher and the workflow environment name must remain the
same. A mismatch prevents npm from accepting the OIDC identity.

The workflow uses a GitHub-hosted runner, `id-token: write`, Node 24,
`actions/setup-node` with `registry-url: https://registry.npmjs.org`, and a
pinned npm CLI version at or above `11.5.1`. These are source-controlled OIDC
preconditions; do not replace them with a write token.

### Tag ruleset

Create an active GitHub tag ruleset targeting `npm/**`. Restrict tag creation,
update, and deletion to the release maintainers or a dedicated release team.
Do not allow release tags to be moved after creation. The workflow independently
fetches `origin/main` and rejects a tagged commit that is not reachable from
that refreshed branch.

Tag protection is part of the trust boundary: GitHub loads a workflow from the
tagged commit, so only authorized maintainers should be able to create tags in
the release namespace.

## Publish a version

1. Update the selected package's `version` in its committed `package.json`.
2. Run `pnpm install --frozen-lockfile` and `pnpm check` (or `pnpm check:release`).
   Neither validation command publishes a package. The complete check
   includes unit, contract, integration, package contents, installed-package
   E2E, and skill discovery.
3. Merge the version change to `main`; do not tag an unmerged branch or local
   working tree.
4. When the release is a deployment criterion of an active Silvermoon idea,
   record implementation acceptance for the approved current idea revision,
   publish that status-only commit, and require `silvermoon whats-next <idea>` to
   return `deploy-idea`. Never create the release tag while the idea still
   derives `implementing`.
5. Fetch the current primary branch and tags.
6. Create the package-specific tag at `origin/main` and push that exact tag.

For an explicitly authorized `silvermoon@0.2.0-rc.1` release:

```sh
git fetch origin main --tags
git tag npm/silvermoon/v0.2.0-rc.1 origin/main
git push origin refs/tags/npm/silvermoon/v0.2.0-rc.1
```

The tag version must exactly equal [`package.json`](../package.json). The
workflow never edits a manifest or chooses a version.

The [`publish-npm.yml`](../.github/workflows/publish-npm.yml) workflow then:

1. verifies the tagged commit is reachable from refreshed `origin/main`;
2. installs the frozen pnpm dependencies;
3. runs the allowlisted release planner and confirms the version is absent
   from npm, or records that an exact-version rerun must skip publication;
4. runs `pnpm test:unit`, `pnpm test:contract`, `pnpm test:integration`, and
   `pnpm check:skills`, Markdown/whitespace and checked-out Silvermoon snapshot
   validation, all unconditionally;
5. creates an isolated `git archive` staging tree and generates commit-pinned
   English and Chinese package READMEs without changing the tagged checkout;
   the repository READMEs use canonical `./assets/...` image paths so branch,
   pull-request, and local previews show the current checkout, while staging
   converts those images to release-commit-pinned jsDelivr URLs and converts
   ordinary relative document links to commit-pinned GitHub blob URLs;
   repository artwork remains available at those immutable URLs but is
   excluded from the npm tarball;
6. records the tagged commit as `gitHead` and the canonical `README.md` content
   in the isolated package manifest, creates one tarball, records its path,
   SHA-256, npm shasum, registry integrity, and complete file list, then passes
   that same file to `npm run pack:check` and `npm run test:e2e`; the
   pre-publication check requires the tarball manifest to contain that exact
   commit and README metadata and requires a fresh directory pack to be
   byte-identical;
7. publishes the unchanged staged directory with provenance and the derived npm
   dist-tag when the version was absent, allowing npm to include the
   package-level README metadata while the deterministic pack check and post-publication
   verifier guarantee the registry tarball matches the candidate bytes; and
8. runs `verify-npm-release.ts` until the registry is consistent or the
   bounded retry window expires.

The post-publication verifier requires the exact version and dist-tag, matching
tarball and registry `gitHead`, registry integrity and tarball bytes, both
tarball READMEs, MIT license, homepage, bugs and repository metadata, npm
publish and SLSA provenance attestations, tagged Git commit, and commit-pinned
jsDelivr SVG responses to match the candidate.
README generation rejects traversal, non-canonical paths, and relative resources
outside the approved `./assets/...` image namespace rather than guessing a
published destination.
Releases on the `latest` dist-tag additionally require npm's package-level
README to match the candidate; prerelease channels leave that `latest` package
page state unchanged. A successful run prints `VERIFY_NPM_RELEASE_OK` with the
verified identities.

Release runs are serialized within this repository. The registry preflight and
`npm publish` cannot form one cross-system transaction, so an external
publisher could still win that interval; npm then atomically rejects the
duplicate publication without replacing the existing version.

### GitHub Release

Prepare release notes in the repository before creating the immutable tag.
After the trusted-publishing workflow succeeds and its verifier proves the npm
result, create the GitHub Release from the already-existing tag. For `0.4.0`:

```sh
gh release create npm/silvermoon/v0.4.0 \
  --verify-tag \
  --title "Silvermoon 0.4.0" \
  --notes-file .github/release-notes/0.4.0.md
```

Do not let a release command create, move, or replace the tag. The GitHub
Release, npm version, provenance, changelog, and release notes must resolve to
the same commit before deployment acceptance.

## Failure behavior

Publication stops before `npm publish` when the tag is malformed, the release
key is unknown, the package is private, the package name or version differs
from the mapping and tag, the npm publish configuration is not public npmjs,
the commit is outside `origin/main`, registry state cannot be observed, or
validation fails. If the exact version already exists during a workflow rerun,
the publish step is skipped and the verifier must prove that registry content
is byte-identical to the rebuilt candidate and carries the expected provenance.
Any mismatch is a hard failure.

For a transient GitHub or registry failure before publication, rerun the same
workflow run. Do not move or recreate the tag. For a source, manifest, or
validation failure, make a new commit on `main`, choose a new version, and push
a new tag after the fix is merged. npm versions and release tags are immutable.
For a transient verification failure after publication, rerun only the failed
workflow job when GitHub offers that option; the planner recognizes the
existing version, skips `npm publish`, and performs the same complete
verification again.

### 0.4.0 one-time recovery

The immutable `npm/silvermoon/v0.4.0` tag remains the failed original release
instruction and must not be moved, deleted, or recreated. The only authorized
recovery source is the protected `npm/silvermoon-recovery/v0.4.0` tag. Its
commit must descend from the original release commit and be reachable from
`origin/main`.

The workflow maps the exact recovery tag to `npm/silvermoon/v0.4.0` only for
package selection and version validation. It builds and archives the recovery
commit itself; the tarball `gitHead`, generated README links, npm provenance
source, post-publication verifier, and GitHub Release all use the actual
recovery tag and commit. Do not override `GITHUB_REF`, `GITHUB_SHA`, or any
provenance environment value.

After the trusted-publishing workflow and verifier succeed, create the
`0.4.0` GitHub Release from the recovery tag:

```sh
gh release create npm/silvermoon-recovery/v0.4.0 \
  --verify-tag \
  --title "Silvermoon 0.4.0" \
  --notes-file .github/release-notes/0.4.0.md
```

This is a one-time recovery for an unpublished version, not a second general
release key. Future source, manifest, or validation failures still require a
new version.

## Add another package

1. Give the package a committed canonical `name`, SemVer `version`, and
   `publishConfig` with `access: public` and registry
   `https://registry.npmjs.org/`.
2. Add a fixed release-key entry to `RELEASE_PACKAGES` in
   [`prepare-npm-release.ts`](../bin/prepare-npm-release.ts). Never derive
   a filesystem path directly from tag text.
3. Extend the release tests with the package selection, identity, version, and
   dist-tag cases.
4. Add the same `publish-npm.yml` trusted publisher to that npm package, using
   the `npm` environment.
5. Keep its tags under the protected `npm/<release-key>/v<semver>` convention.

Run `node --test test/unit/prepare-npm-release.test.ts
test/integration/prepare-npm-release.test.ts
test/contract/npm-release.test.ts` for focused release validation and
`pnpm check` for the complete repository suite before merging the mapping
change.
