[PR #5087] [MERGED] fix(capability-packages): ETag + 304 for package delivery; immutable version-pinned assets #6621

Closed
opened 2026-10-23 04:12:09 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5087
Author: @kolacheee
Created: 8/15/2026
Status: ✅ Merged
Merged: 8/15/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5082-capability-package-cache


📝 Commits (7)

  • 266d3e1 Merge pull request #5020 from Pasta-Devs/staging
  • 9ce7528 Merge upstream Pasta-Devs/Marinara-Engine staging (through #5068) into main
  • 37e835f fix(capability-packages): cache validators for package delivery — ETag + 304, immutable pinned assets (#5082)
  • 2027142 Merge remote-tracking branch 'upstream/staging' into fix/5082-capability-package-cache
  • 5f7f666 test(capability-packages): assert nosniff survives the 304 early return
  • 29c61fd Merge remote-tracking branch 'upstream/staging' into fix/5082-capability-package-cache
  • a4d9399 fix(capability-packages): route-scoped rate limit for package file serving

📊 Changes

6 files changed (+287 additions, -12 deletions)

View changed files

📝 docs/development/optional-agent-packages.md (+10 -0)
📝 package.json (+2 -1)
📝 packages/server/src/middleware/rate-limit.ts (+9 -0)
📝 packages/server/src/routes/capability-packages.routes.ts (+52 -10)
📝 packages/server/src/services/capability-packages/package-manager.service.ts (+10 -1)
➕ scripts/regressions/capability-package-cache.regression.ts (+204 -0)

📄 Description

Closes #5082

Why

Every installed Agent package's client bundle is fully re-downloaded on every app load: /api/capability-packages/:id/client sends Cache-Control: no-cache, must-revalidate with no ETag/Last-Modified, so the browser has nothing to revalidate against and must re-fetch the whole body each time. For anyone with World Maps installed that's the full hierarchical-maps bundle on every app open — on phones too — and it makes bundle size a per-boot tax for any future package that ships more client code or art (e.g. a game-surface Experience).

The fix costs no freshness: every served package file already has a trusted content hash in manifest.files[] (the serve path re-verifies the bytes against it on every read), which is a perfect strong ETag.

What changed

  • packages/server/src/services/capability-packages/package-manager.service.ts: clientEntrypoint() and browserTabAsset() now also return the manifest-declared sha256 for the served file (the declaration they already locate; no new lookups, no behavior change).
  • packages/server/src/routes/capability-packages.routes.ts:
    • GET /:id/client — sends ETag: "<manifest sha256>" and answers a matching If-None-Match with 304. Cache-Control stays no-cache, must-revalidate — deliberately not immutable even though the URL carries ?v=: a package author (or the catalog) can republish the same version string with different bytes during development, and an immutable client bundle would pin the stale copy with no eviction short of a hard reload. no-cache + ETag keeps "always revalidate" while making the revalidation answer 304 instead of re-sending the body.
    • GET /:id/assets/* — same ETag + 304 support; additionally, when the request carries ?v= equal to the installed version, the response is public, max-age=31536000, immutable (path + version + manifest hash pin the exact bytes). Requests without the pin keep today's private, no-cache, must-revalidate byte-for-byte.
    • X-Content-Type-Options: nosniff retained on all responses, including 304s.
  • New tracked regression scripts/regressions/capability-package-cache.regression.ts (wired as regression:capability-cache, included in regression:issues): 200-with-ETag, 304 on exact/weak/list If-None-Match, full body on mismatch, a different validator after a version update (stale validator must not mask a new bundle), immutable-only-when-pinned asset behavior, and the preserved 404s for unknown packages / undeclared assets.
  • docs/development/optional-agent-packages.md: short "Delivery and caching" note.

No client changes: the dynamic import() in use-capability-packages.ts rides the normal browser HTTP cache (it already appends ?v=<version>, so the cache key was correct all along), and a 304 revalidation is transparent to it — pinned by the existing capability-client-version-refresh regression.

No storage-format change (HTTP headers only). Root package.json diff touches scripts only; pnpm version:check passes.

Validation

  • pnpm check
  • pnpm regression:capability-cache (new)
  • capability-package-lifecycle + capability-client-version-refresh regressions (in regression:issues)
  • pnpm regression:static-cache (client-static header contract untouched)
  • pnpm version:check
  • Manually verify with an installed package (e.g. hierarchical-maps): first load returns 200 with an ETag; a reload sends If-None-Match and gets 304 with no body; installing a package update returns 200 with a different ETag and the new bundle hot-swaps as before.
  • Manually verify a home-browser-tab package's icons still render.

Docs note: the docs/development/optional-agent-packages.md edit needs a docs-i18n mirror — intended to be batched into the pending round-8 catch-up rather than a standalone [docs-i18n] issue.

Summary by CodeRabbit

  • New Features

    • Added HTTP caching support for installed package files using manifest-based SHA-256 ETags.
    • Client bundles now revalidate efficiently and can return 304 Not Modified responses.
    • Version-pinned assets support immutable caching, while unpinned assets revalidate automatically.
  • Bug Fixes

    • Added validation to ensure served assets match their declared package files and hashes.
  • Tests

    • Added regression coverage for caching, validators, security headers, package updates, and missing files.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/5087 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/15/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5082-capability-package-cache` --- ### 📝 Commits (7) - [`266d3e1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/266d3e1dfd441ec854fdf5a40305799baa04dd9c) Merge pull request #5020 from Pasta-Devs/staging - [`9ce7528`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9ce7528263e0e49aad2195959bb76072d8c1a0a2) Merge upstream Pasta-Devs/Marinara-Engine staging (through #5068) into main - [`37e835f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/37e835ff5f84bc885688f9b826908ae72ba0b6e5) fix(capability-packages): cache validators for package delivery — ETag + 304, immutable pinned assets (#5082) - [`2027142`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2027142a454a2c3a0f511bbb9986cde1764e5e06) Merge remote-tracking branch 'upstream/staging' into fix/5082-capability-package-cache - [`5f7f666`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5f7f666cb82098eb024236c4836f2c7f3e837a3f) test(capability-packages): assert nosniff survives the 304 early return - [`29c61fd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/29c61fd8879318e0458962bfe6099d62a5ce3d48) Merge remote-tracking branch 'upstream/staging' into fix/5082-capability-package-cache - [`a4d9399`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a4d93991b9543a8f12d5dc0b9f215a44156f15f4) fix(capability-packages): route-scoped rate limit for package file serving ### 📊 Changes **6 files changed** (+287 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `docs/development/optional-agent-packages.md` (+10 -0) 📝 `package.json` (+2 -1) 📝 `packages/server/src/middleware/rate-limit.ts` (+9 -0) 📝 `packages/server/src/routes/capability-packages.routes.ts` (+52 -10) 📝 `packages/server/src/services/capability-packages/package-manager.service.ts` (+10 -1) ➕ `scripts/regressions/capability-package-cache.regression.ts` (+204 -0) </details> ### 📄 Description Closes #5082 ## Why Every installed Agent package's client bundle is fully re-downloaded on every app load: `/api/capability-packages/:id/client` sends `Cache-Control: no-cache, must-revalidate` with no `ETag`/`Last-Modified`, so the browser has nothing to revalidate against and must re-fetch the whole body each time. For anyone with World Maps installed that's the full hierarchical-maps bundle on every app open — on phones too — and it makes bundle size a per-boot tax for any future package that ships more client code or art (e.g. a game-surface Experience). The fix costs no freshness: every served package file already has a trusted content hash in `manifest.files[]` (the serve path re-verifies the bytes against it on every read), which is a perfect strong ETag. ## What changed - `packages/server/src/services/capability-packages/package-manager.service.ts`: `clientEntrypoint()` and `browserTabAsset()` now also return the manifest-declared `sha256` for the served file (the declaration they already locate; no new lookups, no behavior change). - `packages/server/src/routes/capability-packages.routes.ts`: - `GET /:id/client` — sends `ETag: "<manifest sha256>"` and answers a matching `If-None-Match` with `304`. Cache-Control stays `no-cache, must-revalidate` — **deliberately not `immutable`** even though the URL carries `?v=`: a package author (or the catalog) can republish the same version string with different bytes during development, and an immutable client bundle would pin the stale copy with no eviction short of a hard reload. `no-cache` + ETag keeps "always revalidate" while making the revalidation answer `304` instead of re-sending the body. - `GET /:id/assets/*` — same ETag + `304` support; additionally, when the request carries `?v=` equal to the installed version, the response is `public, max-age=31536000, immutable` (path + version + manifest hash pin the exact bytes). Requests without the pin keep today's `private, no-cache, must-revalidate` byte-for-byte. - `X-Content-Type-Options: nosniff` retained on all responses, including `304`s. - New tracked regression `scripts/regressions/capability-package-cache.regression.ts` (wired as `regression:capability-cache`, included in `regression:issues`): 200-with-ETag, `304` on exact/weak/list `If-None-Match`, full body on mismatch, a **different** validator after a version update (stale validator must not mask a new bundle), immutable-only-when-pinned asset behavior, and the preserved 404s for unknown packages / undeclared assets. - `docs/development/optional-agent-packages.md`: short "Delivery and caching" note. No client changes: the dynamic `import()` in `use-capability-packages.ts` rides the normal browser HTTP cache (it already appends `?v=<version>`, so the cache key was correct all along), and a `304` revalidation is transparent to it — pinned by the existing capability-client-version-refresh regression. No storage-format change (HTTP headers only). Root `package.json` diff touches `scripts` only; `pnpm version:check` passes. ## Validation - [x] `pnpm check` - [x] `pnpm regression:capability-cache` (new) - [x] capability-package-lifecycle + capability-client-version-refresh regressions (in `regression:issues`) - [x] `pnpm regression:static-cache` (client-static header contract untouched) - [x] `pnpm version:check` - [x] Manually verify with an installed package (e.g. hierarchical-maps): first load returns `200` with an `ETag`; a reload sends `If-None-Match` and gets `304` with no body; installing a package update returns `200` with a different `ETag` and the new bundle hot-swaps as before. - [x] Manually verify a home-browser-tab package's icons still render. Docs note: the `docs/development/optional-agent-packages.md` edit needs a docs-i18n mirror — intended to be batched into the pending round-8 catch-up rather than a standalone `[docs-i18n]` issue. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added HTTP caching support for installed package files using manifest-based SHA-256 ETags. * Client bundles now revalidate efficiently and can return `304 Not Modified` responses. * Version-pinned assets support immutable caching, while unpinned assets revalidate automatically. * **Bug Fixes** * Added validation to ensure served assets match their declared package files and hashes. * **Tests** * Added regression coverage for caching, validators, security headers, package updates, and missing files. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:12:09 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#6621
No description provided.