feat(p9): route OutboundService through the Capability Broker

Task 9.3: OutboundService now delegates the execute step to CapabilityBroker
(policy-gated + obligation-enforced) instead of calling a provider directly, while
keeping idempotency + rate-limit + the command/attempt ledger. BLOCKED obligations
→ command BLOCKED; a fail-closed throw marks the command FAILED then propagates.
AdaptersModule imports CapabilityModule; P4/P6/P8 egress now flows through the
broker unchanged. Updated 6 spec constructions; added a DENY_OUTBOUND test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-01 20:04:11 +05:30
parent c50a501f32
commit 28e517fc1b
7 changed files with 50 additions and 23 deletions
@@ -1,13 +1,14 @@
import { randomUUID } from 'node:crypto';
import { Injectable, NotFoundException } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { AdapterRegistry } from './adapter.registry';
import { CapabilityBroker } from '../capability/capability.broker';
/**
* Outbound to a provider — in P5 the adapter's `send` is a SANDBOX sink (no real
* network). Per-(channelType,target) rate limiting; idempotent per command key;
* every attempt recorded. Real delivery is gated to P6+.
* Outbound command layer (P5). Owns idempotency + per-(channelType,target) rate
* limiting + the delivery ledger. As of P9 the actual execute step is delegated to
* the governed CapabilityBroker (policy gate + obligations + provider selection);
* this layer no longer talks to a provider directly.
*/
@Injectable()
export class OutboundService {
@@ -17,7 +18,7 @@ export class OutboundService {
constructor(
private readonly prisma: PrismaService,
private readonly registry: AdapterRegistry,
private readonly broker: CapabilityBroker,
) {}
async send(
@@ -25,10 +26,8 @@ export class OutboundService {
target: string,
payload: Record<string, unknown>,
idempotencyKey: string = randomUUID(),
scopeId?: string,
) {
const reg = this.registry.get(channelType);
if (!reg) throw new NotFoundException(`unknown channel type: ${channelType}`);
const existing = await this.prisma.iiosOutboundCommand.findUnique({ where: { idempotencyKey } });
if (existing) return existing;
@@ -43,11 +42,23 @@ export class OutboundService {
const cmd = await this.prisma.iiosOutboundCommand.create({
data: { channelType, target, payload: payload as Prisma.InputJsonValue, status: 'PENDING', idempotencyKey },
});
const result = await reg.adapter.send({ channelType, target, payload }); // sandbox — no network
let result;
try {
result = await this.broker.invoke({ capability: 'channel.send', channelType, target, payload, idempotencyKey, scopeId });
} catch (err) {
// Fail-closed (e.g. PolicyDeniedError): mark the command FAILED, record the attempt, propagate.
await this.prisma.$transaction([
this.prisma.iiosOutboundCommand.update({ where: { id: cmd.id }, data: { status: 'FAILED' } }),
this.prisma.iiosDeliveryAttempt.create({ data: { commandId: cmd.id, attemptNo: 1, status: 'FAILED', errorCode: (err as Error).name } }),
]);
throw err;
}
const [updated] = await this.prisma.$transaction([
this.prisma.iiosOutboundCommand.update({ where: { id: cmd.id }, data: { status: 'SENT', providerRef: result.providerRef } }),
this.prisma.iiosOutboundCommand.update({ where: { id: cmd.id }, data: { status: result.outcome, providerRef: result.providerRef } }),
this.prisma.iiosDeliveryAttempt.create({
data: { commandId: cmd.id, attemptNo: 1, status: 'SENT', providerRef: result.providerRef, latencyMs: result.latencyMs },
data: { commandId: cmd.id, attemptNo: 1, status: result.outcome, providerRef: result.providerRef, latencyMs: result.latencyMs, errorCode: result.errorCode },
}),
]);
return updated;