@unifyplane/logsdk 0.1.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.
Files changed (170) hide show
  1. package/README.md +18 -0
  2. package/contracts/repo.capabilities.json +12 -0
  3. package/contracts/repo.runtime.json +49 -0
  4. package/contracts/schemas/repo/repo.runtime.v1.schema.json +134 -0
  5. package/dist/core/clock.d.ts +3 -0
  6. package/dist/core/clock.d.ts.map +1 -0
  7. package/dist/core/clock.js +21 -0
  8. package/dist/core/clock.js.map +1 -0
  9. package/dist/core/context.d.ts +10 -0
  10. package/dist/core/context.d.ts.map +1 -0
  11. package/dist/core/context.js +100 -0
  12. package/dist/core/context.js.map +1 -0
  13. package/dist/core/fanout.d.ts +4 -0
  14. package/dist/core/fanout.d.ts.map +1 -0
  15. package/dist/core/fanout.js +32 -0
  16. package/dist/core/fanout.js.map +1 -0
  17. package/dist/core/ids.d.ts +6 -0
  18. package/dist/core/ids.d.ts.map +1 -0
  19. package/dist/core/ids.js +27 -0
  20. package/dist/core/ids.js.map +1 -0
  21. package/dist/core/message_constraints.d.ts +4 -0
  22. package/dist/core/message_constraints.d.ts.map +1 -0
  23. package/dist/core/message_constraints.js +53 -0
  24. package/dist/core/message_constraints.js.map +1 -0
  25. package/dist/core/outcomes.d.ts +5 -0
  26. package/dist/core/outcomes.d.ts.map +1 -0
  27. package/dist/core/outcomes.js +4 -0
  28. package/dist/core/outcomes.js.map +1 -0
  29. package/dist/core/record_builder.d.ts +34 -0
  30. package/dist/core/record_builder.d.ts.map +1 -0
  31. package/dist/core/record_builder.js +188 -0
  32. package/dist/core/record_builder.js.map +1 -0
  33. package/dist/core/spool.d.ts +4 -0
  34. package/dist/core/spool.d.ts.map +1 -0
  35. package/dist/core/spool.js +31 -0
  36. package/dist/core/spool.js.map +1 -0
  37. package/dist/core/types.d.ts +33 -0
  38. package/dist/core/types.d.ts.map +1 -0
  39. package/dist/core/types.js +4 -0
  40. package/dist/core/types.js.map +1 -0
  41. package/dist/esm/core/clock.js +21 -0
  42. package/dist/esm/core/clock.js.map +1 -0
  43. package/dist/esm/core/context.js +100 -0
  44. package/dist/esm/core/context.js.map +1 -0
  45. package/dist/esm/core/fanout.js +32 -0
  46. package/dist/esm/core/fanout.js.map +1 -0
  47. package/dist/esm/core/ids.js +27 -0
  48. package/dist/esm/core/ids.js.map +1 -0
  49. package/dist/esm/core/message_constraints.js +53 -0
  50. package/dist/esm/core/message_constraints.js.map +1 -0
  51. package/dist/esm/core/outcomes.js +4 -0
  52. package/dist/esm/core/outcomes.js.map +1 -0
  53. package/dist/esm/core/record_builder.js +188 -0
  54. package/dist/esm/core/record_builder.js.map +1 -0
  55. package/dist/esm/core/spool.js +31 -0
  56. package/dist/esm/core/spool.js.map +1 -0
  57. package/dist/esm/core/types.js +4 -0
  58. package/dist/esm/core/types.js.map +1 -0
  59. package/dist/esm/index.js +52 -0
  60. package/dist/esm/index.js.map +1 -0
  61. package/dist/esm/sinks/file_ndjson.js +29 -0
  62. package/dist/esm/sinks/file_ndjson.js.map +1 -0
  63. package/dist/esm/sinks/file_ndjson_sink.js +36 -0
  64. package/dist/esm/sinks/file_ndjson_sink.js.map +1 -0
  65. package/dist/esm/sinks/sink_types.js +2 -0
  66. package/dist/esm/sinks/sink_types.js.map +1 -0
  67. package/dist/esm/sinks/stdout_sink.js +15 -0
  68. package/dist/esm/sinks/stdout_sink.js.map +1 -0
  69. package/dist/esm/validate/api_surface_guard.js +63 -0
  70. package/dist/esm/validate/api_surface_guard.js.map +1 -0
  71. package/dist/esm/validate/noncompliance.js +17 -0
  72. package/dist/esm/validate/noncompliance.js.map +1 -0
  73. package/dist/esm/validate/schema_guard.js +151 -0
  74. package/dist/esm/validate/schema_guard.js.map +1 -0
  75. package/dist/index.d.ts +26 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +52 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/sinks/file_ndjson.d.ts +18 -0
  80. package/dist/sinks/file_ndjson.d.ts.map +1 -0
  81. package/dist/sinks/file_ndjson.js +29 -0
  82. package/dist/sinks/file_ndjson.js.map +1 -0
  83. package/dist/sinks/file_ndjson_sink.d.ts +7 -0
  84. package/dist/sinks/file_ndjson_sink.d.ts.map +1 -0
  85. package/dist/sinks/file_ndjson_sink.js +36 -0
  86. package/dist/sinks/file_ndjson_sink.js.map +1 -0
  87. package/dist/sinks/sink_types.d.ts +12 -0
  88. package/dist/sinks/sink_types.d.ts.map +1 -0
  89. package/dist/sinks/sink_types.js +2 -0
  90. package/dist/sinks/sink_types.js.map +1 -0
  91. package/dist/sinks/stdout_sink.d.ts +6 -0
  92. package/dist/sinks/stdout_sink.d.ts.map +1 -0
  93. package/dist/sinks/stdout_sink.js +15 -0
  94. package/dist/sinks/stdout_sink.js.map +1 -0
  95. package/dist/types/core/clock.d.ts +3 -0
  96. package/dist/types/core/clock.d.ts.map +1 -0
  97. package/dist/types/core/context.d.ts +10 -0
  98. package/dist/types/core/context.d.ts.map +1 -0
  99. package/dist/types/core/fanout.d.ts +4 -0
  100. package/dist/types/core/fanout.d.ts.map +1 -0
  101. package/dist/types/core/ids.d.ts +6 -0
  102. package/dist/types/core/ids.d.ts.map +1 -0
  103. package/dist/types/core/message_constraints.d.ts +4 -0
  104. package/dist/types/core/message_constraints.d.ts.map +1 -0
  105. package/dist/types/core/outcomes.d.ts +5 -0
  106. package/dist/types/core/outcomes.d.ts.map +1 -0
  107. package/dist/types/core/record_builder.d.ts +34 -0
  108. package/dist/types/core/record_builder.d.ts.map +1 -0
  109. package/dist/types/core/spool.d.ts +4 -0
  110. package/dist/types/core/spool.d.ts.map +1 -0
  111. package/dist/types/core/types.d.ts +33 -0
  112. package/dist/types/core/types.d.ts.map +1 -0
  113. package/dist/types/index.d.ts +26 -0
  114. package/dist/types/index.d.ts.map +1 -0
  115. package/dist/types/sinks/file_ndjson.d.ts +18 -0
  116. package/dist/types/sinks/file_ndjson.d.ts.map +1 -0
  117. package/dist/types/sinks/file_ndjson_sink.d.ts +7 -0
  118. package/dist/types/sinks/file_ndjson_sink.d.ts.map +1 -0
  119. package/dist/types/sinks/sink_types.d.ts +12 -0
  120. package/dist/types/sinks/sink_types.d.ts.map +1 -0
  121. package/dist/types/sinks/stdout_sink.d.ts +6 -0
  122. package/dist/types/sinks/stdout_sink.d.ts.map +1 -0
  123. package/dist/types/validate/api_surface_guard.d.ts +2 -0
  124. package/dist/types/validate/api_surface_guard.d.ts.map +1 -0
  125. package/dist/types/validate/noncompliance.d.ts +15 -0
  126. package/dist/types/validate/noncompliance.d.ts.map +1 -0
  127. package/dist/types/validate/schema_guard.d.ts +5 -0
  128. package/dist/types/validate/schema_guard.d.ts.map +1 -0
  129. package/dist/validate/api_surface_guard.d.ts +2 -0
  130. package/dist/validate/api_surface_guard.d.ts.map +1 -0
  131. package/dist/validate/api_surface_guard.js +63 -0
  132. package/dist/validate/api_surface_guard.js.map +1 -0
  133. package/dist/validate/noncompliance.d.ts +15 -0
  134. package/dist/validate/noncompliance.d.ts.map +1 -0
  135. package/dist/validate/noncompliance.js +17 -0
  136. package/dist/validate/noncompliance.js.map +1 -0
  137. package/dist/validate/schema_guard.d.ts +5 -0
  138. package/dist/validate/schema_guard.d.ts.map +1 -0
  139. package/dist/validate/schema_guard.js +151 -0
  140. package/dist/validate/schema_guard.js.map +1 -0
  141. package/evidence/audit/llm/llm_audit_report_2026-01-25T15-11-32Z.json +30 -0
  142. package/evidence/audit/llm/llm_audit_report_2026-01-25T15-53-20Z.json +154 -0
  143. package/evidence/audit/llm/llm_audit_report_2026-01-25T16-14-52Z.json +130 -0
  144. package/evidence/audit/llm/llm_audit_report_2026-01-25T16-15-42Z.json +152 -0
  145. package/evidence/audit/llm/llm_audit_report_2026-01-25T16-17-39Z.json +152 -0
  146. package/evidence/audit/llm/llm_audit_report_2026-01-25T16-18-51Z.json +152 -0
  147. package/evidence/audit/llm/llm_audit_report_2026-01-25T16-19-50Z.json +152 -0
  148. package/evidence/audit/llm/llm_audit_report_2026-01-25T16-20-36Z.json +152 -0
  149. package/evidence/audit/llm/llm_audit_report_2026-01-25T23-22-43Z.json +170 -0
  150. package/evidence/audit/llm/llm_audit_report_2026-01-25T23-39-03Z.json +157 -0
  151. package/filestructure.output.txt +73 -0
  152. package/package.json +16 -0
  153. package/src/core/clock.ts +25 -0
  154. package/src/core/context.ts +142 -0
  155. package/src/core/fanout.ts +38 -0
  156. package/src/core/ids.ts +35 -0
  157. package/src/core/message_constraints.ts +66 -0
  158. package/src/core/outcomes.ts +5 -0
  159. package/src/core/record_builder.ts +269 -0
  160. package/src/core/spool.ts +41 -0
  161. package/src/core/types.ts +56 -0
  162. package/src/index.ts +105 -0
  163. package/src/sinks/file_ndjson.ts +42 -0
  164. package/src/sinks/file_ndjson_sink.ts +45 -0
  165. package/src/sinks/sink_types.ts +15 -0
  166. package/src/sinks/stdout_sink.ts +20 -0
  167. package/src/validate/api_surface_guard.ts +106 -0
  168. package/src/validate/noncompliance.ts +33 -0
  169. package/src/validate/schema_guard.ts +242 -0
  170. package/unifyplane-logsdk-0.1.0.tgz +0 -0
