Migrate service

From TryGhost/Ghost · View interactive version

Migrate service is an AI agent skill from the GitHub repository TryGhost/Ghost. Migrate one existing Ghost Core service to the boot-owned defineService pattern. Use when asked to migrate or prepare an individual service, including its dependency wiring, consumers, inventory and enforcement. Establish coverage first and keep the initializer migration to one service and one commi

How to use this skill

Skill file

Migrate one service

Prepare a small, behavior-preserving migration of one existing service. This is

an execution workflow, not permission to redesign a service's lifecycle. The

services guide is the authoring

guide; read it rather than copying an earlier migration blindly.

For a new service, use add-service to generate and

complete the root instead.

1. Establish the starting point

services guide,

kernel contract,

testing guide and

contribution workflow.

work. Fetch origin and start from current origin/main; refresh dependencies

with pnpm bootstrap in a fresh checkout or after dependency changes.

ghost/core/server/services/legacy-service-inventory.yaml and any existing

migration plan. If a plan-registry skill is available, use it to find/update that plan;

do not create a competing plan in the repository.

Preparation can proceed against a proposed policy, but record its exact head

and repeat validation against the policy that actually merges.

2. Audit the whole service and its callers

Use rg across production and tests. Include deep imports, nested helpers,

separate actions, job handlers, CLI use and CommonJS consumers. Also inspect

shared boot harnesses and tests that construct routers or renderers indirectly;

a search for the service name alone misses those fixtures. Record:

Confirm the capability belongs in services. A reusable constructor or helper

without an application-owned instance belongs elsewhere; do not add an empty

initializer merely to keep its directory.

Read .dependency-cruiser.cjs and .oxlintrc.json on the actual base. The

service-dependencies rule covers every implementation file in the directory,

not just the main class. Only the top-level wiring index.ts is exempt in lint.

pnpm gx services audit also exempts a legacy index.js root, so its output

omits that root's imports.

Implementations must not retrieve collaborators through their own root.

Wire application instances, models, logging, DomainEvents and configured

adapters in the root. The guide's dependency boundaries cover fs, lodash,

deferred lookups and live configuration reads.

Keep approved libraries imported where used and use explicit type-only imports.

Type injected logging as GhostLogger (or a Pick of it) from

@tryghost/logging, never a hand-written shape; class tests inject

createLoggingFake() from ghost/test/utils/logging-utils rather than stubbing

the global logger.

Do not inject stateless utilities to evade the policy or duplicate shared helpers.

Inspect the source before adding a library allowance. Justify why the dependency

is a library rather than an application-owned instance, and allow only its exact

entry or file. Include the reason and a boundary regression in the migration PR

that needs the allowance. Write out each file the allowance admits in full, as

dependency-cruiser resolves it from source and from built output, in a boundary

regression; gx services verify looks for the literal path, not one built from

variables. Assert each rejected target as well as its rule and source. Keep

allowances out of standalone skill/tooling PRs.

Seek review when that distinction is unclear or a broader policy change is

needed; do not widen the rule merely to make lint pass. Deleting the service's

legacy entry still belongs in each migration.

Preserve late acquisition with getters/factories when dependencies are unavailable

at boot. Inspect pnpm gx services report <name> and the full

pnpm gx services audit output: a summary may omit unresolved/own-root imports.

3. Establish coverage before changing production

Map each proposed change to an existing behavioral assertion. Run the relevant

unchanged root/implementation tests and real consumer checks on the base.

Use the testing guide's focused Nx commands: they prepare workspace builds and

Core assets before running the selected files. Direct Vitest or test:single

commands skip those prerequisites; pnpm bootstrap alone does not supply them.

Treat missing package outputs or card manifests as setup failures. Do not compile

Core merely to supply workspace outputs: adjacent emitted JavaScript can shadow

TypeScript edits in CommonJS consumers.

Initialization complexity, an audited inventory entry or an old green result

is not proof of coverage. If a test reloads CommonJS modules, restore the loaded

consumer chain too: cached consumers can retain a discarded root or collaborator.

Run those suites with their neighboring consumers in one worker to check isolation.

Cover the real risk: retained consumers, configuration timing, transaction or

