mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-02 14:31:01 +02:00
Use scoped Effect producer runtime
This commit is contained in:
parent
89ef3dbbbf
commit
18b27aeba7
3 changed files with 175 additions and 38 deletions
|
|
@ -12,8 +12,8 @@ Verified source roots:
|
||||||
- Effect v4 subtree: `/home/elpresidank/YeeBois/projects/beep-effect2/.repos/effect-v4`
|
- Effect v4 subtree: `/home/elpresidank/YeeBois/projects/beep-effect2/.repos/effect-v4`
|
||||||
- Installed Effect beta used by this workspace: `ts/node_modules/effect`
|
- Installed Effect beta used by this workspace: `ts/node_modules/effect`
|
||||||
|
|
||||||
Current signal counts from `ts/packages` after the 2026-06-02 gateway dispatcher
|
Current signal counts from `ts/packages` after the 2026-06-02 base producer
|
||||||
ownership and serialization slice:
|
scoped runtime slice:
|
||||||
|
|
||||||
| Signal | Count |
|
| Signal | Count |
|
||||||
| --- | ---: |
|
| --- | ---: |
|
||||||
|
|
@ -21,8 +21,8 @@ ownership and serialization slice:
|
||||||
| `Effect.runPromiseWith` | 0 |
|
| `Effect.runPromiseWith` | 0 |
|
||||||
| `Effect.cached` | 0 |
|
| `Effect.cached` | 0 |
|
||||||
| `Layer.succeed` | 12 |
|
| `Layer.succeed` | 12 |
|
||||||
| `Map<` | 88 |
|
| `Map<` | 82 |
|
||||||
| `WebSocket` | 74 |
|
| `WebSocket` | 64 |
|
||||||
| `new Map` | 60 |
|
| `new Map` | 60 |
|
||||||
| `toPromiseRequestor` | 0 |
|
| `toPromiseRequestor` | 0 |
|
||||||
| `makeAsyncProcessor` | 19 |
|
| `makeAsyncProcessor` | 19 |
|
||||||
|
|
@ -96,6 +96,11 @@ Notes:
|
||||||
`PubSub` is an in-process hub and does not replace the broker-backed
|
`PubSub` is an in-process hub and does not replace the broker-backed
|
||||||
`PubSubBackend`/NATS boundary, but it should be preferred for future
|
`PubSubBackend`/NATS boundary, but it should be preferred for future
|
||||||
in-process broadcast/fanout needs.
|
in-process broadcast/fanout needs.
|
||||||
|
- The base producer scoped runtime slice moved the legacy `makeProducer`
|
||||||
|
Promise facade onto the existing `makeEffectProducerFromPubSub` scoped
|
||||||
|
factory. Public `start`/`send`/`stop` remain Promise compatibility
|
||||||
|
boundaries, while producer allocation, flush, and finalizer close now go
|
||||||
|
through the Effect runtime path.
|
||||||
- The gateway streaming callback slice added Effect-returning dispatcher
|
- The gateway streaming callback slice added Effect-returning dispatcher
|
||||||
streaming methods, switched the RPC stream server off nested
|
streaming methods, switched the RPC stream server off nested
|
||||||
`Effect.runPromiseWith(context)` queue offers, and replaced the client
|
`Effect.runPromiseWith(context)` queue offers, and replaced the client
|
||||||
|
|
@ -1128,6 +1133,29 @@ Notes:
|
||||||
- `cd ts && bun run test`
|
- `cd ts && bun run test`
|
||||||
- `git diff --check`
|
- `git diff --check`
|
||||||
|
|
||||||
|
### 2026-06-02: Base Producer Scoped Runtime Slice
|
||||||
|
|
||||||
|
- Status: migrated and root-verified.
|
||||||
|
- Completed:
|
||||||
|
- Kept `PubSubBackend` as the broker adapter boundary; Effect native
|
||||||
|
`PubSub` remains an in-process primitive and is not a replacement for
|
||||||
|
broker-backed topics, subscriptions, acknowledgements, codecs, or backend
|
||||||
|
lifecycle.
|
||||||
|
- Reworked `makeProducer` so the legacy Promise facade allocates producers
|
||||||
|
through `makeEffectProducerFromPubSub` inside a closeable `Scope`.
|
||||||
|
- `stop()` now flushes the Effect producer and closes the scope with the
|
||||||
|
registered producer finalizer, including the flush-failure path.
|
||||||
|
- Added focused producer facade coverage for send routing, idempotent stop,
|
||||||
|
tagged not-started lifecycle errors, and close-on-flush-failure behavior.
|
||||||
|
- Verification:
|
||||||
|
- `cd ts && bun run check:tsgo`
|
||||||
|
- `bun run --cwd ts/packages/base build`
|
||||||
|
- `cd ts/packages/base && bunx --bun vitest run src/__tests__/producer.test.ts`
|
||||||
|
- `bun run --cwd ts/packages/base test`
|
||||||
|
- `cd ts && bun run check`
|
||||||
|
- `cd ts && bun run build`
|
||||||
|
- `cd ts && bun run test`
|
||||||
|
|
||||||
## Subagent Findings To Preserve
|
## Subagent Findings To Preserve
|
||||||
|
|
||||||
- MCP/workbench:
|
- MCP/workbench:
|
||||||
|
|
@ -1155,6 +1183,10 @@ Notes:
|
||||||
- The legacy `messaging/subscriber.ts` async queue/fanout implementation is
|
- The legacy `messaging/subscriber.ts` async queue/fanout implementation is
|
||||||
removed. Use native `effect/PubSub` for future in-process fanout, while
|
removed. Use native `effect/PubSub` for future in-process fanout, while
|
||||||
keeping `PubSubBackend` for broker-backed messaging.
|
keeping `PubSubBackend` for broker-backed messaging.
|
||||||
|
- The legacy producer facade now delegates to the scoped Effect producer
|
||||||
|
runtime. Remaining broker P0 work should focus on native backend/NATS
|
||||||
|
runtime shape and consumer polling, not replacing `PubSubBackend` with
|
||||||
|
`effect/PubSub`.
|
||||||
- Existing constructor shims preserve callable-plus-newable public exports;
|
- Existing constructor shims preserve callable-plus-newable public exports;
|
||||||
removing them needs a public API split or real class redesign.
|
removing them needs a public API split or real class redesign.
|
||||||
- Typed string registries in `Flow` now have Schema-backed parameter specs
|
- Typed string registries in `Flow` now have Schema-backed parameter specs
|
||||||
|
|
@ -1222,6 +1254,9 @@ Notes:
|
||||||
acquisition and stream/schedule-based consumer loops.
|
acquisition and stream/schedule-based consumer loops.
|
||||||
- Keep `PubSubBackend` as the compatibility adapter boundary; Effect native
|
- Keep `PubSubBackend` as the compatibility adapter boundary; Effect native
|
||||||
`PubSub` remains in-process only.
|
`PubSub` remains in-process only.
|
||||||
|
- Treat the producer Promise facade as a completed compatibility wrapper;
|
||||||
|
avoid reopening it unless backend runtime changes require a narrower
|
||||||
|
adapter.
|
||||||
- Tests:
|
- Tests:
|
||||||
- Fake backend ack/nak/backoff/stop tests, NATS close finalizer tests, and
|
- Fake backend ack/nak/backoff/stop tests, NATS close finalizer tests, and
|
||||||
config-push stream tests.
|
config-push stream tests.
|
||||||
|
|
|
||||||
86
ts/packages/base/src/__tests__/producer.test.ts
Normal file
86
ts/packages/base/src/__tests__/producer.test.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
makeProducer,
|
||||||
|
type BackendConsumer,
|
||||||
|
type BackendProducer,
|
||||||
|
type CreateConsumerOptions,
|
||||||
|
type CreateProducerOptions,
|
||||||
|
type PubSubBackend,
|
||||||
|
} from "../index.js";
|
||||||
|
|
||||||
|
class ProducerBackend implements PubSubBackend {
|
||||||
|
readonly sent: Array<{ readonly message: unknown; readonly properties?: Record<string, string> }> = [];
|
||||||
|
readonly producerTopics: Array<string> = [];
|
||||||
|
closeCount = 0;
|
||||||
|
flushCount = 0;
|
||||||
|
failFlush = false;
|
||||||
|
|
||||||
|
async createProducer<T>(options: CreateProducerOptions<T>): Promise<BackendProducer<T>> {
|
||||||
|
this.producerTopics.push(options.topic);
|
||||||
|
|
||||||
|
return {
|
||||||
|
send: async (message, properties) => {
|
||||||
|
this.sent.push(properties === undefined ? { message } : { message, properties });
|
||||||
|
},
|
||||||
|
flush: async () => {
|
||||||
|
this.flushCount += 1;
|
||||||
|
if (this.failFlush) {
|
||||||
|
return Promise.reject("flush failed");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
close: async () => {
|
||||||
|
this.closeCount += 1;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
createConsumer<T>(_options: CreateConsumerOptions<T>): Promise<BackendConsumer<T>> {
|
||||||
|
return Promise.reject("consumer not supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
async close(): Promise<void> {}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Producer", () => {
|
||||||
|
it("routes the compatibility facade through the scoped Effect producer", async () => {
|
||||||
|
const backend = new ProducerBackend();
|
||||||
|
const producer = makeProducer<string>(backend, "tg.test.producer");
|
||||||
|
|
||||||
|
await producer.start();
|
||||||
|
await producer.send("message-1", "hello");
|
||||||
|
await producer.stop();
|
||||||
|
|
||||||
|
expect(backend.producerTopics).toEqual(["tg.test.producer"]);
|
||||||
|
expect(backend.sent).toEqual([
|
||||||
|
{ message: "hello", properties: { id: "message-1" } },
|
||||||
|
]);
|
||||||
|
expect(backend.flushCount).toBe(1);
|
||||||
|
expect(backend.closeCount).toBe(1);
|
||||||
|
await expect(producer.stop()).resolves.toBeUndefined();
|
||||||
|
|
||||||
|
const error = await producer.send("message-2", "late").catch((caught: unknown) => caught);
|
||||||
|
expect(error).toMatchObject({
|
||||||
|
_tag: "MessagingLifecycleError",
|
||||||
|
operation: "send",
|
||||||
|
resource: "tg.test.producer",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("closes the scoped producer when flush fails during stop", async () => {
|
||||||
|
const backend = new ProducerBackend();
|
||||||
|
const producer = makeProducer<string>(backend, "tg.test.producer");
|
||||||
|
|
||||||
|
await producer.start();
|
||||||
|
backend.failFlush = true;
|
||||||
|
|
||||||
|
const error = await producer.stop().catch((caught: unknown) => caught);
|
||||||
|
|
||||||
|
expect(error).toMatchObject({
|
||||||
|
_tag: "MessagingDeliveryError",
|
||||||
|
operation: "flush",
|
||||||
|
topic: "tg.test.producer",
|
||||||
|
});
|
||||||
|
expect(backend.flushCount).toBe(1);
|
||||||
|
expect(backend.closeCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -6,8 +6,9 @@
|
||||||
|
|
||||||
import type { PubSubBackend } from "../backend/types.js";
|
import type { PubSubBackend } from "../backend/types.js";
|
||||||
import type { ProducerMetrics } from "../metrics/prometheus.js";
|
import type { ProducerMetrics } from "../metrics/prometheus.js";
|
||||||
import { Effect } from "effect";
|
import { Effect, Exit, Scope } from "effect";
|
||||||
import { makeEffectProducerHandle, type EffectProducer } from "./runtime.js";
|
import { PubSub } from "../backend/pubsub.js";
|
||||||
|
import { makeEffectProducerFromPubSub, type EffectProducer } from "./runtime.js";
|
||||||
import { messagingLifecycleError } from "../errors.js";
|
import { messagingLifecycleError } from "../errors.js";
|
||||||
|
|
||||||
export interface Producer<T> {
|
export interface Producer<T> {
|
||||||
|
|
@ -16,46 +17,61 @@ export interface Producer<T> {
|
||||||
readonly stop: () => Promise<void>;
|
readonly stop: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ProducerRuntime<T> {
|
||||||
|
readonly scope: Scope.Closeable;
|
||||||
|
readonly producer: EffectProducer<T>;
|
||||||
|
}
|
||||||
|
|
||||||
export function makeProducer<T>(
|
export function makeProducer<T>(
|
||||||
pubsub: PubSubBackend,
|
pubsub: PubSubBackend,
|
||||||
topic: string,
|
topic: string,
|
||||||
metrics?: ProducerMetrics,
|
metrics?: ProducerMetrics,
|
||||||
): Producer<T> {
|
): Producer<T> {
|
||||||
let effectProducer: EffectProducer<T> | null = null;
|
let runtime: ProducerRuntime<T> | null = null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
start: () =>
|
start: () =>
|
||||||
Effect.runPromise(
|
runtime !== null
|
||||||
Effect.gen(function* () {
|
? Promise.resolve()
|
||||||
const backend = yield* Effect.tryPromise({
|
: Effect.runPromise(
|
||||||
try: () => pubsub.createProducer<T>({ topic }),
|
Effect.gen(function* () {
|
||||||
catch: (error) => messagingLifecycleError(topic, "create-producer", error),
|
const scope = yield* Scope.make();
|
||||||
});
|
const startProducer = Effect.gen(function* () {
|
||||||
effectProducer = makeEffectProducerHandle(backend, {
|
const producer = yield* makeEffectProducerFromPubSub<T>(
|
||||||
topic,
|
PubSub.fromBackend(pubsub),
|
||||||
...(metrics === undefined ? {} : { metrics }),
|
{
|
||||||
});
|
topic,
|
||||||
}),
|
...(metrics === undefined ? {} : { metrics }),
|
||||||
),
|
},
|
||||||
send: (id, message) =>
|
).pipe(
|
||||||
effectProducer === null
|
Scope.provide(scope),
|
||||||
|
Effect.mapError((error) => messagingLifecycleError(topic, "create-producer", error)),
|
||||||
|
);
|
||||||
|
|
||||||
|
runtime = { scope, producer };
|
||||||
|
});
|
||||||
|
|
||||||
|
yield* startProducer.pipe(
|
||||||
|
Effect.onError((cause) => Scope.close(scope, Exit.failCause(cause))),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
send: (id, message) => {
|
||||||
|
const current = runtime;
|
||||||
|
return current === null
|
||||||
? Effect.runPromise(Effect.fail(messagingLifecycleError(topic, "send", "Producer not started")))
|
? Effect.runPromise(Effect.fail(messagingLifecycleError(topic, "send", "Producer not started")))
|
||||||
: Effect.runPromise(effectProducer.send(id, message)),
|
: Effect.runPromise(current.producer.send(id, message));
|
||||||
stop: () =>
|
},
|
||||||
Effect.runPromise(
|
stop: () => {
|
||||||
Effect.gen(function* () {
|
const current = runtime;
|
||||||
if (effectProducer !== null) {
|
runtime = null;
|
||||||
const producer = effectProducer;
|
return current === null
|
||||||
yield* producer.flush.pipe(
|
? Promise.resolve()
|
||||||
Effect.flatMap(() => producer.close),
|
: Effect.runPromise(
|
||||||
Effect.ensuring(
|
current.producer.flush.pipe(
|
||||||
Effect.sync(() => {
|
Effect.ensuring(Scope.close(current.scope, Exit.void)),
|
||||||
effectProducer = null;
|
),
|
||||||
}),
|
);
|
||||||
),
|
},
|
||||||
);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue