@telorun/lease 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ # SUSTAINABLE USE LICENSE (Fair-code)
2
+
3
+ Copyright (c) 2026 CodeNet Sp. z o.o.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, and distribute the Software for any purpose—including commercial purposes—subject to the following conditions:
6
+
7
+ 1. ANTI-COMPETITION RESTRICTION: The Software may not be provided to third parties as a managed service, commercial SaaS (Software-as-a-Service), PaaS (Platform-as-a-Service), BaaS (Backend-as-a-Service), or similar offering where the primary value provided to the user is the functionality of the Software itself, without a separate commercial license from the copyright holder.
8
+
9
+ 2. PERMITTED COMMERCIAL USE: You are free to use the Software to build, host, and monetize your own commercial applications, products, and services, provided such use does not violate Clause 1.
10
+
11
+ 3. ATTRIBUTION: This copyright notice and license must be included in all copies or substantial portions of the Software.
12
+
13
+ 4. CONTRIBUTIONS: Contributions to the Software are welcome and encouraged. By contributing, you agree that your contributions may be incorporated into the Software and distributed under this license.
14
+
15
+ 5. DISCLAIMER: The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
16
+
17
+ For commercial licensing, managed hosting exemptions, or enterprise inquiries, please contact <contact@codenet.pl>.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/telorun/telo/main/assets/telo.png" alt="Telo" width="200" />
3
+ </p>
4
+
5
+ <h1 align="center">Telo</h1>
6
+
7
+ <p align="center">Runtime for declarative backends.</p>
8
+
9
+ <p align="center">
10
+ <a href="https://github.com/telorun/telo/actions/workflows/test.yml"><img alt="Tests" src="https://github.com/telorun/telo/actions/workflows/test.yml/badge.svg" /></a>
11
+ <a href="https://www.npmjs.com/package/@telorun/cli"><img alt="node" src="https://img.shields.io/node/v/@telorun/cli" /></a>
12
+ <br />
13
+ <a href="https://github.com/telorun/telo/commits/main"><img alt="Last commit" src="https://img.shields.io/github/last-commit/telorun/telo" /></a>
14
+ <a href="https://github.com/telorun/telo/issues"><img alt="Issues" src="https://img.shields.io/github/issues/telorun/telo" /></a>
15
+ <a href="https://github.com/telorun/telo/pulls"><img alt="Pull requests" src="https://img.shields.io/github/issues-pr/telorun/telo" /></a>
16
+ <br />
17
+ <img alt="Changesets" src="https://img.shields.io/badge/maintained%20with-changesets-176de3" />
18
+ </p>
19
+
20
+ Telo is an execution engine (Micro-Kernel) that runs logic defined entirely in YAML manifests. Instead of writing imperative backend code, you define your routes, databases, schemas, and AI workflows as atomic, interconnected YAML documents. Telo takes those manifests and runs them.
21
+
22
+ Built to be language-agnostic and infinitely extensible.
23
+
24
+ ```bash
25
+ # Reconcile your manifest into a running backend
26
+ $ telo ./examples/hello-api
27
+
28
+ {"level":30,"time":1771610393008,"pid":1310178,"hostname":"dev","msg":"Server listening at http://127.0.0.1:8844"}
29
+ ```
30
+
31
+ ## Why use Telo?
32
+
33
+ - **Open Standards:** Built on YAML, JSON Schema, and CEL — no proprietary DSL.
34
+ - **Static Analysis:** CEL type checking, reference validation, and IDE diagnostics catch errors before runtime.
35
+ - **Micro-Kernel Architecture:** Telo itself knows nothing about HTTP or SQL. Everything is a module you import, scope, and compose with typed variable and secret contracts.
36
+ - **Language Agnostic:** Available as a Node.js runtime today, with a shared YAML runtime contract that allows for future Rust or Go implementations without changing your manifests.
37
+
38
+ ## What It Does
39
+
40
+ - **Loads** YAML resources and compiles CEL expressions (`${{ }}`) into an in-memory registry.
41
+ - **Resolves** resource dependencies via a multi-pass init loop, handling ordering automatically.
42
+ - **Indexes** resources by Kind and Name for constant-time lookup.
43
+ - **Dispatches** execution to the controller that owns each Kind.
44
+
45
+ ## Example manifest
46
+
47
+ See [examples/](./examples/) for a list of working applications.
48
+
49
+ ## Status
50
+
51
+ Telo is under **active development**. The core runtime, module system, and standard library are functional, but the API surface — including YAML shapes — may change without notice. Not yet recommended for production use.
52
+
53
+ ## The Meaning of Telo
54
+
55
+ The name Telo is derived from the Greek root Telos - meaning the "end goal", "purpose", or "final state". That is exactly the philosophy behind this runtime. In standard imperative programming, you have to write thousands of lines of code to tell a server exactly how to start. With Telo, you simply declare your desired final state.
56
+
57
+ You define the end state. Telo makes it real.
58
+
59
+ ## Philosophy
60
+
61
+ Modern platforms often spend disproportionate effort on technical mechanics-wiring frameworks, managing infrastructure, and negotiating toolchains-while the original business problem gets delayed or diluted. Telo pushes in the opposite direction: it treats kernel execution as a stable, predictable host so teams can concentrate on the **business logic and outcomes** instead of the plumbing.
62
+
63
+ By separating "what the system should do" from "how it is hosted", the runtime reduces friction for domain‑level changes. Teams can move faster on product requirements, experiment more safely, and keep conversations centered on value delivered rather than implementation trivia.
64
+
65
+ Telo also aims to **join forces across all programming language communities**, so the best ideas, patterns, and implementations can converge into a shared kernel truth without forcing everyone into a single stack.
66
+
67
+ YAML also makes the system more **AI‑friendly** than traditional programming languages: it is explicit, structured, and easier for tools to generate, review, and transform without losing intent.
68
+
69
+ ## Modularity
70
+
71
+ Telo is built around **modules** that own specific resource kinds. A module is loaded from a manifest, declares which kinds it implements, and then receives only the resources of those kinds. This keeps concerns isolated and lets teams compose systems from focused building blocks rather than monolithic services.
72
+
73
+ At kernel execution time, execution is always routed by **Kind.Name**. The kernel resolves the Kind to its owning module and hands off execution. Modules can call back into the kernel to execute other resources, enabling composition without tight coupling.
74
+
75
+ ## Architecture
76
+
77
+ The architecture is inspired by Kubernetes-style manifests: declarative resources, explicit kinds, and a control plane that routes work based on those definitions.
78
+ Those manifests were taken to the next level by allowing them to run inside a standalone runtime host.
79
+
80
+ ## See more at
81
+
82
+ - [Telo Kernel](./kernel/README.md)
83
+ - [Telo SDK for module authors](sdk/README.md)
84
+ - [Modules](modules/README.md)
85
+
86
+ ## License
87
+
88
+ See [LICENSE](https://github.com/telorun/telo/blob/main/LICENSE).
89
+
90
+ ## Contribution Note
91
+
92
+ By contributing, you agree that code and examples in this repository may be translated or re‑implemented in other programming languages (including by AI systems) to support the project’s polyglot goals.
@@ -0,0 +1,45 @@
1
+ import { type ControllerContext, type InvokeContext, type ResourceContext, type ResourceInstance } from "@telorun/sdk";
2
+ import { type CacheStore } from "@telorun/cache";
3
+ interface CriticalResource {
4
+ metadata: {
5
+ name: string;
6
+ module?: string;
7
+ };
8
+ store?: CacheStore | {
9
+ name: string;
10
+ alias?: string;
11
+ };
12
+ ttl: string;
13
+ detach?: boolean;
14
+ invoke?: unknown;
15
+ }
16
+ interface CriticalInputs {
17
+ key: string;
18
+ holder?: string;
19
+ inputs?: Record<string, unknown>;
20
+ }
21
+ interface CriticalResult {
22
+ acquired: boolean;
23
+ holder?: unknown;
24
+ result?: unknown;
25
+ }
26
+ /**
27
+ * Lease.Critical — a declarative critical section. Acquires a keyed lease, runs
28
+ * the wrapped `invoke` body under it, and releases automatically; the caller
29
+ * never issues acquire/release. Synchronous mode releases when the body returns
30
+ * (or throws); `detach` mode dispatches the body detached, holds the lease
31
+ * across it, and releases on its terminal — so a lease can span a background
32
+ * operation that outlives the call while the acquire outcome is still returned
33
+ * synchronously (for a 200-vs-409 branch).
34
+ */
35
+ declare class LeaseCritical implements ResourceInstance<CriticalInputs, CriticalResult> {
36
+ private readonly resource;
37
+ private readonly ctx;
38
+ private readonly ttlMs;
39
+ constructor(resource: CriticalResource, ctx: ResourceContext);
40
+ invoke(inputs: CriticalInputs, _ctx?: InvokeContext): Promise<CriticalResult>;
41
+ snapshot(): Record<string, unknown>;
42
+ }
43
+ export declare function register(_ctx: ControllerContext): void;
44
+ export declare function create(resource: CriticalResource, ctx: ResourceContext): Promise<LeaseCritical>;
45
+ export {};
@@ -0,0 +1,67 @@
1
+ import { parseDurationMs, resolveInvocableDispatcher, } from "@telorun/sdk";
2
+ import { resolveCacheStore } from "@telorun/cache";
3
+ import { randomUUID } from "node:crypto";
4
+ import { Mutex } from "./mutex.js";
5
+ /**
6
+ * Lease.Critical — a declarative critical section. Acquires a keyed lease, runs
7
+ * the wrapped `invoke` body under it, and releases automatically; the caller
8
+ * never issues acquire/release. Synchronous mode releases when the body returns
9
+ * (or throws); `detach` mode dispatches the body detached, holds the lease
10
+ * across it, and releases on its terminal — so a lease can span a background
11
+ * operation that outlives the call while the acquire outcome is still returned
12
+ * synchronously (for a 200-vs-409 branch).
13
+ */
14
+ class LeaseCritical {
15
+ resource;
16
+ ctx;
17
+ ttlMs;
18
+ constructor(resource, ctx) {
19
+ this.resource = resource;
20
+ this.ctx = ctx;
21
+ this.ttlMs = parseDurationMs(resource.ttl);
22
+ }
23
+ async invoke(inputs, _ctx) {
24
+ const name = this.resource.metadata.name;
25
+ if (!inputs || typeof inputs.key !== "string" || inputs.key.length === 0) {
26
+ // Fail closed: an empty key must not collapse every caller into one lease.
27
+ return { acquired: false };
28
+ }
29
+ const store = resolveCacheStore(this.resource.store, this.ctx);
30
+ const mutex = new Mutex(store, name, this.ttlMs);
31
+ // The holder token is both the 409 payload and the release guard — unique
32
+ // per acquisition so a stale holder can't free another owner's lease.
33
+ const holder = inputs.holder ?? randomUUID();
34
+ const acq = await mutex.acquire(inputs.key, holder);
35
+ if (!acq.acquired)
36
+ return { acquired: false, holder: acq.holder ?? null };
37
+ const dispatch = resolveInvocableDispatcher(this.resource.invoke, this.ctx, () => `Lease.Critical "${name}"`);
38
+ const bodyInputs = inputs.inputs ?? {};
39
+ if (this.resource.detach) {
40
+ // Hold the lease across a detached body — the lease resource owns the
41
+ // detach so the hold outlives this call; release on the body's terminal.
42
+ this.ctx.runDetached(async () => {
43
+ try {
44
+ await dispatch(bodyInputs);
45
+ }
46
+ finally {
47
+ await mutex.release(inputs.key, holder);
48
+ }
49
+ });
50
+ return { acquired: true, holder: null };
51
+ }
52
+ try {
53
+ const result = await dispatch(bodyInputs);
54
+ return { acquired: true, holder: null, result };
55
+ }
56
+ finally {
57
+ await mutex.release(inputs.key, holder);
58
+ }
59
+ }
60
+ snapshot() {
61
+ return {};
62
+ }
63
+ }
64
+ export function register(_ctx) { }
65
+ export async function create(resource, ctx) {
66
+ return new LeaseCritical(resource, ctx);
67
+ }
@@ -0,0 +1,2 @@
1
+ export { Mutex } from "./mutex.js";
2
+ export type { AcquireResult } from "./mutex.js";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { Mutex } from "./mutex.js";
@@ -0,0 +1,25 @@
1
+ import type { CacheStore } from "@telorun/cache";
2
+ export interface AcquireResult {
3
+ acquired: boolean;
4
+ /** The current holder's token when not acquired (undefined if unknown). */
5
+ holder?: unknown;
6
+ }
7
+ /**
8
+ * A race-free, self-healing keyed mutex over a Cache.Store. The atomic gate is
9
+ * `increment`: exactly one caller can bring the counter from 0 to 1, so exactly
10
+ * one wins; losers undo their increment and read back the holder. The counter's
11
+ * `ttlMs` expiry makes the lease self-healing — if a holder dies without
12
+ * releasing, the lease frees on expiry. Release is holder-matched, so a stale
13
+ * holder whose lease already expired (and was re-acquired by another) can't free
14
+ * the new owner's lease.
15
+ */
16
+ export declare class Mutex {
17
+ private readonly store;
18
+ private readonly name;
19
+ private readonly ttlMs;
20
+ constructor(store: CacheStore, name: string, ttlMs: number);
21
+ private counterKey;
22
+ private holderKey;
23
+ acquire(key: string, holder: unknown): Promise<AcquireResult>;
24
+ release(key: string, holder: unknown): Promise<void>;
25
+ }
package/dist/mutex.js ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * A race-free, self-healing keyed mutex over a Cache.Store. The atomic gate is
3
+ * `increment`: exactly one caller can bring the counter from 0 to 1, so exactly
4
+ * one wins; losers undo their increment and read back the holder. The counter's
5
+ * `ttlMs` expiry makes the lease self-healing — if a holder dies without
6
+ * releasing, the lease frees on expiry. Release is holder-matched, so a stale
7
+ * holder whose lease already expired (and was re-acquired by another) can't free
8
+ * the new owner's lease.
9
+ */
10
+ export class Mutex {
11
+ store;
12
+ name;
13
+ ttlMs;
14
+ constructor(store, name, ttlMs) {
15
+ this.store = store;
16
+ this.name = name;
17
+ this.ttlMs = ttlMs;
18
+ }
19
+ counterKey(key) {
20
+ return `lease:${this.name}:${key}`;
21
+ }
22
+ holderKey(key) {
23
+ return `lease:${this.name}:${key}:holder`;
24
+ }
25
+ async acquire(key, holder) {
26
+ const n = await this.store.increment(this.counterKey(key), 1, this.ttlMs);
27
+ if (n === 1) {
28
+ await this.store.set(this.holderKey(key), holder, this.ttlMs, 0);
29
+ return { acquired: true };
30
+ }
31
+ // Someone holds it — undo our over-count and report the current holder.
32
+ await this.store.increment(this.counterKey(key), -1, this.ttlMs);
33
+ const cur = await this.store.get(this.holderKey(key));
34
+ return { acquired: false, holder: cur.state === "miss" ? undefined : cur.value };
35
+ }
36
+ async release(key, holder) {
37
+ const cur = await this.store.get(this.holderKey(key));
38
+ // Only release a lease we still hold. A miss means it already expired —
39
+ // decrementing would seed a stray -1 counter and wedge the key until TTL.
40
+ // A mismatch means another owner took it over on our expiry.
41
+ if (cur.state === "miss" || cur.value !== holder)
42
+ return;
43
+ await this.store.increment(this.counterKey(key), -1, this.ttlMs);
44
+ await this.store.delete(this.holderKey(key));
45
+ }
46
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@telorun/lease",
3
+ "version": "0.2.0",
4
+ "description": "Telo lease module — Lease.Critical, a declarative critical section over a Cache.Store.",
5
+ "keywords": [
6
+ "telo",
7
+ "lease",
8
+ "lock",
9
+ "mutex",
10
+ "critical-section"
11
+ ],
12
+ "author": "Bartosz Pasiński <bartosz.pasinski@codenet.pl>",
13
+ "license": "SEE LICENSE IN LICENSE",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/telorun/telo.git",
17
+ "directory": "modules/lease/nodejs"
18
+ },
19
+ "homepage": "https://github.com/telorun/telo#readme",
20
+ "bugs": {
21
+ "url": "https://github.com/telorun/telo/issues"
22
+ },
23
+ "type": "module",
24
+ "main": "./dist/index.js",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "bun": "./src/index.ts",
31
+ "import": "./dist/index.js"
32
+ },
33
+ "./critical": {
34
+ "types": "./dist/critical-controller.d.ts",
35
+ "bun": "./src/critical-controller.ts",
36
+ "import": "./dist/critical-controller.js"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist/**",
41
+ "src/**"
42
+ ],
43
+ "dependencies": {
44
+ "@telorun/cache": "0.3.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^20.0.0",
48
+ "typescript": "^5.0.0",
49
+ "@telorun/sdk": "0.38.0"
50
+ },
51
+ "peerDependencies": {
52
+ "@telorun/sdk": "*"
53
+ },
54
+ "scripts": {
55
+ "build": "tsc -p tsconfig.lib.json"
56
+ }
57
+ }
@@ -0,0 +1,107 @@
1
+ import {
2
+ type ControllerContext,
3
+ type InvokeContext,
4
+ type ResourceContext,
5
+ type ResourceInstance,
6
+ parseDurationMs,
7
+ resolveInvocableDispatcher,
8
+ } from "@telorun/sdk";
9
+ import { type CacheStore, resolveCacheStore } from "@telorun/cache";
10
+ import { randomUUID } from "node:crypto";
11
+ import { Mutex } from "./mutex.js";
12
+
13
+ interface CriticalResource {
14
+ metadata: { name: string; module?: string };
15
+ store?: CacheStore | { name: string; alias?: string };
16
+ ttl: string;
17
+ detach?: boolean;
18
+ invoke?: unknown;
19
+ }
20
+
21
+ interface CriticalInputs {
22
+ key: string;
23
+ holder?: string;
24
+ inputs?: Record<string, unknown>;
25
+ }
26
+
27
+ interface CriticalResult {
28
+ acquired: boolean;
29
+ holder?: unknown;
30
+ result?: unknown;
31
+ }
32
+
33
+ /**
34
+ * Lease.Critical — a declarative critical section. Acquires a keyed lease, runs
35
+ * the wrapped `invoke` body under it, and releases automatically; the caller
36
+ * never issues acquire/release. Synchronous mode releases when the body returns
37
+ * (or throws); `detach` mode dispatches the body detached, holds the lease
38
+ * across it, and releases on its terminal — so a lease can span a background
39
+ * operation that outlives the call while the acquire outcome is still returned
40
+ * synchronously (for a 200-vs-409 branch).
41
+ */
42
+ class LeaseCritical implements ResourceInstance<CriticalInputs, CriticalResult> {
43
+ private readonly ttlMs: number;
44
+
45
+ constructor(
46
+ private readonly resource: CriticalResource,
47
+ private readonly ctx: ResourceContext,
48
+ ) {
49
+ this.ttlMs = parseDurationMs(resource.ttl);
50
+ }
51
+
52
+ async invoke(inputs: CriticalInputs, _ctx?: InvokeContext): Promise<CriticalResult> {
53
+ const name = this.resource.metadata.name;
54
+ if (!inputs || typeof inputs.key !== "string" || inputs.key.length === 0) {
55
+ // Fail closed: an empty key must not collapse every caller into one lease.
56
+ return { acquired: false };
57
+ }
58
+ const store = resolveCacheStore(this.resource.store, this.ctx);
59
+ const mutex = new Mutex(store, name, this.ttlMs);
60
+ // The holder token is both the 409 payload and the release guard — unique
61
+ // per acquisition so a stale holder can't free another owner's lease.
62
+ const holder = inputs.holder ?? randomUUID();
63
+
64
+ const acq = await mutex.acquire(inputs.key, holder);
65
+ if (!acq.acquired) return { acquired: false, holder: acq.holder ?? null };
66
+
67
+ const dispatch = resolveInvocableDispatcher(
68
+ this.resource.invoke,
69
+ this.ctx,
70
+ () => `Lease.Critical "${name}"`,
71
+ );
72
+ const bodyInputs = inputs.inputs ?? {};
73
+
74
+ if (this.resource.detach) {
75
+ // Hold the lease across a detached body — the lease resource owns the
76
+ // detach so the hold outlives this call; release on the body's terminal.
77
+ this.ctx.runDetached(async () => {
78
+ try {
79
+ await dispatch(bodyInputs);
80
+ } finally {
81
+ await mutex.release(inputs.key, holder);
82
+ }
83
+ });
84
+ return { acquired: true, holder: null };
85
+ }
86
+
87
+ try {
88
+ const result = await dispatch(bodyInputs);
89
+ return { acquired: true, holder: null, result };
90
+ } finally {
91
+ await mutex.release(inputs.key, holder);
92
+ }
93
+ }
94
+
95
+ snapshot(): Record<string, unknown> {
96
+ return {};
97
+ }
98
+ }
99
+
100
+ export function register(_ctx: ControllerContext): void {}
101
+
102
+ export async function create(
103
+ resource: CriticalResource,
104
+ ctx: ResourceContext,
105
+ ): Promise<LeaseCritical> {
106
+ return new LeaseCritical(resource, ctx);
107
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { Mutex } from "./mutex.js";
2
+ export type { AcquireResult } from "./mutex.js";
package/src/mutex.ts ADDED
@@ -0,0 +1,54 @@
1
+ import type { CacheStore } from "@telorun/cache";
2
+
3
+ export interface AcquireResult {
4
+ acquired: boolean;
5
+ /** The current holder's token when not acquired (undefined if unknown). */
6
+ holder?: unknown;
7
+ }
8
+
9
+ /**
10
+ * A race-free, self-healing keyed mutex over a Cache.Store. The atomic gate is
11
+ * `increment`: exactly one caller can bring the counter from 0 to 1, so exactly
12
+ * one wins; losers undo their increment and read back the holder. The counter's
13
+ * `ttlMs` expiry makes the lease self-healing — if a holder dies without
14
+ * releasing, the lease frees on expiry. Release is holder-matched, so a stale
15
+ * holder whose lease already expired (and was re-acquired by another) can't free
16
+ * the new owner's lease.
17
+ */
18
+ export class Mutex {
19
+ constructor(
20
+ private readonly store: CacheStore,
21
+ private readonly name: string,
22
+ private readonly ttlMs: number,
23
+ ) {}
24
+
25
+ private counterKey(key: string): string {
26
+ return `lease:${this.name}:${key}`;
27
+ }
28
+
29
+ private holderKey(key: string): string {
30
+ return `lease:${this.name}:${key}:holder`;
31
+ }
32
+
33
+ async acquire(key: string, holder: unknown): Promise<AcquireResult> {
34
+ const n = await this.store.increment(this.counterKey(key), 1, this.ttlMs);
35
+ if (n === 1) {
36
+ await this.store.set(this.holderKey(key), holder, this.ttlMs, 0);
37
+ return { acquired: true };
38
+ }
39
+ // Someone holds it — undo our over-count and report the current holder.
40
+ await this.store.increment(this.counterKey(key), -1, this.ttlMs);
41
+ const cur = await this.store.get(this.holderKey(key));
42
+ return { acquired: false, holder: cur.state === "miss" ? undefined : cur.value };
43
+ }
44
+
45
+ async release(key: string, holder: unknown): Promise<void> {
46
+ const cur = await this.store.get(this.holderKey(key));
47
+ // Only release a lease we still hold. A miss means it already expired —
48
+ // decrementing would seed a stray -1 counter and wedge the key until TTL.
49
+ // A mismatch means another owner took it over on our expiry.
50
+ if (cur.state === "miss" || cur.value !== holder) return;
51
+ await this.store.increment(this.counterKey(key), -1, this.ttlMs);
52
+ await this.store.delete(this.holderKey(key));
53
+ }
54
+ }