cache behavior, boot ordering and resource cleanup. Use existing cold-boot

HTTP/job coverage where it already proves readiness. Do not copy a boot harness

for every service or duplicate generic kernel concurrency/retry tests.

For async ordering tests, verify both a missing required await and a harmless

extra await: the first must fail and the second must pass. Avoid checks that

count microtask turns; follow the testing guide's async-ordering advice.

If a needed assertion is absent, prepare passing characterization against the

old implementation in a tests-only PR and merge it first. Keep one commit per

service in preparatory batches. If a test exposes a production bug, isolate the

fix ahead of initializer adoption; do not change expectations to hide it.

Update each prepared service's inventory evidence in that tests-only change,

citing the new tests and narrowing only the blockers they cover. Preserve

historical audit revisions; do not defer this evidence to initializer adoption.

Substantial dependency refactors, fixture rewrites and business changes are also

preparation work, separate from the minimal initializer PR. Preserve the old

public API during preparation. Small dependency wiring can accompany migration

when existing tests protect it.

4. Make the smallest initializer change

from the kernel. Return one fully ready instance from its factory, or one

typed object when several objects are built together.

contracts in implementation files; root named exports are type-only.

helper's purpose, callers, timing, dependencies, failure policy and destination.

Extras are not automatic lifecycle hooks or a substitute for readiness.

synchronous factory. Initialize before eager consumers read .service.

Importing a root early is fine; evaluating its guarded getter early is not.

boundaries; do not convert boot wholesale or add a wrapper solely for it.

boundary and preserve synchronous throws versus promise rejections where

callers depend on that distinction.

their established times. Do not make the first request initialize the service.

dispose borrowed shared resources or change normal shutdown ownership.

If the service requires new kernel features, a restart/replacement policy, an API

redesign or broad boot orchestration, record the concrete mismatch and separate

that decision from the small migration. Do not silently expand its scope.

Migration tests may receive mechanical import/type/stub and initializer-await

adaptations. New assertions about behavior the old implementation already has,

and substantial harness changes, return to preparation. The migration commit

adds new cases only for what cannot be tested before the root exists:

init() before it starts theirs.

consumer loaded before init() reads the instance on use.

Each must fail under a change you can name. Never weaken assertions,

coverage gates, lint or types to make the refactor pass.

5. Update the inventory and verify

A migrated service is no longer legacy:

history keeps the audit record.

service that is not legacy cannot have any.

the inventory's followUps section. A suggested future home, and

notes on what defineService does not do for the service yet, go there too.

That entry holds nothing else; leave it out when nothing is open.

section, so deleting the entry does not lose it.

legacy entries mechanically, without changing their unrelated assessments.

An audit SHA you add must be a commit on main: a branch commit disappears on

squash merge.

There is no lint list to update. Deleting the entry applies

ghost/service-root-shape to the root and full boundary enforcement to the

implementation. It also applies ghost/boot-awaits-service-init and the

standard services boot test: boot must bind the root with .default and await

its init(), as

creating a service

shows. Never add a legacy entry or defer a service's dependency

checks: CI rejects both. Do not claim enforcement passed if that policy was

only checked in a temporary integration or is still awaiting merge.

Run focused tests before/after, relevant real consumer/DB checks and the default

pnpm check, then pnpm gx services verify <name> on the committed result. Repeat

gx services verify after every rebase or fix: hooks do not run during a rebase.

Before a PR is opened or marked ready, an agent that did not write the change runs

the verify-service-change skill. Run one full

local validation at a time. Investigate failures from fresh output; a passing

retry or an unchanged file alone does not establish that a failure belongs to

main. Keep unrelated fixes out of the migration.

Review the final diff: one service, unchanged operation behavior, no hidden

composition, and only justified metadata/enforcement changes. New test cases

are limited to those step 4 allows.

Use the repository commit skill and make one commit for this service.

Keep unrelated tooling/skill changes on their own branch. Report the branch,

commit, validation and unresolved prerequisites. Push/open a PR only within the

user's authorization, rebase on fetched main before opening, and report current

CI/review state accurately. Do not merge or enable auto-merge implicitly.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.