From d57d4db4ab7bf1cce39630ae90e6fd39b0998d53 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:19:19 +0000 Subject: [PATCH] fix(test): share module instances in instance-settings-routes test (#23423) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The server test suite verifies signed Cloud control assertions in `server/src/__tests__/instance-settings-routes.test.ts` through the real `cloudControlMiddleware` and `instanceSettingsRoutes` > - `hoistModuleGraph` in that test loaded `../middleware/cloud-control.js`, `../services/cloud-runtime-identity.js`, and `../routes/instance-settings.js` via `vi.importActual()` inside `Promise.all` > - In Vitest 5, `vi.importActual()` appends a `_vitest_original` query when fetching the module, which can evaluate a second instance of `cloud-control.ts` with separate `WeakMap`s (`idleCapabilities` and `controlClaims`) from the instance imported by `instance-settings.ts`, causing intermittent 403 and missing-capability failures that cancel `master` Cloud readiness and Docker image builds > - This pull request replaces `vi.importActual()` with standard dynamic `import()` for those unmocked modules inside `hoistModuleGraph` > - The benefit is deterministic module identity across middleware, routes, and replay-fence helpers, eliminating flaky `master` CI failures in `Serialized tests (13/16)` ## Linked Issues or Issue Description **What happened?** `verify / Serialized tests (13/16)` intermittently failed all five tests in `instance settings routes > task drain > signed incoming capability handoff` on `master` (for example, Cloud readiness run 46753879142 / Docker run 46753878936). Requests authenticated by `cloudControlMiddleware` returned `403 Forbidden` or passed `incomingWake: undefined` because `verifiedCloudControlClaims(req)` and `verifiedIdleCapabilities(req)` read from a different `cloud-control.ts` module instance than the one populated by `cloudControlMiddleware`. **Expected behavior** `cloudControlMiddleware`, `instanceSettingsRoutes`, and `controlIdentity` should always share a single module instance so `WeakMap` lookups and replay-fence resets behave deterministically. **Steps to reproduce** 1. Run `server/src/__tests__/instance-settings-routes.test.ts` under Vitest 5 (`verify / Serialized tests (13/16)`). 2. Observe `hoistModuleGraph` calling `vi.importActual()` in parallel on `../routes/instance-settings.js`, `../middleware/cloud-control.js`, and `../services/cloud-runtime-identity.js`. 3. When `cloud-control.ts?_vitest_original` evaluates separately from `cloud-control.ts`, the five `signed incoming capability handoff` tests fail. **Paperclip version or commit** `3833e1c8e8d870a4ab387d579e6f44c0a6066930` / `3df59a1853752dea020524815737afec39882be2` **Deployment mode** Other (CI test suite) ## What Changed - Replace `vi.importActual()` with dynamic `import()` for the four unmocked modules loaded in `hoistModuleGraph` in `server/src/__tests__/instance-settings-routes.test.ts`, and document why shared module identity is required. ## Verification - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/instance-settings-routes.test.ts` — 112/112 passed. - `pnpm --filter @paperclipai/server typecheck` — passed. ## Risks - Low risk. Test-only change affecting four unmocked imports inside one test file. ## Model Used - Google DeepMind Antigravity using Gemini 4 Neon (Medium) with reasoning, tool use, and local command execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --- server/src/__tests__/instance-settings-routes.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/__tests__/instance-settings-routes.test.ts b/server/src/__tests__/instance-settings-routes.test.ts index 56b4b211ee1..e9250eea092 100644 --- a/server/src/__tests__/instance-settings-routes.test.ts +++ b/server/src/__tests__/instance-settings-routes.test.ts @@ -89,11 +89,13 @@ function localAdminActor() { describe("instance settings routes", () => { const routeModules = hoistModuleGraph(registerModuleMocks, async () => { + // Use dynamic import() rather than vi.importActual() so middleware and routes + // share the same module instances (and WeakMap/replay-fence state) in Vitest. const [{ errorHandler }, { instanceSettingsRoutes }, { cloudControlMiddleware }, controlIdentity] = await Promise.all([ - vi.importActual("../middleware/index.js"), - vi.importActual("../routes/instance-settings.js"), - vi.importActual("../middleware/cloud-control.js"), - vi.importActual("../services/cloud-runtime-identity.js"), + import("../middleware/index.js"), + import("../routes/instance-settings.js"), + import("../middleware/cloud-control.js"), + import("../services/cloud-runtime-identity.js"), ]); return { errorHandler, instanceSettingsRoutes, cloudControlMiddleware, controlIdentity }; });