Files
PaperClipAI/scripts/check-module-boundaries.test.mjs
T
Nicky LeachandPaperclip f65991a5f1 refactor(server): move scheduled-retry and queued-run dispatch into a run-dispatch module (#12920)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The server heartbeat service dispatches scheduled retries and queued
runs.
> - The service kept policy decisions and database writes in one large
file.
> - This layout made policy branches harder to test and transaction
boundaries harder to inspect.
> - This pull request moves the policy rules and database transactions
into a run-dispatch module.
> - The benefit is a smaller service, pure policy tests, and clear
transaction ownership.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

The server heartbeat service promotes scheduled retries and cancels
stale queued runs.

**Subsystem affected**

server/ — REST API and orchestration services.

**Current behavior**

The heartbeat service contains the policy rules and the database writes
for these dispatch paths.

**Proposed behavior**

A run-dispatch module owns pure policy functions and semantic database
transactions. The public service contracts stay unchanged.

**Reason and benefit**

The new layout separates branch rules from database effects. It makes
each policy branch easier to test and keeps each operation’s row writes
in one transaction.

**Breaking changes**

None. The public service contracts stay unchanged.

## What Changed

- Move scheduled-retry promotion and queued-run staleness rules into
pure functions.
- Add table-driven unit tests for each policy branch.
- Move promotion and cancellation writes into semantic transactions.
- Keep row locking, company isolation, and post-commit effects
unchanged.

## Verification

- `node scripts/check-module-boundaries.mjs` passes.
- `tsc --noEmit` from `server/` reports no errors.
- The focused server test command passes 228 tests in six files.
- Full pull request CI passes.
- Greptile reports 5/5, and all review threads are resolved.

## Risks

The main risk concerns changed transaction boundaries in scheduled-retry
promotion and queued-run cancellation. The focused tests retain coverage
for locking, transactionality, company isolation, and transport
contracts. The public service contracts do not change.

## Model Used

Codex, GPT-5, with code execution and tool use. The context window is
not provided by the runtime.

## 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 have addressed all Greptile and reviewer comments before
requesting merge

---------

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

86 lines
3.6 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 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: "@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(), []);
});