create-a-service-layer-from-a-managed-resource

create-a-service-layer-from-a-managed-resource is a cursor rule for Cursor from PaulJPhilp/EffectPatterns. It costs 589 tokens per session, scanned A, original, MIT.

A TypeScript pattern for exposing a managed resource, such as a database connection pool, as an application service. The resource is acquired and released with the service lifetime.

In plain words
What is it for?
Use it to provide database or other scoped resources to Effect programs with automatic cleanup.
Why use it?
It prevents connection pools and similar resources from staying open after the work using them has ended.

Cursor rule for Cursor

Written for Cursor: a Cursor rule (.mdc).

Good fit Use it to provide database or other scoped resources to Effect programs with automatic cleanup.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/pauljphilp/effectpatterns/create-a-service-layer-from-a-managed-resource
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/PaulJPhilp/EffectPatterns

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for create-a-service-layer-from-a-managed-resource

README.md
[![agentmods](https://agentmods.dev/badge/rules/pauljphilp/effectpatterns/create-a-service-layer-from-a-managed-resource.svg)](https://agentmods.dev/rules/pauljphilp/effectpatterns/create-a-service-layer-from-a-managed-resource)
Your own site
<a href="https://agentmods.dev/rules/pauljphilp/effectpatterns/create-a-service-layer-from-a-managed-resource"><img src="https://agentmods.dev/badge/rules/pauljphilp/effectpatterns/create-a-service-layer-from-a-managed-resource.svg" alt="Measured on agentmods" height="20"></a>
Per session 589 This file is loaded in full into every session.
When invoked 589 The same file — it is already loaded in full.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00589 $0.00589
Opus 5 $0.00295 $0.00295
Sonnet 5 $0.00118 $0.00118
Haiku 4.5 $0.00059 $0.00059

Measured 4d ago against content hash 8f65b8c62cde, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-07, from the pricing page.

Security

Grade A, and why

create-a-service-layer-from-a-managed-resource scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

content/published/rules/cursor/create-a-service-layer-from-a-managed-resource.mdc · 62 lines

How it starts

The opening of the file, as written. The whole thing — 62 lines — stays where its author put it; the contents beside it link to each section on GitHub.

description: Provide a managed resource to the application context using Layer.scoped. globs: "**/*.ts" alwaysApply: true

Create a Service Layer from a Managed Resource

Rule: Provide a managed resource to the application context using Layer.scoped.

Example

import { Effect, Console } from "effect";

// 1. Define the service interface
interface DatabaseService {
  readonly query: (sql: string) => Effect.Effect<string[], never, never>;
}

// 2. Define the service implementation with scoped resource management
class Database extends Effect.Service<DatabaseService>()("Database", {
  // The scoped property manages the resource lifecycle
  scoped: Effect.gen(function* () {
    const id = Math.floor(Math.random() * 1000);

    // Acquire the connection
    yield* Effect.log(`[Pool ${id}] Acquired`);

    // Setup cleanup to run when scope closes
    yield* Effect.addFinalizer(() => Effect.log(`[Pool ${id}] Released`));

    // Return the service implementation
    return {
      query: (sql: string) =>
        Effect.sync(() => [`Result for '${sql}' from pool ${id}`]),
    };
  }),
}) {}

// 3. Use the service in your program
const program = Effect.gen(function* () {
  const db = yield* Database;
  const users = yield* db.query("SELECT * FROM users");
  yield* Effect.log(`Query successful: ${users[0]}`);
});

// 4. Run the program with scoped resource management
Effect.runPromise(
  Effect.scoped(program).pipe(Effect.provide(Database.Default))
);

/*
Output:
[Pool 458] Acquired
Query successful: Result for 'SELECT * FROM users' from pool 458
[Pool 458] Released
*/

Explanation: The Effect.Service helper creates the Database class, which acts as both the service definition and its context key (Tag). The Database.Live layer connects this service to a concrete, lifecycle-managed implementation. When program asks for the Database service, the Effect runtime uses the Live layer to run the acquire effect once, caches the resulting DbPool, and injects it. The release effect is automatically run when the program completes.

Read the full file on GitHub · 62 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 62 lines · 589 tokens per session scan A 8f65b8c62cde

Subscribe to this mod's changes

create-a-service-layer-from-a-managed-resource is a cursor rule published in the GitHub repository PaulJPhilp/EffectPatterns (796 stars, last pushed 2mo ago), licensed MIT. It adds 589 tokens to every session, about $0.0029 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.