2026-04-05 21:09:33 -05:00
|
|
|
/**
|
|
|
|
|
* Flow-aware processor that manages dynamic flow instances.
|
|
|
|
|
*
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
* Subscribes to config-push topic and dynamically creates/destroys
|
|
|
|
|
* flow instances based on the configuration received.
|
|
|
|
|
*
|
2026-04-05 21:09:33 -05:00
|
|
|
* Python reference: trustgraph-base/trustgraph/base/flow_processor.py
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { AsyncProcessor, type ProcessorConfig } from "./async-processor.js";
|
|
|
|
|
import type { Spec } from "../spec/types.js";
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
import type { BackendConsumer } from "../backend/types.js";
|
2026-04-05 21:09:33 -05:00
|
|
|
import { Flow, type FlowDefinition } from "./flow.js";
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
import { topics } from "../schema/topics.js";
|
|
|
|
|
|
|
|
|
|
interface ConfigPush {
|
|
|
|
|
version: number;
|
|
|
|
|
config: Record<string, unknown>;
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
export abstract class FlowProcessor extends AsyncProcessor {
|
|
|
|
|
private specifications: Spec[] = [];
|
|
|
|
|
private flows = new Map<string, Flow>();
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
private configConsumer: BackendConsumer<ConfigPush> | null = null;
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
constructor(config: ProcessorConfig) {
|
|
|
|
|
super(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerSpecification(spec: Spec): void {
|
|
|
|
|
this.specifications.push(spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async run(): Promise<void> {
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
// Subscribe to config-push topic to receive flow definitions.
|
|
|
|
|
// Use "earliest" to replay any config pushes that arrived before this service started.
|
|
|
|
|
this.configConsumer = await this.pubsub.createConsumer<ConfigPush>({
|
|
|
|
|
topic: topics.configPush,
|
|
|
|
|
subscription: `${this.config.id}-config-push`,
|
|
|
|
|
initialPosition: "earliest",
|
2026-04-05 21:09:33 -05:00
|
|
|
});
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
|
|
|
|
|
console.log(`[${this.config.id}] Listening for config pushes on ${topics.configPush}`);
|
|
|
|
|
|
|
|
|
|
while (this.running) {
|
|
|
|
|
try {
|
|
|
|
|
const msg = await this.configConsumer.receive(2000);
|
|
|
|
|
if (!msg) continue;
|
|
|
|
|
|
|
|
|
|
const push = msg.value();
|
|
|
|
|
console.log(`[${this.config.id}] Received config push version=${push.version}`);
|
|
|
|
|
|
|
|
|
|
await this.onConfigureFlows(push.config, push.version);
|
|
|
|
|
|
|
|
|
|
// Also call any registered config handlers
|
|
|
|
|
for (const handler of this.configHandlers) {
|
|
|
|
|
await handler(push.config, push.version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.configConsumer.acknowledge(msg);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (!this.running) break;
|
|
|
|
|
console.error(`[${this.config.id}] Config consumer error:`, err);
|
|
|
|
|
await sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async onConfigureFlows(
|
|
|
|
|
config: Record<string, unknown>,
|
|
|
|
|
version: number,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const flowDefs = config.flows as Record<string, FlowDefinition> | undefined;
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
if (!flowDefs) {
|
|
|
|
|
console.log(`[${this.config.id}] No flows in config push, skipping`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
// Stop removed flows
|
|
|
|
|
for (const [name, flow] of this.flows) {
|
|
|
|
|
if (!(name in flowDefs)) {
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
console.log(`[${this.config.id}] Stopping removed flow: ${name}`);
|
2026-04-05 21:09:33 -05:00
|
|
|
await flow.stop();
|
|
|
|
|
this.flows.delete(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
// Start or update flows
|
2026-04-05 21:09:33 -05:00
|
|
|
for (const [name, defn] of Object.entries(flowDefs)) {
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
// Skip invalid definitions (e.g., stringified JSON)
|
|
|
|
|
if (typeof defn !== "object" || defn === null) {
|
|
|
|
|
console.warn(`[${this.config.id}] Skipping flow "${name}": definition is not an object`);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:09:33 -05:00
|
|
|
if (!this.flows.has(name)) {
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
console.log(`[${this.config.id}] Starting flow "${name}" with topics:`, defn.topics);
|
2026-04-05 21:09:33 -05:00
|
|
|
const flow = new Flow(name, this.config.id, this.pubsub, defn, this.specifications);
|
|
|
|
|
await flow.start();
|
|
|
|
|
this.flows.set(name, flow);
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
console.log(`[${this.config.id}] Flow "${name}" started`);
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override async stop(): Promise<void> {
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
if (this.configConsumer) {
|
|
|
|
|
await this.configConsumer.close();
|
|
|
|
|
this.configConsumer = null;
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
for (const flow of this.flows.values()) {
|
|
|
|
|
await flow.stop();
|
|
|
|
|
}
|
|
|
|
|
this.flows.clear();
|
|
|
|
|
await super.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
fix: NATS pipeline bugs, add integration tests and service runners
Fix three critical bugs preventing the NATS message pipeline from working:
- FlowProcessor now subscribes to config-push topic (was missing entirely),
using DeliverPolicy.All to replay config on service restart
- NATS streams use wildcard subjects (tg.flow.>) instead of per-topic
narrow filters that caused 503 errors on publish
- Subscriber dispatch loop has exponential backoff on errors to prevent
tight error loops
Add service runner scripts (gateway, config, LLM) and a 7-test
integration suite that verifies config CRUD, WebSocket round-trip,
and full LLM text-completion through the NATS pipeline.
Fix Docker Compose infra: pin Tempo to v2.6.1, remove deprecated Loki
config fields, add user:0 for volume permissions, remap conflicting
ports (FalkorDB 6380, OTLP 4327/4328).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:41:39 -05:00
|
|
|
|
|
|
|
|
function sleep(ms: number): Promise<void> {
|
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
|
}
|