@telorun/kernel 0.49.0 → 0.50.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 (186) hide show
  1. package/dist/controller-loader.d.ts +6 -1
  2. package/dist/controller-loader.d.ts.map +1 -1
  3. package/dist/controller-loader.js +2 -0
  4. package/dist/controller-loader.js.map +1 -1
  5. package/dist/controller-loaders/bundle-builder.d.ts +2 -1
  6. package/dist/controller-loaders/bundle-builder.d.ts.map +1 -1
  7. package/dist/controller-loaders/bundle-builder.js +14 -9
  8. package/dist/controller-loaders/bundle-builder.js.map +1 -1
  9. package/dist/controller-loaders/npm-loader.d.ts +8 -2
  10. package/dist/controller-loaders/npm-loader.d.ts.map +1 -1
  11. package/dist/controller-loaders/npm-loader.js +19 -10
  12. package/dist/controller-loaders/npm-loader.js.map +1 -1
  13. package/dist/controllers/logging/console-sink-controller.d.ts +15 -0
  14. package/dist/controllers/logging/console-sink-controller.d.ts.map +1 -0
  15. package/dist/controllers/logging/console-sink-controller.js +40 -0
  16. package/dist/controllers/logging/console-sink-controller.js.map +1 -0
  17. package/dist/controllers/logging/file-sink-controller.d.ts +11 -0
  18. package/dist/controllers/logging/file-sink-controller.d.ts.map +1 -0
  19. package/dist/controllers/logging/file-sink-controller.js +33 -0
  20. package/dist/controllers/logging/file-sink-controller.js.map +1 -0
  21. package/dist/controllers/logging/sink-identity.d.ts +37 -0
  22. package/dist/controllers/logging/sink-identity.d.ts.map +1 -0
  23. package/dist/controllers/logging/sink-identity.js +40 -0
  24. package/dist/controllers/logging/sink-identity.js.map +1 -0
  25. package/dist/controllers/module/import-controller.d.ts.map +1 -1
  26. package/dist/controllers/module/import-controller.js +25 -0
  27. package/dist/controllers/module/import-controller.js.map +1 -1
  28. package/dist/controllers/resource-definition/resource-definition-controller.d.ts.map +1 -1
  29. package/dist/controllers/resource-definition/resource-definition-controller.js +1 -0
  30. package/dist/controllers/resource-definition/resource-definition-controller.js.map +1 -1
  31. package/dist/evaluation-context.d.ts +27 -1
  32. package/dist/evaluation-context.d.ts.map +1 -1
  33. package/dist/evaluation-context.js +84 -16
  34. package/dist/evaluation-context.js.map +1 -1
  35. package/dist/index.d.ts +4 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +5 -0
  38. package/dist/index.js.map +1 -1
  39. package/dist/internal-context.d.ts +4 -0
  40. package/dist/internal-context.d.ts.map +1 -1
  41. package/dist/kernel.d.ts +26 -0
  42. package/dist/kernel.d.ts.map +1 -1
  43. package/dist/kernel.js +78 -5
  44. package/dist/kernel.js.map +1 -1
  45. package/dist/logging/bootstrap-writer.d.ts +23 -0
  46. package/dist/logging/bootstrap-writer.d.ts.map +1 -0
  47. package/dist/logging/bootstrap-writer.js +31 -0
  48. package/dist/logging/bootstrap-writer.js.map +1 -0
  49. package/dist/logging/color-precedence.d.ts +27 -0
  50. package/dist/logging/color-precedence.d.ts.map +1 -0
  51. package/dist/logging/color-precedence.js +47 -0
  52. package/dist/logging/color-precedence.js.map +1 -0
  53. package/dist/logging/console-sink.d.ts +45 -0
  54. package/dist/logging/console-sink.d.ts.map +1 -0
  55. package/dist/logging/console-sink.js +56 -0
  56. package/dist/logging/console-sink.js.map +1 -0
  57. package/dist/logging/debug-wire-sink.d.ts +38 -0
  58. package/dist/logging/debug-wire-sink.d.ts.map +1 -0
  59. package/dist/logging/debug-wire-sink.js +85 -0
  60. package/dist/logging/debug-wire-sink.js.map +1 -0
  61. package/dist/logging/drop-accounting.d.ts +34 -0
  62. package/dist/logging/drop-accounting.d.ts.map +1 -0
  63. package/dist/logging/drop-accounting.js +98 -0
  64. package/dist/logging/drop-accounting.js.map +1 -0
  65. package/dist/logging/encode-json.d.ts +28 -0
  66. package/dist/logging/encode-json.d.ts.map +1 -0
  67. package/dist/logging/encode-json.js +86 -0
  68. package/dist/logging/encode-json.js.map +1 -0
  69. package/dist/logging/encode-pretty.d.ts +7 -0
  70. package/dist/logging/encode-pretty.d.ts.map +1 -0
  71. package/dist/logging/encode-pretty.js +104 -0
  72. package/dist/logging/encode-pretty.js.map +1 -0
  73. package/dist/logging/file-sink.d.ts +33 -0
  74. package/dist/logging/file-sink.d.ts.map +1 -0
  75. package/dist/logging/file-sink.js +72 -0
  76. package/dist/logging/file-sink.js.map +1 -0
  77. package/dist/logging/index.d.ts +39 -0
  78. package/dist/logging/index.d.ts.map +1 -0
  79. package/dist/logging/index.js +27 -0
  80. package/dist/logging/index.js.map +1 -0
  81. package/dist/logging/kernel-logging.d.ts +110 -0
  82. package/dist/logging/kernel-logging.d.ts.map +1 -0
  83. package/dist/logging/kernel-logging.js +199 -0
  84. package/dist/logging/kernel-logging.js.map +1 -0
  85. package/dist/logging/log-sink.d.ts +9 -0
  86. package/dist/logging/log-sink.d.ts.map +1 -0
  87. package/dist/logging/log-sink.js +8 -0
  88. package/dist/logging/log-sink.js.map +1 -0
  89. package/dist/logging/logging-host.d.ts +14 -0
  90. package/dist/logging/logging-host.d.ts.map +1 -0
  91. package/dist/logging/logging-host.js +27 -0
  92. package/dist/logging/logging-host.js.map +1 -0
  93. package/dist/logging/logging-pipeline.d.ts +89 -0
  94. package/dist/logging/logging-pipeline.d.ts.map +1 -0
  95. package/dist/logging/logging-pipeline.js +349 -0
  96. package/dist/logging/logging-pipeline.js.map +1 -0
  97. package/dist/logging/normalize-attributes.d.ts +48 -0
  98. package/dist/logging/normalize-attributes.d.ts.map +1 -0
  99. package/dist/logging/normalize-attributes.js +140 -0
  100. package/dist/logging/normalize-attributes.js.map +1 -0
  101. package/dist/logging/record-buffer.d.ts +24 -0
  102. package/dist/logging/record-buffer.d.ts.map +1 -0
  103. package/dist/logging/record-buffer.js +78 -0
  104. package/dist/logging/record-buffer.js.map +1 -0
  105. package/dist/logging/redact-attributes.d.ts +45 -0
  106. package/dist/logging/redact-attributes.d.ts.map +1 -0
  107. package/dist/logging/redact-attributes.js +152 -0
  108. package/dist/logging/redact-attributes.js.map +1 -0
  109. package/dist/logging/sampler.d.ts +34 -0
  110. package/dist/logging/sampler.d.ts.map +1 -0
  111. package/dist/logging/sampler.js +52 -0
  112. package/dist/logging/sampler.js.map +1 -0
  113. package/dist/logging/scope-config.d.ts +32 -0
  114. package/dist/logging/scope-config.d.ts.map +1 -0
  115. package/dist/logging/scope-config.js +2 -0
  116. package/dist/logging/scope-config.js.map +1 -0
  117. package/dist/logging/span-id.d.ts +24 -0
  118. package/dist/logging/span-id.d.ts.map +1 -0
  119. package/dist/logging/span-id.js +85 -0
  120. package/dist/logging/span-id.js.map +1 -0
  121. package/dist/logging/to-error-value.d.ts +3 -0
  122. package/dist/logging/to-error-value.d.ts.map +1 -0
  123. package/dist/logging/to-error-value.js +79 -0
  124. package/dist/logging/to-error-value.js.map +1 -0
  125. package/dist/logging/trace-parent.d.ts +54 -0
  126. package/dist/logging/trace-parent.d.ts.map +1 -0
  127. package/dist/logging/trace-parent.js +92 -0
  128. package/dist/logging/trace-parent.js.map +1 -0
  129. package/dist/manifest-schemas.d.ts +1 -1
  130. package/dist/manifest-schemas.d.ts.map +1 -1
  131. package/dist/manifest-schemas.js +12 -0
  132. package/dist/manifest-schemas.js.map +1 -1
  133. package/dist/module-context.d.ts +16 -0
  134. package/dist/module-context.d.ts.map +1 -1
  135. package/dist/module-context.js +6 -0
  136. package/dist/module-context.js.map +1 -1
  137. package/dist/resource-context.d.ts +15 -1
  138. package/dist/resource-context.d.ts.map +1 -1
  139. package/dist/resource-context.js +45 -0
  140. package/dist/resource-context.js.map +1 -1
  141. package/dist/schema-compiled-values.d.ts.map +1 -1
  142. package/dist/schema-compiled-values.js +7 -0
  143. package/dist/schema-compiled-values.js.map +1 -1
  144. package/dist/schema-validator.d.ts +8 -1
  145. package/dist/schema-validator.d.ts.map +1 -1
  146. package/dist/schema-validator.js +13 -3
  147. package/dist/schema-validator.js.map +1 -1
  148. package/package.json +3 -3
  149. package/src/controller-loader.ts +7 -1
  150. package/src/controller-loaders/bundle-builder.ts +18 -10
  151. package/src/controller-loaders/npm-loader.ts +32 -11
  152. package/src/controllers/logging/console-sink-controller.ts +46 -0
  153. package/src/controllers/logging/file-sink-controller.ts +40 -0
  154. package/src/controllers/logging/sink-identity.ts +53 -0
  155. package/src/controllers/module/import-controller.ts +27 -0
  156. package/src/controllers/resource-definition/resource-definition-controller.ts +1 -0
  157. package/src/evaluation-context.ts +93 -15
  158. package/src/index.ts +54 -0
  159. package/src/internal-context.ts +4 -0
  160. package/src/kernel.ts +98 -8
  161. package/src/logging/bootstrap-writer.ts +38 -0
  162. package/src/logging/color-precedence.ts +62 -0
  163. package/src/logging/console-sink.ts +86 -0
  164. package/src/logging/debug-wire-sink.ts +85 -0
  165. package/src/logging/drop-accounting.ts +108 -0
  166. package/src/logging/encode-json.ts +98 -0
  167. package/src/logging/encode-pretty.ts +117 -0
  168. package/src/logging/file-sink.ts +87 -0
  169. package/src/logging/index.ts +65 -0
  170. package/src/logging/kernel-logging.ts +266 -0
  171. package/src/logging/log-sink.ts +18 -0
  172. package/src/logging/logging-host.ts +33 -0
  173. package/src/logging/logging-pipeline.ts +465 -0
  174. package/src/logging/normalize-attributes.ts +228 -0
  175. package/src/logging/record-buffer.ts +72 -0
  176. package/src/logging/redact-attributes.ts +168 -0
  177. package/src/logging/sampler.ts +76 -0
  178. package/src/logging/scope-config.ts +32 -0
  179. package/src/logging/span-id.ts +84 -0
  180. package/src/logging/to-error-value.ts +89 -0
  181. package/src/logging/trace-parent.ts +110 -0
  182. package/src/manifest-schemas.ts +12 -0
  183. package/src/module-context.ts +23 -0
  184. package/src/resource-context.ts +48 -0
  185. package/src/schema-compiled-values.ts +6 -0
  186. package/src/schema-validator.ts +20 -5