package/src/index.ts ADDED
@@ -0,0 +1,105 @@
1
+ import { initContext } from "./core/context.js";
2
+ import { createIdGenerator } from "./core/ids.js";
3
+ import { buildStepRecord, SystemOwnership } from "./core/record_builder.js";
4
+ import { assertValidMessage } from "./core/message_constraints.js";
5
+ import { fanout } from "./core/fanout.js";
6
+ import { assertApiSurface } from "./validate/api_surface_guard.js";
7
+ import { assertStepRecord } from "./validate/schema_guard.js";
8
+ import {
9
+ AUTHORITATIVE_FAILURE_SWALLOWED,
10
+ CONTEXT_INJECTION_VIOLATION,
11
+ PAYLOAD_EMBEDDING_VIOLATION,
12
+ NonComplianceError,
13
+ } from "./validate/noncompliance.js";
14
+ import type { StepRecord } from "./core/types.js";
15
+
16
+ type SinkClass = "authoritative" | "observability";
17
+
18
+ type Sink = {
19
+ emit(record: Readonly<StepRecord>): void | Promise<void>;
20
+ flush?(): void | Promise<void>;
21
+ };
22
+
23
+ type SinkEntry = {
24
+ sink: Sink;
25
+ sinkClass: SinkClass;
26
+ };
27
+
28
+ export type LogSDKConfig = {
29
+ context: unknown;
30
+ system: SystemOwnership;
31
+ sinks: ReadonlyArray<SinkEntry>;
32
+ };
33
+
34
+ export type LogApi = {
35
+ step(message: string): Promise<void>;
36
+ flush?: () => Promise<void>;
37
+ };
38
+
39
+ export type { SystemOwnership };
40
+
41
+ export function initLogSDK(config: LogSDKConfig): LogApi {
42
+ initContext(config.context);
43
+
44
+ const idGenerator = createIdGenerator(config.system.instance_id);
45
+ const sinks = [...config.sinks];
46
+
47
+ const api: LogApi = {
48
+ async step(message: string): Promise<void> {
49
+ if (typeof message !== "string") {
50
+ if (message && typeof message === "object") {
51
+ throw new NonComplianceError(
52
+ CONTEXT_INJECTION_VIOLATION,
53
+ "Per-step context injection is not allowed."
54
+ );
55
+ }
56
+
57
+ throw new NonComplianceError(
58
+ PAYLOAD_EMBEDDING_VIOLATION,
59
+ "Message must be a string."
60
+ );
61
+ }
62
+
63
+ try {
64
+ assertValidMessage(message);
65
+ } catch (error) {
66
+ throw new NonComplianceError(
67
+ PAYLOAD_EMBEDDING_VIOLATION,
68
+ error instanceof Error ? error.message : "Invalid message content."
69
+ );
70
+ }
71
+
72
+ const record = buildStepRecord(message, idGenerator.nextSequence(), config.system, {
73
+ record_id: idGenerator.nextRecordId(),
74
+ });
75
+
76
+ assertStepRecord(record);
77
+
78
+ const outcome = await fanout(record, sinks);
79
+ if (outcome === "FAILED") {
80
+ throw new NonComplianceError(
81
+ AUTHORITATIVE_FAILURE_SWALLOWED,
82
+ "Authoritative sink failed to emit."
83
+ );
84
+ }
85
+ },
86
+ async flush(): Promise<void> {
87
+ for (const entry of sinks) {
88
+ if (entry.sink.flush) {
89
+ await entry.sink.flush();
90
+ }
91
+ }
92
+ },
93
+ };
94
+
95
+ assertApiSurface(api);
96
+ return api;
97
+ }
98
+
99
+ export function createLogger(config: LogSDKConfig): LogApi {
100
+ return initLogSDK(config);
101
+ }
102
+
103
+ export { fileNdjsonSink } from "./sinks/file_ndjson.js";
104
+
105
+ export { authoritativeNdjsonSink } from "./sinks/file_ndjson.js";
@@ -0,0 +1,42 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import type { StepRecord } from "../core/types.js";
4
+
5
+ type SinkClass = "authoritative" | "observability";
6
+
7
+ type Sink = {
8
+ emit(record: Readonly<StepRecord>): void | Promise<void>;
9
+ flush?(): void | Promise<void>;
10
+ };
11
+
12
+ type SinkEntry = {
13
+ sink: Sink;
14
+ sinkClass: SinkClass;
15
+ };
16
+
17
+ export function fileNdjsonSink(pathOrOptions: string | { path: string }): SinkEntry {
18
+ const p = typeof pathOrOptions === "string" ? pathOrOptions : pathOrOptions.path;
19
+ const dir = path.dirname(p);
20
+ if (!fs.existsSync(dir)) {
21
+ fs.mkdirSync(dir, { recursive: true });
22
+ }
23
+ const sink: Sink = {
24
+ emit(record: Readonly<StepRecord>): void {
25
+ fs.appendFileSync(p, JSON.stringify(record) + "\n", { encoding: "utf8" });
26
+ },
27
+ };
28
+ return { sink, sinkClass: "observability" };
29
+ }
30
+ export function authoritativeNdjsonSink(pathOrOptions: string | { path: string }): SinkEntry {
31
+ const p = typeof pathOrOptions === "string" ? pathOrOptions : pathOrOptions.path;
32
+ const dir = path.dirname(p);
33
+ if (!fs.existsSync(dir)) {
34
+ fs.mkdirSync(dir, { recursive: true });
35
+ }
36
+ const sink: Sink = {
37
+ emit(record: Readonly<StepRecord>): void {
38
+ fs.appendFileSync(p, JSON.stringify(record) + "\n", { encoding: "utf8" });
39
+ },
40
+ };
41
+ return { sink, sinkClass: "authoritative" };
42
+ }
@@ -0,0 +1,45 @@
1
+ /// <reference path="../fs-shim.d.ts" />
2
+
3
+ import { promises as fs } from "fs";
4
+ import type { StepRecord } from "../core/types.js";
5
+
6
+ export interface FileNdjsonSink {
7
+ emit(record: StepRecord): Promise<void>;
8
+ close(): Promise<void>;
9
+ }
10
+
11
+ export function createFileNdjsonSink(filePath: string): FileNdjsonSink {
12
+ let closed = false;
13
+
14
+ return {
15
+ async emit(record: StepRecord): Promise<void> {
16
+ if (closed) {
17
+ throw new Error("File NDJSON sink is closed.");
18
+ }
19
+
20
+ const line = JSON.stringify(record);
21
+ const payload = `${line}\n`;
22
+ const handle = await fs.open(filePath, "a");
23
+
24
+ try {
25
+ const result = await handle.write(payload, undefined, "utf8");
26
+ if (!result || typeof result.bytesWritten !== "number") {
27
+ throw new Error("File NDJSON sink write did not report bytes.");
28
+ }
29
+
30
+ if (result.bytesWritten !== payload.length) {
31
+ throw new Error("File NDJSON sink partial write detected.");
32
+ }
33
+ await handle.sync();
34
+ } catch (error) {
35
+ const message = error instanceof Error ? error.message : String(error);
36
+ throw new Error(`File NDJSON sink write failed: ${message}`);
37
+ } finally {
38
+ await handle.close();
39
+ }
40
+ },
41
+ async close(): Promise<void> {
42
+ closed = true;
43
+ },
44
+ };
45
+ }
@@ -0,0 +1,15 @@
1
+ import type { StepRecord as CoreStepRecord } from "../core/types.js";
2
+
3
+ export type StepRecord = Readonly<CoreStepRecord>;
4
+
5
+ export type SinkClass = "authoritative" | "observability";
6
+
7
+ export interface Sink<TRecord = StepRecord> {
8
+ emit(record: Readonly<TRecord>): void | Promise<void>;
9
+ flush?(): void | Promise<void>;
10
+ }
11
+
12
+ export interface SinkEntry<TRecord = StepRecord> {
13
+ sink: Sink<TRecord>;
14
+ sinkClass: SinkClass;
15
+ }
@@ -0,0 +1,20 @@
1
+ /// <reference path="../process-shim.d.ts" />
2
+
3
+ import type { StepRecord } from "../core/types.js";
4
+
5
+ export interface StdoutSink {
6
+ emit(record: StepRecord): Promise<void>;
7
+ }
8
+
9
+ export function createStdoutSink(): StdoutSink {
10
+ return {
11
+ async emit(record: StepRecord): Promise<void> {
12
+ try {
13
+ const line = JSON.stringify(record);
14
+ process.stdout.write(`${line}\n`);
15
+ } catch {
16
+ // Best-effort only: never throw.
17
+ }
18
+ },
19
+ };
20
+ }
@@ -0,0 +1,106 @@
1
+ import {
2
+ API_SURFACE_VIOLATION,
3
+ NonComplianceError,
4
+ } from "./noncompliance.js";
5
+
6
+ const ALLOWED_SCOPES = ["request", "job", "component"] as const;
7
+
8
+ type AllowedScope = (typeof ALLOWED_SCOPES)[number];
9
+
10
+ const FORBIDDEN_NAMES = new Set([
11
+ "withContext",
12
+ "ctx",
13
+ "meta",
14
+ "attributes",
15
+ "tags",
16
+ "labels",
17
+ "log",
18
+ "emit",
19
+ "metric",
20
+ "span",
21
+ ]);
22
+
23
+ function assertFunction(value: unknown, name: string, arity: number): void {
24
+ if (typeof value !== "function") {
25
+ throw new NonComplianceError(
26
+ API_SURFACE_VIOLATION,
27
+ `${name} must be a function.`
28
+ );
29
+ }
30
+
31
+ if ((value as Function).length !== arity) {
32
+ throw new NonComplianceError(
33
+ API_SURFACE_VIOLATION,
34
+ `${name} must accept exactly ${arity} argument(s).`
35
+ );
36
+ }
37
+ }
38
+
39
+ function assertChildScopePolicy(child: unknown): void {
40
+ const candidate = child as { allowedScopes?: unknown };
41
+ const allowedScopes = candidate.allowedScopes;
42
+
43
+ if (!Array.isArray(allowedScopes) || allowedScopes.length === 0) {
44
+ throw new NonComplianceError(
45
+ API_SURFACE_VIOLATION,
46
+ "child() must declare allowedScopes to enforce scope union."
47
+ );
48
+ }
49
+
50
+ for (const scope of allowedScopes) {
51
+ if (!ALLOWED_SCOPES.includes(scope as AllowedScope)) {
52
+ throw new NonComplianceError(
53
+ API_SURFACE_VIOLATION,
54
+ `child() scope not allowed: ${String(scope)}.`
55
+ );
56
+ }
57
+ }
58
+ }
59
+
60
+ export function assertApiSurface(surface: unknown): void {
61
+ if (!surface || typeof surface !== "object") {
62
+ throw new NonComplianceError(
63
+ API_SURFACE_VIOLATION,
64
+ "API surface must be an object."
65
+ );
66
+ }
67
+
68
+ const api = surface as Record<string, unknown>;
69
+ const keys = Object.keys(api);
70
+
71
+ for (const key of keys) {
72
+ if (FORBIDDEN_NAMES.has(key)) {
73
+ throw new NonComplianceError(
74
+ API_SURFACE_VIOLATION,
75
+ `Forbidden API method detected: ${key}.`
76
+ );
77
+ }
78
+ }
79
+
80
+ if (!("step" in api)) {
81
+ throw new NonComplianceError(
82
+ API_SURFACE_VIOLATION,
83
+ "API surface must expose step(message)."
84
+ );
85
+ }
86
+
87
+ assertFunction(api.step, "step", 1);
88
+
89
+ if ("flush" in api) {
90
+ assertFunction(api.flush, "flush", 0);
91
+ }
92
+
93
+ if ("child" in api) {
94
+ assertFunction(api.child, "child", 1);
95
+ assertChildScopePolicy(api.child);
96
+ }
97
+
98
+ for (const key of keys) {
99
+ if (key !== "step" && key !== "child" && key !== "flush") {
100
+ throw new NonComplianceError(
101
+ API_SURFACE_VIOLATION,
102
+ `Unexpected API export: ${key}.`
103
+ );
104
+ }
105
+ }
106
+ }
@@ -0,0 +1,33 @@
1
+ export const API_SURFACE_VIOLATION = "API_SURFACE_VIOLATION" as const;
2
+ export const CONTEXT_INJECTION_VIOLATION =
3
+ "CONTEXT_INJECTION_VIOLATION" as const;
4
+ export const PAYLOAD_EMBEDDING_VIOLATION =
5
+ "PAYLOAD_EMBEDDING_VIOLATION" as const;
6
+ export const CANONICAL_SCHEMA_VIOLATION =
7
+ "CANONICAL_SCHEMA_VIOLATION" as const;
8
+ export const SINK_MUTATION_VIOLATION = "SINK_MUTATION_VIOLATION" as const;
9
+ export const NO_AUTHORITATIVE_SINK = "NO_AUTHORITATIVE_SINK" as const;
10
+ export const FANOUT_NONDETERMINISM = "FANOUT_NONDETERMINISM" as const;
11
+ export const AUTHORITATIVE_FAILURE_SWALLOWED =
12
+ "AUTHORITATIVE_FAILURE_SWALLOWED" as const;
13
+ export const UNBOUNDED_BUFFER_RISK = "UNBOUNDED_BUFFER_RISK" as const;
14
+
15
+ export type NonComplianceCode =
16
+ | typeof API_SURFACE_VIOLATION
17
+ | typeof CONTEXT_INJECTION_VIOLATION
18
+ | typeof PAYLOAD_EMBEDDING_VIOLATION
19
+ | typeof CANONICAL_SCHEMA_VIOLATION
20
+ | typeof SINK_MUTATION_VIOLATION
21
+ | typeof NO_AUTHORITATIVE_SINK
22
+ | typeof FANOUT_NONDETERMINISM
23
+ | typeof AUTHORITATIVE_FAILURE_SWALLOWED
24
+ | typeof UNBOUNDED_BUFFER_RISK;
25
+
26
+ export class NonComplianceError extends Error {
27
+ readonly code: NonComplianceCode;
28
+
29
+ constructor(code: NonComplianceCode, message: string) {
30
+ super(message);
31
+ this.code = code;
32
+ }
33
+ }
@@ -0,0 +1,242 @@
1
+ import type { StepRecord } from "../core/types.js";
2
+ import { assertValidMessage } from "../core/message_constraints.js";
3
+ import type { SinkEntry } from "../sinks/sink_types.js";
4
+ import {
5
+ CANONICAL_SCHEMA_VIOLATION,
6
+ CONTEXT_INJECTION_VIOLATION,
7
+ NO_AUTHORITATIVE_SINK,
8
+ PAYLOAD_EMBEDDING_VIOLATION,
9
+ NonComplianceError,
10
+ } from "./noncompliance.js";
11
+
12
+ const REQUIRED_FIELDS: Array<keyof StepRecord> = [
13
+ "record_version",
14
+ "record_id",
15
+ "sequence",
16
+ "timestamp_utc",
17
+ "monotonic_time",
18
+ "institution",
19
+ "system_name",
20
+ "system_type",
21
+ "environment",
22
+ "system_version",
23
+ "message",
24
+ "context_hash",
25
+ "context_version",
26
+ "record_hash",
27
+ "hash_algorithm",
28
+ ];
29
+
30
+ const OPTIONAL_FIELDS: Array<keyof StepRecord> = [
31
+ "instance_id",
32
+ "trace_id",
33
+ "span_id",
34
+ "parent_step_id",
35
+ "surface_type",
36
+ "surface_name",
37
+ "surface_instance",
38
+ "source_file",
39
+ "source_module",
40
+ "source_function",
41
+ "source_line",
42
+ "message_code",
43
+ "evidence_refs",
44
+ ];
45
+
46
+ const ALLOWED_FIELDS = new Set<string>([...REQUIRED_FIELDS, ...OPTIONAL_FIELDS]);
47
+ const CONTEXT_FIELDS = ["context", "context_blob", "context_data", "context_value"];
48
+
49
+ // Explicit type assertion for string keys from Object.keys(schema.properties)
50
+
51
+ function asRecord(value: unknown): Record<string, unknown> {
52
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
53
+ throw new NonComplianceError(
54
+ CANONICAL_SCHEMA_VIOLATION,
55
+ "Step record must be a plain object."
56
+ );
57
+ }
58
+ return value as Record<string, unknown>;
59
+ }
60
+
61
+ export function assertStepRecord(record: unknown): asserts record is StepRecord {
62
+ const candidate = asRecord(record);
63
+
64
+ if (!Object.isFrozen(candidate)) {
65
+ throw new NonComplianceError(
66
+ CANONICAL_SCHEMA_VIOLATION,
67
+ "Step record must be immutable at emission time."
68
+ );
69
+ }
70
+
71
+ for (const forbidden of CONTEXT_FIELDS) {
72
+ if (forbidden in candidate) {
73
+ throw new NonComplianceError(
74
+ CONTEXT_INJECTION_VIOLATION,
75
+ "Context must not be embedded in the step record."
76
+ );
77
+ }
78
+ }
79
+
80
+ for (const key of Object.keys(candidate)) {
81
+ if (!ALLOWED_FIELDS.has(key)) {
82
+ throw new NonComplianceError(
83
+ CANONICAL_SCHEMA_VIOLATION,
84
+ `Unexpected field in step record: ${key}.`
85
+ );
86
+ }
87
+ }
88
+
89
+ for (const field of REQUIRED_FIELDS) {
90
+ if (!(field in candidate)) {
91
+ throw new NonComplianceError(
92
+ CANONICAL_SCHEMA_VIOLATION,
93
+ `Missing required field: ${field}.`
94
+ );
95
+ }
96
+ }
97
+
98
+ assertStringField(candidate, "record_version");
99
+ assertStringField(candidate, "record_id");
100
+ assertIntegerField(candidate, "sequence");
101
+ assertStringField(candidate, "timestamp_utc");
102
+ assertNumberField(candidate, "monotonic_time");
103
+ assertStringField(candidate, "institution");
104
+ assertStringField(candidate, "system_name");
105
+ assertStringField(candidate, "system_type");
106
+ assertStringField(candidate, "environment");
107
+ assertStringField(candidate, "system_version");
108
+ assertStringField(candidate, "context_hash");
109
+ assertStringField(candidate, "context_version");
110
+ assertStringField(candidate, "record_hash");
111
+ assertStringField(candidate, "hash_algorithm");
112
+
113
+ if (candidate.record_version !== "log.step.v1") {
114
+ throw new NonComplianceError(
115
+ CANONICAL_SCHEMA_VIOLATION,
116
+ "record_version must be log.step.v1."
117
+ );
118
+ }
119
+
120
+ try {
121
+ assertValidMessage(candidate.message);
122
+ } catch (error) {
123
+ throw new NonComplianceError(
124
+ PAYLOAD_EMBEDDING_VIOLATION,
125
+ error instanceof Error ? error.message : "Invalid message content."
126
+ );
127
+ }
128
+
129
+ if (typeof candidate.timestamp_utc === "string") {
130
+ const parsed = Date.parse(candidate.timestamp_utc);
131
+ if (Number.isNaN(parsed)) {
132
+ throw new NonComplianceError(
133
+ CANONICAL_SCHEMA_VIOLATION,
134
+ "timestamp_utc must be a valid ISO-8601 string."
135
+ );
136
+ }
137
+ }
138
+
139
+ assertOptionalStringField(candidate, "instance_id");
140
+ assertOptionalStringField(candidate, "trace_id");
141
+ assertOptionalStringField(candidate, "span_id");
142
+ assertOptionalStringField(candidate, "parent_step_id");
143
+ assertOptionalStringField(candidate, "surface_type");
144
+ assertOptionalStringField(candidate, "surface_name");
145
+ assertOptionalStringField(candidate, "surface_instance");
146
+ assertOptionalStringField(candidate, "source_file");
147
+ assertOptionalStringField(candidate, "source_module");
148
+ assertOptionalStringField(candidate, "source_function");
149
+ assertOptionalStringField(candidate, "message_code");
150
+
151
+ if ("source_line" in candidate && candidate.source_line !== undefined) {
152
+ assertNumberField(candidate, "source_line");
153
+ }
154
+
155
+ if ("evidence_refs" in candidate && candidate.evidence_refs !== undefined) {
156
+ if (!Array.isArray(candidate.evidence_refs)) {
157
+ throw new NonComplianceError(
158
+ CANONICAL_SCHEMA_VIOLATION,
159
+ "evidence_refs must be an array of strings."
160
+ );
161
+ }
162
+ for (const ref of candidate.evidence_refs) {
163
+ if (typeof ref !== "string" || ref.length === 0) {
164
+ throw new NonComplianceError(
165
+ CANONICAL_SCHEMA_VIOLATION,
166
+ "evidence_refs must contain non-empty strings."
167
+ );
168
+ }
169
+ }
170
+ }
171
+ }
172
+
173
+ // Explicit type assertion for string keys from Object.keys(schema.properties)
174
+
175
+ export function assertHasAuthoritativeSink(
176
+ sinks: ReadonlyArray<SinkEntry>
177
+ ): void {
178
+ const hasAuthoritative = sinks.some(
179
+ (entry) => entry.sinkClass === "authoritative"
180
+ );
181
+
182
+ if (!hasAuthoritative) {
183
+ throw new NonComplianceError(
184
+ NO_AUTHORITATIVE_SINK,
185
+ "At least one authoritative sink is required."
186
+ );
187
+ }
188
+ }
189
+
190
+ function assertStringField(
191
+ record: Record<string, unknown>,
192
+ field: string
193
+ ): void {
194
+ const value = record[field];
195
+ if (typeof value !== "string" || value.length === 0) {
196
+ throw new NonComplianceError(
197
+ CANONICAL_SCHEMA_VIOLATION,
198
+ `${field} must be a non-empty string.`
199
+ );
200
+ }
201
+ }
202
+
203
+ function assertOptionalStringField(
204
+ record: Record<string, unknown>,
205
+ field: string
206
+ ): void {
207
+ if (field in record && record[field] !== undefined) {
208
+ const value = record[field];
209
+ if (typeof value !== "string" || value.length === 0) {
210
+ throw new NonComplianceError(
211
+ CANONICAL_SCHEMA_VIOLATION,
212
+ `${field} must be a non-empty string when provided.`
213
+ );
214
+ }
215
+ }
216
+ }
217
+
218
+ function assertNumberField(
219
+ record: Record<string, unknown>,
220
+ field: string
221
+ ): void {
222
+ const value = record[field];
223
+ if (typeof value !== "number" || !Number.isFinite(value)) {
224
+ throw new NonComplianceError(
225
+ CANONICAL_SCHEMA_VIOLATION,
226
+ `${field} must be a finite number.`
227
+ );
228
+ }
229
+ }
230
+
231
+ function assertIntegerField(
232
+ record: Record<string, unknown>,
233
+ field: string
234
+ ): void {
235
+ const value = record[field];
236
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
237
+ throw new NonComplianceError(
238
+ CANONICAL_SCHEMA_VIOLATION,
239
+ `${field} must be a non-negative integer.`
240
+ );
241
+ }
242
+ }
Binary file