domain-refactor
Refactor a backend domain to follow the 3-layer architecture pattern (Controller → Service → Entity). Use when asked to refactor a domain, add an entity layer, fix architecture violations, or align a domain with backend-architecture.md. Triggers on phrases like "refactor domain", "add entity", "fix architecture", "align with pattern", or "domain doesn't have an entity".
What this skill does
# Domain Architecture Refactor
Refactor a backend domain to follow the 3-layer pattern documented in `docs/backend-architecture.md`.
**Announce at start:** "I'm using the domain-refactor skill to audit and refactor this domain."
## The Process
This is an interactive, incremental refactor. Never batch large changes without human review.
### Step 1: Identify the Domain
If the user hasn't specified a domain, ask which one. Domains live in `apps/backend/src/api/<domain>/`.
### Step 2: Audit
Read all files in the domain directory. Also read `docs/backend-architecture.md` for the reference patterns.
Produce an audit report listing every violation found, organized by category:
**Missing entity layer**
- Does the domain have an entity file? If not, flag it.
- Are there DB queries (`database.select`, `database.insert`, `database.update`, `database.delete`, `database.query`) directly in the service? List each one.
**Business logic in controllers**
- Controllers branching on domain state (checking fields, conditional logic beyond input shape validation)
- Controllers computing error messages based on domain data
- Controllers calling entity methods directly
**Business logic in services that should be entity predicates**
- Services checking domain state inline (`if (thing.status === 'x')`) instead of calling an entity predicate
- Services resolving internal IDs independently instead of getting them from predicates
**Event emission in services**
- `eventBridge.emit()` or `eventBridge.emitMany()` calls in the service that are paired with a single entity data operation
**Side effects in wrong layers**
- Notifications or emails triggered from entities or controllers
Present the audit and ask: **"Here's what I found. Which categories should we address? Want to tackle all of them, or focus on specific ones?"**
### Step 3: Plan Entity Creation (if needed)
If the domain needs a new entity file, present what it should contain:
1. **Predicates to extract** — List each business rule check in the service that should become a `can*` or `is*` predicate. Show the current service code and proposed entity signature.
2. **Data access to move** — List each DB query in the service that should move to the entity. Group by read vs write operations.
3. **Event emissions to move** — List each `eventBridge` call that should move into the entity alongside its paired data operation.
Ask: **"Does this look right for the new entity? Anything to add or remove?"**
### Step 4: Implement Incrementally
Work through changes one category at a time. After each category:
1. Make the changes
2. Show a brief summary of what moved where
3. Ask: **"How does this look? Ready for the next category?"**
**Order of operations:**
1. Create the entity file (empty class with singleton export)
2. Move data access methods (reads first, then writes)
3. Extract predicates from service inline checks
4. Move event emissions into entity methods
5. Update service to call entity methods
6. Clean up controller if needed
### Step 5: Verify
After all changes:
- Run `yarn tsc:all` to confirm compilation
- Run domain tests if they exist
- Present final summary of the refactor
## Audit Checklist
Use this to check each violation type:
**Controller layer:**
- [ ] Only extracts session, validates input shape, calls assertions, calls service, returns response
- [ ] No business logic or domain state checks
- [ ] No direct entity calls
- [ ] No DB access
**Service layer:**
- [ ] Orchestrates entity calls
- [ ] Enforces rules by calling entity predicates then throwing
- [ ] Owns transactions (`database.transaction(...)`)
- [ ] Triggers side effects (notifications, emails) after transactions
- [ ] Does NOT resolve internal IDs independently (gets them from predicate returns)
- [ ] Does NOT contain raw DB queries (these belong in the entity)
- [ ] Does NOT define business rules inline
**Entity layer:**
- [ ] Defines business rules as predicates (`can*` → `{ allowed, reason }`, `is*` → `boolean`)
- [ ] Predicates never throw
- [ ] Async predicates return resolved context (e.g. `instanceId`) alongside `allowed`
- [ ] Owns all data access (CRUD, complex queries, joins)
- [ ] Emits domain events via `eventBridge` after data writes
- [ ] Returns `null` for not found (doesn't throw `NotFoundError`)
- [ ] Transforms DB rows to SDK types
- [ ] Private `resolve*` methods for internal ID resolution
## Patterns to Apply
**Sync predicate** (entity has the data already fetched by service):
```typescript
canConvert(deal: Pick<Deal, 'convertedAt' | 'applicationId'>): { allowed: boolean; reason?: string } {
if (deal.convertedAt) return { allowed: false, reason: 'Already converted' };
return { allowed: true };
}
```
**Async predicate with context** (entity resolves internal state):
```typescript
async canAddTodoItem(taskId: string): Promise<
{ allowed: true; instanceId: string } | { allowed: false; reason: string }
> {
const instance = await this.resolveInstance(taskId);
if (!instance) return { allowed: false, reason: 'No output found' };
return { allowed: true, instanceId: instance.instanceId };
}
```
**Service enforcement:**
```typescript
const check = await entity.canDoThing(id);
if (!check.allowed) throw new ValidationError(check.reason);
await entity.doThing(check.resolvedId, data);
```
**Entity event emission:**
```typescript
async createThing(data: CreateThingData) {
const [result] = await database.insert(things).values(data).returning();
await eventBridge.emit('thing.created', {
brandId: data.brandId,
data: { thingId: result.id },
});
return result;
}
```
## Key Principles
- **One category at a time** — Don't dump all changes at once
- **Ask before creating** — Always confirm the entity shape before writing it
- **Preserve behavior** — This is a refactor, not a feature change. Inputs and outputs stay the same.
- **Match existing style** — Look at `dealEntity.ts` and `taskOutputsEntity.ts` for reference
- **Don't over-extract** — Simple read-only passthroughs in the service are fine. Not every query needs a predicate.
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.