feat(core-shared): extend wireUseCase with analytics arg and Analyzed brand propagation

Add AnalyticsProtocol to bind-protocols, extend WireUseCaseOptions with
optional analytics field, and compose the __analyzed brand inline in
wireUseCase (innermost, before withAudit) when analytics is provided.

Propagate __analyzed through withCapture and withSpan PROPAGATED_BRANDS
so the outermost container binding carries the brand for boot-time
assertion checks (Story 05).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-18 15:12:57 +00:00
parent ff1e0b052c
commit 32018e8a7b
5 changed files with 149 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
import type { Container } from "inversify";
import type { ITracer } from "../instrumentation/tracer.interface";
import type { ILogger } from "../instrumentation/logger.interface";
import type { AuditLogProtocol } from "../di/bind-protocols";
import type { AuditLogProtocol, AnalyticsProtocol } from "../di/bind-protocols";
import { withSpan } from "../instrumentation/with-span";
import { withCapture } from "../instrumentation/with-capture";
import { attachBrand } from "./brand-runtime";
@@ -20,6 +20,7 @@ export type WireUseCaseOptions<
name: string;
tracer: ITracer;
logger: ILogger;
analytics?: AnalyticsProtocol;
auditLog?: AuditLogProtocol;
};
@@ -50,6 +51,7 @@ export function wireUseCase<
name,
tracer,
logger,
analytics,
auditLog,
} = opts;
@@ -59,14 +61,30 @@ export function wireUseCase<
const raw = factory(...deps);
let toWrap: (...args: FnArgs) => Promise<R>;
if (auditLog !== undefined) {
void auditLog; // reserved for future automated audit recording from manifest declarations
const audited: (...args: FnArgs) => Promise<R> = (...args) => raw(...args);
attachBrand(audited, "__audited");
toWrap = audited;
// analytics is innermost — wraps raw before audit. withAnalytics lives in
// @repo/core-analytics which depends on core-shared (not vice versa), so we
// replicate the forwarding-wrapper semantics inline to avoid a circular dep.
if (analytics !== undefined) {
void analytics; // reserved for future automated event recording from manifest declarations
const analyzed: (...args: FnArgs) => Promise<R> = (...args) => raw(...args);
attachBrand(analyzed, "__analyzed");
toWrap = analyzed;
} else {
toWrap = raw;
}
if (auditLog !== undefined) {
void auditLog; // reserved for future automated audit recording from manifest declarations
// snapshot before reassignment — closure captures variable reference, not value
const prev = toWrap;
const audited: (...args: FnArgs) => Promise<R> = (...args) => prev(...args);
attachBrand(audited, "__audited");
// propagate __analyzed from the analytics layer below so withCapture/withSpan
// can see it on the outermost binding
if ((prev as unknown as Record<string, unknown>)["__analyzed"] === true) {
attachBrand(audited, "__analyzed");
}
toWrap = audited;
}
const wired = withSpan(
tracer,