@@ -0,0 +1,86 @@
1
+ import type { LogRecord } from "@telorun/sdk";
2
+ import { decideColor, type ColorSetting } from "./color-precedence.js";
3
+ import { encodeJsonLine, type BytesEncoder } from "./encode-json.js";
4
+ import { encodePrettyLine } from "./encode-pretty.js";
5
+ import type { LogSinkInstance } from "./log-sink.js";
6
+
7
+ /**
8
+ * `Telo.ConsoleSink` — a kernel built-in (§10.2).
9
+ *
10
+ * Console and file are built-ins rather than standard-library modules because
11
+ * §16 already requires every conforming runtime to implement both, along with
12
+ * the `pretty` and `json` encodings, byte-identically. Mandatory runtime
13
+ * behaviour belongs in the runtime; shipping it as an installable module would
14
+ * make conformance depend on whether that module happened to be installed.
15
+ *
16
+ * Synchronous by default: a developer-facing stream that silently reorders or
17
+ * drops is worse than a slow one, and `on_full` does not apply to a sink with no
18
+ * buffer to saturate.
19
+ */
20
+
21
+ export type ConsoleEncoding = "auto" | "pretty" | "json";
22
+ export type ConsoleDestination = "stderr" | "stdout";
23
+
24
+ export interface ConsoleSinkOptions {
25
+ sinkId: string;
26
+ level: number;
27
+ destination?: ConsoleDestination;
28
+ encoding?: ConsoleEncoding;
29
+ color?: ColorSetting;
30
+ /** The host environment — capability signals only, never a config channel. */
31
+ env: Record<string, string | undefined>;
32
+ stdout: NodeJS.WritableStream;
33
+ stderr: NodeJS.WritableStream;
34
+ encodeBytes?: BytesEncoder;
35
+ }
36
+
37
+ export class ConsoleSink implements LogSinkInstance {
38
+ readonly sinkId: string;
39
+ readonly level: number;
40
+ /** A file-descriptor write blocks until the bytes are handed to the OS, so a
41
+ * `fatal` record is durable by the time `log()` returns (§10.5). */
42
+ readonly syncFlushable = true;
43
+
44
+ readonly #stream: NodeJS.WritableStream;
45
+ readonly #pretty: boolean;
46
+ readonly #color: boolean;
47
+ readonly #encodeBytes: BytesEncoder | undefined;
48
+
49
+ constructor(options: ConsoleSinkOptions) {
50
+ this.sinkId = options.sinkId;
51
+ this.level = options.level;
52
+ const destination = options.destination ?? "stderr";
53
+ this.#stream = destination === "stdout" ? options.stdout : options.stderr;
54
+
55
+ // `auto` is evaluated against *this sink's* destination descriptor, not the
56
+ // process's: a console sink on stdout and another on stderr can resolve
57
+ // differently, and that is correct.
58
+ const isTTY = Boolean((this.#stream as { isTTY?: boolean }).isTTY);
59
+ const encoding = options.encoding ?? "auto";
60
+ this.#pretty = encoding === "pretty" || (encoding === "auto" && isTTY);
61
+ this.#color = this.#pretty
62
+ ? decideColor({ setting: options.color ?? "auto", env: options.env, isTTY })
63
+ : false;
64
+ this.#encodeBytes = options.encodeBytes;
65
+ }
66
+
67
+ write(record: LogRecord): void {
68
+ this.#stream.write(
69
+ this.#pretty
70
+ ? encodePrettyLine(record, { color: this.#color })
71
+ : encodeJsonLine(record, { encodeBytes: this.#encodeBytes }),
72
+ );
73
+ }
74
+
75
+ async flush(): Promise<void> {
76
+ // Nothing is held: every record is handed to the descriptor on write.
77
+ }
78
+
79
+ flushSync(): void {
80
+ // Same — the write already reached the OS.
81
+ }
82
+
83
+ async close(): Promise<void> {
84
+ // The process owns stdout/stderr; a sink never closes them.
85
+ }
86
+ }
@@ -0,0 +1,85 @@
1
+ import type { LogRecord } from "@telorun/sdk";
2
+ import { toJsonProfile, type BytesEncoder } from "./encode-json.js";
3
+ import type { LogSinkInstance } from "./log-sink.js";
4
+
5
+ /**
6
+ * The `debug-wire` sink — `kernel/specs/logging.md` §10.2 and §11.4.
7
+ *
8
+ * **Not declarable.** It is host-attached when a debug consumer connects
9
+ * (`--debug` / `--inspect`) and detached on disconnect, which makes it tooling
10
+ * attachment rather than application configuration — the same category as TTY
11
+ * detection, not a violation of D6.
12
+ *
13
+ * Not sync-flushable: delivery crosses an SSE/stream boundary owned by the host,
14
+ * so a `fatal` record's flush here is initiated but never awaited (§10.5).
15
+ */
16
+
17
+ export const DEBUG_WIRE_SINK_ID = "<debug-wire>";
18
+
19
+ export class DebugWireSink implements LogSinkInstance {
20
+ readonly sinkId = DEBUG_WIRE_SINK_ID;
21
+ readonly level: number;
22
+ readonly syncFlushable = false;
23
+
24
+ readonly #emit: (frame: { kind: "record"; timestamp: string; record: Record<string, unknown> }) => void;
25
+ readonly #encodeBytes: BytesEncoder | undefined;
26
+
27
+ constructor(options: {
28
+ level: number;
29
+ /** Hands a §11.4 `record` frame to the host's wire writer. */
30
+ emit: (frame: { kind: "record"; timestamp: string; record: Record<string, unknown> }) => void;
31
+ /** Offloads `bytes` attributes to the host's blob store when it has one —
32
+ * the debug wire does. Raw bytes are never inlined (§6.1). */
33
+ encodeBytes?: BytesEncoder;
34
+ }) {
35
+ this.level = options.level;
36
+ this.#emit = options.emit;
37
+ this.#encodeBytes = options.encodeBytes;
38
+ }
39
+
40
+ write(record: LogRecord): void {
41
+ const profile = toJsonProfile(record);
42
+ if (this.#encodeBytes) offloadBytes(profile, this.#encodeBytes);
43
+ this.#emit({
44
+ kind: "record",
45
+ timestamp: new Date(Number(record.timestamp / 1_000_000n)).toISOString(),
46
+ record: profile,
47
+ });
48
+ }
49
+
50
+ async flush(): Promise<void> {
51
+ // Frames are handed to the host synchronously; the host owns delivery.
52
+ }
53
+
54
+ flushSync(): void {
55
+ // Never synchronously drainable — see the class doc.
56
+ }
57
+
58
+ async close(): Promise<void> {}
59
+ }
60
+
61
+ /** Replace `Uint8Array` leaves with the host's pointer form, bounded by the
62
+ * attribute limits already applied upstream. */
63
+ function offloadBytes(value: unknown, encodeBytes: BytesEncoder): void {
64
+ const stack: unknown[] = [value];
65
+ let guard = 0;
66
+ while (stack.length > 0 && guard < 10_000) {
67
+ guard += 1;
68
+ const current = stack.pop();
69
+ if (!current || typeof current !== "object") continue;
70
+ if (Array.isArray(current)) {
71
+ for (let i = 0; i < current.length; i += 1) {
72
+ const item = current[i];
73
+ if (item instanceof Uint8Array) current[i] = encodeBytes(item);
74
+ else stack.push(item);
75
+ }
76
+ continue;
77
+ }
78
+ const record = current as Record<string, unknown>;
79
+ for (const key of Object.keys(record)) {
80
+ const item = record[key];
81
+ if (item instanceof Uint8Array) record[key] = encodeBytes(item);
82
+ else stack.push(item);
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,108 @@
1
+ import type { DropCause } from "./log-sink.js";
2
+
3
+ /**
4
+ * Drop accounting — `kernel/specs/logging.md` §10.4.
5
+ *
6
+ * A runtime maintains a monotonic counter of records dropped per sink, per
7
+ * cause. When drops occur and then cease, it emits exactly one `warn` record
8
+ * reporting the count and the cause. **Dropping without accounting is
9
+ * non-conformant** — "nothing is silently lost" is a design principle, not a
10
+ * nice-to-have, so every drop, truncation, and sink failure is counted and
11
+ * surfaced.
12
+ */
13
+
14
+ /** Sampling drops happen before fan-out, so they are not attributable to any one
15
+ * sink and are counted against the pipeline itself. */
16
+ export const PIPELINE_SINK_ID = "<pipeline>";
17
+
18
+ /** How long a cause must go quiet before its recovery warning is emitted. */
19
+ const QUIESCE_MS = 1000;
20
+
21
+ interface Counter {
22
+ /** Monotonic across the process lifetime; never reset. */
23
+ total: number;
24
+ /** Total as of the last emitted recovery warning. */
25
+ reported: number;
26
+ timer: ReturnType<typeof setTimeout> | undefined;
27
+ }
28
+
29
+ export interface DropReport {
30
+ sinkId: string;
31
+ cause: DropCause;
32
+ /** Records dropped since the previous report. */
33
+ count: number;
34
+ /** Monotonic lifetime total for this (sink, cause) pair. */
35
+ total: number;
36
+ }
37
+
38
+ export class DropRegistry {
39
+ readonly #counters = new Map<string, Counter>();
40
+ readonly #onRecovered: (report: DropReport) => void;
41
+ /** Guards against a recovery warning that itself drops, which would otherwise
42
+ * re-arm the timer forever. */
43
+ #reporting = false;
44
+
45
+ constructor(onRecovered: (report: DropReport) => void) {
46
+ this.#onRecovered = onRecovered;
47
+ }
48
+
49
+ record(sinkId: string, cause: DropCause, count = 1): void {
50
+ const key = `${sinkId}\x00${cause}`;
51
+ let counter = this.#counters.get(key);
52
+ if (!counter) {
53
+ counter = { total: 0, reported: 0, timer: undefined };
54
+ this.#counters.set(key, counter);
55
+ }
56
+ // Always count — a drop caused by emitting the recovery warning itself is
57
+ // still a drop, and undercounting it is exactly the silent loss this class
58
+ // exists to prevent.
59
+ counter.total += count;
60
+
61
+ // The only thing suppressed while a recovery warning is in flight is the
62
+ // timer re-arm: re-arming here would loop forever (the warning's own drop
63
+ // re-arms the timer, which fires and drops again). The increment above keeps
64
+ // the total honest; the next ordinary drop — or `reportPending` at
65
+ // shutdown — surfaces it, since `total !== reported`.
66
+ if (this.#reporting) return;
67
+
68
+ if (counter.timer) clearTimeout(counter.timer);
69
+ counter.timer = setTimeout(() => this.#report(sinkId, cause, counter!), QUIESCE_MS);
70
+ // A pending drop report must never be the reason a process stays alive.
71
+ (counter.timer as { unref?: () => void }).unref?.();
72
+ }
73
+
74
+ /** Lifetime total for a (sink, cause) pair. */
75
+ total(sinkId: string, cause: DropCause): number {
76
+ return this.#counters.get(`${sinkId}\x00${cause}`)?.total ?? 0;
77
+ }
78
+
79
+ /** Emit any outstanding reports immediately — used at shutdown so a run that
80
+ * ends while still dropping does not lose its final accounting. */
81
+ reportPending(): void {
82
+ for (const [key, counter] of this.#counters) {
83
+ if (counter.total === counter.reported) continue;
84
+ const [sinkId, cause] = key.split("\x00") as [string, DropCause];
85
+ this.#report(sinkId, cause, counter);
86
+ }
87
+ }
88
+
89
+ dispose(): void {
90
+ for (const counter of this.#counters.values()) {
91
+ if (counter.timer) clearTimeout(counter.timer);
92
+ counter.timer = undefined;
93
+ }
94
+ }
95
+
96
+ #report(sinkId: string, cause: DropCause, counter: Counter): void {
97
+ counter.timer = undefined;
98
+ const count = counter.total - counter.reported;
99
+ if (count <= 0) return;
100
+ counter.reported = counter.total;
101
+ this.#reporting = true;
102
+ try {
103
+ this.#onRecovered({ sinkId, cause, count, total: counter.total });
104
+ } finally {
105
+ this.#reporting = false;
106
+ }
107
+ }
108
+ }
@@ -0,0 +1,98 @@
1
+ import { formatUnixNano, type AnyValue, type ErrorValue, type LogRecord } from "@telorun/sdk";
2
+
3
+ /**
4
+ * The `json` encoding — `kernel/specs/logging.md` §11.1. One JSON object per
5
+ * line, UTF-8, newline-terminated.
6
+ *
7
+ * No de-facto cross-ecosystem standard exists for key names, so Telo defines its
8
+ * own profile and keeps OTLP as a separate one. Keys are emitted in the §11.1
9
+ * order for diffability, and extension data belongs in `attributes` — unknown
10
+ * top-level keys are never emitted.
11
+ */
12
+
13
+ /** How a `bytes` attribute is rendered. Raw bytes are never inlined into a text
14
+ * encoding: a sink with a blob store offloads them to a pointer, and every
15
+ * other sink base64-encodes (§6.1). */
16
+ export type BytesEncoder = (bytes: Uint8Array) => AnyValue;
17
+
18
+ export const base64Bytes: BytesEncoder = (bytes) => Buffer.from(bytes).toString("base64");
19
+
20
+ export interface JsonEncodeOptions {
21
+ encodeBytes?: BytesEncoder;
22
+ }
23
+
24
+ /** Encode one record as a newline-terminated JSON line. */
25
+ export function encodeJsonLine(record: LogRecord, options: JsonEncodeOptions = {}): string {
26
+ return `${encodeJson(record, options)}\n`;
27
+ }
28
+
29
+ /** Encode one record as a JSON object, without the trailing newline — the shape
30
+ * the debug wire nests inside its `record` frame (§11.4). */
31
+ export function encodeJson(record: LogRecord, options: JsonEncodeOptions = {}): string {
32
+ return JSON.stringify(toJsonProfile(record), makeReplacer(options.encodeBytes ?? base64Bytes));
33
+ }
34
+
35
+ /** The §11.1 key profile as a plain object, in the recommended order. Exposed
36
+ * separately because the debug wire embeds the same profile rather than
37
+ * re-deriving it. */
38
+ export function toJsonProfile(record: LogRecord): Record<string, unknown> {
39
+ const out: Record<string, unknown> = {
40
+ time: formatUnixNano(record.timestamp),
41
+ level: record.severityText.toUpperCase(),
42
+ severity: record.severityNumber,
43
+ msg: record.message,
44
+ };
45
+
46
+ // `observed_timestamp` is deliberately NOT emitted: §11.1 defines a closed key
47
+ // set and forbids extra top-level keys, and §17's schema is
48
+ // `additionalProperties: false`. The field survives on the record model and in
49
+ // the `otlp` profile, which has a real `observedTimeUnixNano` slot for it.
50
+ if (record.traceId !== undefined) out["trace_id"] = record.traceId;
51
+ if (record.spanId !== undefined) out["span_id"] = record.spanId;
52
+ if (record.traceFlags !== undefined) out["trace_flags"] = record.traceFlags;
53
+ if (record.resource !== undefined) out["resource"] = record.resource;
54
+ if (record.module !== undefined) out["module"] = record.module;
55
+ if (record.scope !== undefined) out["scope"] = record.scope;
56
+ // Nested, never flattened — flattening risks collision with reserved keys.
57
+ // Emitted with keys in sorted (code-point) order so the `json` encoding is
58
+ // byte-identical across runtimes (§16 vector 18): attribute keys are arbitrary
59
+ // user data with no natural order, and sorted is the only order both this
60
+ // runtime and a Rust `BTreeMap`-backed one produce without extra config.
61
+ if (record.attributes !== undefined) out["attributes"] = sortKeysDeep(record.attributes);
62
+ if (record.error !== undefined) out["err"] = record.error satisfies ErrorValue;
63
+ if (record.eventName !== undefined) out["event_name"] = record.eventName;
64
+ if (record.droppedAttributesCount) {
65
+ out["dropped_attributes_count"] = record.droppedAttributesCount;
66
+ }
67
+
68
+ return out;
69
+ }
70
+
71
+ /** Rebuild an attribute value with every nested object's keys in sorted order.
72
+ * `JSON.stringify` emits string keys in insertion order, so reconstructing the
73
+ * object with sorted insertion is what pins the byte order. Arrays keep their
74
+ * index order; `Uint8Array` is a leaf. The attributes are already normalized
75
+ * (§6.3), so depth and breadth are bounded and this cannot diverge. */
76
+ function sortKeysDeep(value: AnyValue): AnyValue {
77
+ if (value === null || typeof value !== "object") return value;
78
+ if (value instanceof Uint8Array) return value;
79
+ if (Array.isArray(value)) return value.map(sortKeysDeep);
80
+ const sorted: Record<string, AnyValue> = {};
81
+ for (const key of Object.keys(value).sort()) sorted[key] = sortKeysDeep(value[key]!);
82
+ return sorted;
83
+ }
84
+
85
+ function makeReplacer(encodeBytes: BytesEncoder) {
86
+ return function replacer(this: unknown, _key: string, value: unknown): unknown {
87
+ if (typeof value === "bigint") {
88
+ // Values beyond 2^53 lose precision in a JS receiver, so they degrade to a
89
+ // decimal string rather than to a wrong number — the same reasoning OTLP
90
+ // gives for quoting its 64-bit fields.
91
+ return value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER)
92
+ ? Number(value)
93
+ : value.toString();
94
+ }
95
+ if (value instanceof Uint8Array) return encodeBytes(value);
96
+ return value;
97
+ };
98
+ }
@@ -0,0 +1,117 @@
1
+ import { severityFloor, type AnyValue, type ErrorValue, type LogRecord } from "@telorun/sdk";
2
+
3
+ /**
4
+ * The `pretty` encoding — `kernel/specs/logging.md` §11.2. For humans on a
5
+ * terminal:
6
+ *
7
+ * 12:34:56.789 INFO Http.Server.api listening net.host.port=8080
8
+ *
9
+ * The color table is a modal synthesis across pino-pretty, zap, tint,
10
+ * tracing-subscriber, consola, and winston, which do not fully agree — zap alone
11
+ * uses magenta for DEBUG and blue for INFO, and TRACE has no consensus. It is a
12
+ * deliberate choice, not a standard.
13
+ */
14
+
15
+ const RESET = "\u001b[0m";
16
+
17
+ const LEVEL_COLOR: Readonly<Record<number, string>> = {
18
+ 1: "\u001b[2m", // TRACE — dim
19
+ 5: "\u001b[34m", // DEBUG — blue
20
+ 9: "\u001b[32m", // INFO — green
21
+ 13: "\u001b[33m", // WARN — yellow
22
+ 17: "\u001b[31m", // ERROR — red
23
+ 21: "\u001b[1;41m", // FATAL — bold, red background
24
+ };
25
+
26
+ const DIM = "\u001b[2m";
27
+
28
+ export interface PrettyEncodeOptions {
29
+ color: boolean;
30
+ }
31
+
32
+ export function encodePrettyLine(record: LogRecord, options: PrettyEncodeOptions): string {
33
+ return `${encodePretty(record, options)}\n`;
34
+ }
35
+
36
+ export function encodePretty(record: LogRecord, options: PrettyEncodeOptions): string {
37
+ const paint = options.color
38
+ ? (code: string, text: string) => `${code}${text}${RESET}`
39
+ : (_code: string, text: string) => text;
40
+
41
+ const parts: string[] = [
42
+ formatLocalTime(record.timestamp),
43
+ paint(LEVEL_COLOR[severityFloor(record.severityNumber)]!, record.severityText.padEnd(5)),
44
+ ];
45
+
46
+ if (record.resource) parts.push(`${record.resource.kind}.${record.resource.name}`);
47
+
48
+ // The message itself is never colored by level — only the level token is.
49
+ parts.push(record.message);
50
+
51
+ const attributes = record.attributes;
52
+ if (attributes) {
53
+ for (const [key, value] of Object.entries(attributes)) {
54
+ parts.push(`${paint(DIM, key)}=${formatAttributeValue(value)}`);
55
+ }
56
+ }
57
+
58
+ if (record.droppedAttributesCount) {
59
+ parts.push(`${paint(DIM, "dropped_attributes_count")}=${record.droppedAttributesCount}`);
60
+ }
61
+
62
+ let line = parts.join(" ");
63
+
64
+ if (record.error) line += `\n${formatError(record.error, paint)}`;
65
+
66
+ return line;
67
+ }
68
+
69
+ /** `HH:MM:ss.SSS`, local timezone. Millisecond resolution: the sub-millisecond
70
+ * digits of the record's nanosecond timestamp are noise on a console line. */
71
+ function formatLocalTime(timestamp: bigint): string {
72
+ const date = new Date(Number(timestamp / 1_000_000n));
73
+ const hh = String(date.getHours()).padStart(2, "0");
74
+ const mm = String(date.getMinutes()).padStart(2, "0");
75
+ const ss = String(date.getSeconds()).padStart(2, "0");
76
+ const ms = String(date.getMilliseconds()).padStart(3, "0");
77
+ return `${hh}:${mm}:${ss}.${ms}`;
78
+ }
79
+
80
+ function formatAttributeValue(value: AnyValue): string {
81
+ if (value === null) return "null";
82
+ if (value instanceof Uint8Array) return `<${value.byteLength} bytes>`;
83
+ if (typeof value === "object") return quoteIfNeeded(JSON.stringify(value, jsonSafe));
84
+ return quoteIfNeeded(String(value));
85
+ }
86
+
87
+ /** Values containing whitespace, `"`, or `=` must be quoted so the `key=value`
88
+ * stream stays unambiguously parseable by eye. */
89
+ function quoteIfNeeded(text: string): string {
90
+ return /[\s"=]/.test(text) ? JSON.stringify(text) : text;
91
+ }
92
+
93
+ function jsonSafe(_key: string, value: unknown): unknown {
94
+ if (typeof value === "bigint") return value.toString();
95
+ if (value instanceof Uint8Array) return `<${value.byteLength} bytes>`;
96
+ return value;
97
+ }
98
+
99
+ /** Errors render after the message; the stack goes on following lines,
100
+ * indented, unmodified. The `cause` chain follows, already bounded by §6.3. */
101
+ function formatError(error: ErrorValue, paint: (code: string, text: string) => string): string {
102
+ const lines: string[] = [` ${paint(LEVEL_COLOR[17]!, error.type)}: ${error.message}`];
103
+ if (error.stack) {
104
+ for (const line of error.stack.split("\n")) lines.push(` ${line}`);
105
+ }
106
+ let cause = error.cause;
107
+ let guard = 0;
108
+ while (cause && guard < 16) {
109
+ lines.push(` ${paint(DIM, "caused by")} ${cause.type}: ${cause.message}`);
110
+ if (cause.stack) {
111
+ for (const line of cause.stack.split("\n")) lines.push(` ${line}`);
112
+ }
113
+ cause = cause.cause;
114
+ guard += 1;
115
+ }
116
+ return lines.join("\n");
117
+ }
@@ -0,0 +1,87 @@
1
+ import { closeSync, openSync, writeSync } from "node:fs";
2
+ import type { LogRecord } from "@telorun/sdk";
3
+ import { encodeJsonLine, type BytesEncoder } from "./encode-json.js";
4
+ import { encodePrettyLine } from "./encode-pretty.js";
5
+ import { DEFAULT_BUFFER_POLICY, type LogSinkInstance, type SinkBufferPolicy } from "./log-sink.js";
6
+ import { RecordBuffer } from "./record-buffer.js";
7
+
8
+ /**
9
+ * `Telo.FileSink` — a kernel built-in (§10.2). Asynchronous, `json` by default.
10
+ *
11
+ * Sync-flushable: a positional write is available on every target platform, so a
12
+ * `fatal` record is durable by the time `log()` returns. That is what makes the
13
+ * file sink a legitimate audit destination while an OTLP sink is not — delivery
14
+ * there is a network round-trip that cannot complete without yielding.
15
+ */
16
+
17
+ export type FileEncoding = "json" | "pretty";
18
+
19
+ export interface FileSinkOptions {
20
+ sinkId: string;
21
+ level: number;
22
+ destination: string;
23
+ encoding?: FileEncoding;
24
+ policy?: SinkBufferPolicy;
25
+ onDrop: () => void;
26
+ encodeBytes?: BytesEncoder;
27
+ }
28
+
29
+ export class FileSink implements LogSinkInstance {
30
+ readonly sinkId: string;
31
+ readonly level: number;
32
+ readonly syncFlushable = true;
33
+
34
+ readonly #buffer: RecordBuffer;
35
+ readonly #encode: (record: LogRecord) => string;
36
+ readonly #fd: number;
37
+ #timer: ReturnType<typeof setInterval> | undefined;
38
+ #closed = false;
39
+
40
+ constructor(options: FileSinkOptions) {
41
+ this.sinkId = options.sinkId;
42
+ this.level = options.level;
43
+ const policy = options.policy ?? DEFAULT_BUFFER_POLICY;
44
+ this.#buffer = new RecordBuffer(policy, options.onDrop);
45
+ const encoding = options.encoding ?? "json";
46
+ this.#encode =
47
+ encoding === "pretty"
48
+ ? (record) => encodePrettyLine(record, { color: false })
49
+ : (record) => encodeJsonLine(record, { encodeBytes: options.encodeBytes });
50
+
51
+ this.#fd = openSync(options.destination, "a");
52
+
53
+ this.#timer = setInterval(() => this.flushSync(), policy.flushIntervalMs);
54
+ // A pending flush tick must never be the reason a process stays alive; the
55
+ // shutdown flush is what guarantees the tail is written.
56
+ (this.#timer as { unref?: () => void }).unref?.();
57
+ }
58
+
59
+ write(record: LogRecord): void {
60
+ if (this.#closed) return;
61
+ this.#buffer.push(record);
62
+ }
63
+
64
+ async flush(): Promise<void> {
65
+ this.flushSync();
66
+ }
67
+
68
+ flushSync(): void {
69
+ if (this.#closed) return;
70
+ const records = this.#buffer.drain();
71
+ if (records.length === 0) return;
72
+ let payload = "";
73
+ for (const record of records) payload += this.#encode(record);
74
+ writeSync(this.#fd, payload);
75
+ }
76
+
77
+ async close(): Promise<void> {
78
+ if (this.#closed) return;
79
+ this.flushSync();
80
+ this.#closed = true;
81
+ if (this.#timer) {
82
+ clearInterval(this.#timer);
83
+ this.#timer = undefined;
84
+ }
85
+ closeSync(this.#fd);
86
+ }
87
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Structured logging — the Node.js implementation of `kernel/specs/logging.md`.
3
+ *
4
+ * The record model, severity scale, and `Logger` surface live in `@telorun/sdk`
5
+ * because module authors consume them; everything here is the runtime half —
6
+ * the pipeline, the encodings, the sinks, and the policies that gate emission.
7
+ * The redaction path grammar lives in `@telorun/analyzer` so `telo check` and
8
+ * the runtime share one parser rather than two that can drift.
9
+ */
10
+
11
+ export { BOOTSTRAP_SINK_ID, createBootstrapWriter } from "./bootstrap-writer.js";
12
+ export { decideColor } from "./color-precedence.js";
13
+ export type { ColorDecisionInput, ColorSetting } from "./color-precedence.js";
14
+ export { ConsoleSink } from "./console-sink.js";
15
+ export { DEBUG_WIRE_SINK_ID, DebugWireSink } from "./debug-wire-sink.js";
16
+ export type { ConsoleDestination, ConsoleEncoding, ConsoleSinkOptions } from "./console-sink.js";
17
+ export { DropRegistry, PIPELINE_SINK_ID } from "./drop-accounting.js";
18
+ export type { DropReport } from "./drop-accounting.js";
19
+ export { base64Bytes, encodeJson, encodeJsonLine, toJsonProfile } from "./encode-json.js";
20
+ export type { BytesEncoder, JsonEncodeOptions } from "./encode-json.js";
21
+ export { encodePretty, encodePrettyLine } from "./encode-pretty.js";
22
+ export type { PrettyEncodeOptions } from "./encode-pretty.js";
23
+ export { FileSink } from "./file-sink.js";
24
+ export type { FileEncoding, FileSinkOptions } from "./file-sink.js";
25
+ export {
26
+ BLOCK_UNSUPPORTED,
27
+ blockUnsupportedMessage,
28
+ DEFAULT_BUFFER_POLICY,
29
+ } from "./log-sink.js";
30
+ export type { DropCause, LogSinkInstance, OnFull, SinkBufferPolicy } from "./log-sink.js";
31
+ export { LoggingPipeline, ROOT_SCOPE_CONFIG } from "./logging-pipeline.js";
32
+ export type { PipelineOptions, ScopeConfig, TraceContextProvider } from "./logging-pipeline.js";
33
+ export {
34
+ DEFAULT_ATTRIBUTE_LIMITS,
35
+ normalizeAttributes,
36
+ } from "./normalize-attributes.js";
37
+ export type { AttributeLimits, NormalizedAttributes, NormalizeOptions } from "./normalize-attributes.js";
38
+ export {
39
+ compileRedactionPolicy,
40
+ DEFAULT_CENSOR,
41
+ EMPTY_REDACTION_POLICY,
42
+ redactAttributes,
43
+ redactError,
44
+ } from "./redact-attributes.js";
45
+ export type { CompiledRedactionPath, RedactionPolicy } from "./redact-attributes.js";
46
+ export { RecordBuffer } from "./record-buffer.js";
47
+ export { Sampler } from "./sampler.js";
48
+ export type { SamplingConfig } from "./sampler.js";
49
+ export {
50
+ formatSpanCounter,
51
+ formatSpanId,
52
+ newTraceId,
53
+ normalizeSpanId,
54
+ normalizeTraceId,
55
+ saltSpanId,
56
+ } from "./span-id.js";
57
+ export { toErrorValue } from "./to-error-value.js";
58
+ export {
59
+ formatTraceParent,
60
+ parseTraceParent,
61
+ parseTraceState,
62
+ TRACE_FLAG_RANDOM,
63
+ TRACE_FLAG_SAMPLED,
64
+ } from "./trace-parent.js";
65
+ export type { TraceContext } from "./trace-parent.js";