Files
PaperClipAI/scripts/check-module-boundaries.test.mjs
Nicky LeachandPaperclip ae0c1fbbd5 refactor(server): move the admission half of the deferred wake state machine into the wake-queue module (#13136)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The heartbeat service admits wake requests while an issue has an
active execution run.
> - That admission branch mixes wake policy, database reads, and
database writes in one service.
> - This structure makes the wake-queue boundary hard to test and
extend.
> - This pull request moves the admission policy and its database
adapter into the wake-queue module.
> - The result keeps heartbeat orchestration small and makes the
admission behavior testable in isolation.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

The heartbeat service now delegates deferred wake admission to the
wake-queue module. The module keeps the existing merge, defer, and
ordinary-wake outcomes.

**Subsystem affected**

server/ — REST API and orchestration services.

**Current behavior**

The heartbeat service contains a 146-line branch that reads wake state,
chooses an outcome, and writes the result.

**Proposed behavior**

The wake-queue module owns the pure admission decision and the adapter
reads and writes. The heartbeat service calls one module method.

**Reason and benefit**

This boundary reduces service coupling and lets module tests cover the
admission policy. The change keeps the existing reason strings and
outcomes.

**Breaking changes**

None. The change preserves the current behavior and public API.

**Additional context**

This pull request follows [PR
#13132](https://github.com/paperclipai/paperclip/pull/13132), which
merged the first slice of this refactor. I searched GitHub for duplicate
and related pull requests before opening this pull request.

## What Changed

- Move deferred wake admission policy into
`server/src/modules/wake-queue`.
- Add module ports and a PostgreSQL adapter for the admission reads and
writes.
- Keep the existing wake outcomes and stored reason strings.
- Extend the module boundary check to reject service imports from the
application layer.
- Add unit and adapter tests for the moved behavior.

## Verification

- `node --test scripts/check-module-boundaries.test.mjs` passes.
- The `server/src/modules/wake-queue` suite passes 58 tests.
- The eight pinned heartbeat and queued-comment tests remain unchanged
and require CI verification.
- Every continuous-integration check must reach a terminal green state
before merge.

## Risks

The refactor changes the location of wake admission logic. A missed
adapter condition could change deferred wake behavior. The tests cover
the policy outcomes and the adapter writes. The residual tenant-scope
risk remains documented in the review record.

## Model Used

OpenAI Codex, GPT-5, tool use and code execution. The implementation
author ran the tests and prepared the commit set.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-10 00:55:48 -07:00

91 lines
3.8 KiB
JavaScript

import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { extractImportSpecifiers, scanModuleBoundaries } from "./check-module-boundaries.mjs";
test("extractImportSpecifiers recognizes supported TypeScript dependency forms", () => {
assert.deepEqual(
extractImportSpecifiers([
'import type { Db } from "@paperclipai/db";',
'export { helper } from "./helper.js";',
'const adapter = await import("../adapters/postgres.js");',
'const postgres = require("postgres");',
'import fs = require("node:fs");',
].join("\n")),
["@paperclipai/db", "./helper.js", "../adapters/postgres.js", "postgres", "node:fs"],
);
});
test("scanModuleBoundaries rejects outward dependencies and module-internal imports", () => {
const serverSrc = mkdtempSync(join(tmpdir(), "paperclip-module-boundaries-"));
const modulesRoot = join(serverSrc, "modules");
const write = (relativePath, source) => {
const filePath = join(serverSrc, relativePath);
mkdirSync(join(filePath, ".."), { recursive: true });
writeFileSync(filePath, source);
};
try {
write("modules/watchdog/domain/policy.ts", [
'import { eq } from "drizzle-orm";',
'import { service } from "../../../services/example.js";',
'import { parse } from "../../../adapters/utils.js";',
'import { run } from "../application/run.js";',
].join("\n"));
write(
"modules/watchdog/application/run.ts",
[
'import { adapter } from "../adapters/postgres.js";',
'import { parse } from "../../../adapters/application-utils.js";',
'import { forbidden } from "../../../errors.js";',
'import { helper } from "../../../services/example.js";',
'import db = require("@paperclipai/db");',
].join("\n"),
);
write("modules/watchdog/adapters/postgres.ts", 'import { eq } from "drizzle-orm";\n');
write("modules/watchdog/index.ts", 'export { run } from "./application/run.js";\n');
write("services/example.ts", 'import { run } from "../modules/watchdog/application/run.js";\n');
const violations = scanModuleBoundaries({ serverSrc, modulesRoot });
assert.deepEqual(
violations.map(({ specifier, reason }) => ({ specifier, reason })),
[
{ specifier: "../adapters/postgres.js", reason: "application cannot import concrete adapters" },
{
specifier: "../../../adapters/application-utils.js",
reason: "application cannot import concrete adapters",
},
{ specifier: "../../../errors.js", reason: "application cannot import HTTP error helpers" },
{
specifier: "../../../services/example.js",
reason: "application cannot import server services or routes",
},
{ specifier: "@paperclipai/db", reason: "application cannot import database packages" },
{ specifier: "drizzle-orm", reason: "domain cannot import database packages" },
{
specifier: "../../../services/example.js",
reason: "domain cannot import server services, routes, or adapters",
},
{
specifier: "../../../adapters/utils.js",
reason: "domain cannot import server services, routes, or adapters",
},
{ specifier: "../application/run.js", reason: "domain cannot depend on outer module layers" },
{
specifier: "../modules/watchdog/application/run.js",
reason: "imports inside module watchdog instead of its index",
},
],
);
} finally {
rmSync(serverSrc, { recursive: true, force: true });
}
});
test("the repository's feature modules satisfy their import boundaries", () => {
assert.deepEqual(scanModuleBoundaries(), []);
});