@unrdf/kgc-probe 26.4.2

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.
package/src/config.mjs ADDED
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * @fileoverview Configuration parsing and validation for KGC Probe
4
+ *
5
+ * Centralizes config logic with Zod validation for type safety.
6
+ *
7
+ * Design principles:
8
+ * - Fail fast: Invalid config = immediate error
9
+ * - Defaults: Sensible defaults for all options
10
+ * - Composable: Can be used by CLI, API, or tests
11
+ * - Validated: Zod schemas ensure type correctness
12
+ */
13
+
14
+ import { z } from 'zod';
15
+ import { resolve, normalize } from 'node:path';
16
+
17
+ /**
18
+ * Probe configuration schema
19
+ */
20
+ export const ProbeConfigSchema = z.object({
21
+ // Output directory for probe results
22
+ outputDir: z.string().default('./kgc-output'),
23
+
24
+ // Allowed filesystem roots (sandboxing)
25
+ allowedRoots: z.array(z.string()).default([process.cwd()]),
26
+
27
+ // Allowed network hosts (for network probes)
28
+ allowedHosts: z.array(z.string()).default([]),
29
+
30
+ // Time budget per agent (ms)
31
+ budgetMs: z.number().int().positive().default(5000),
32
+
33
+ // Benchmark sample count
34
+ samples: z.number().int().positive().default(10),
35
+
36
+ // Enforce timeouts
37
+ enforceTimeouts: z.boolean().default(true),
38
+
39
+ // Maximum timeout limit (ms)
40
+ maxTimeoutMs: z.number().int().positive().default(50000),
41
+
42
+ // Probe version
43
+ probeVersion: z.string().default('1.0.0')
44
+ });
45
+
46
+ /**
47
+ * Validated probe configuration
48
+ *
49
+ * @typedef {z.infer<typeof ProbeConfigSchema>} ProbeConfig
50
+ */
51
+
52
+ /**
53
+ * Parse and validate CLI options into ProbeConfig
54
+ *
55
+ * @param {Object} options - Raw CLI options
56
+ * @param {string} options.out - Output directory
57
+ * @param {string[]} options.root - Allowed root paths
58
+ * @param {string[]} [options.netAllow] - Allowed network hosts
59
+ * @param {number} options.budgetMs - Time budget (ms)
60
+ * @param {number} options.samples - Benchmark samples
61
+ * @returns {ProbeConfig}
62
+ */
63
+ export function parseConfig(options) {
64
+ const config = {
65
+ outputDir: options.out,
66
+ allowedRoots: (options.root || [process.cwd()]).map(r => resolve(normalize(r))),
67
+ allowedHosts: options.netAllow || [],
68
+ budgetMs: options.budgetMs,
69
+ samples: options.samples,
70
+ enforceTimeouts: true,
71
+ maxTimeoutMs: (options.budgetMs || 5000) * 10,
72
+ probeVersion: '1.0.0'
73
+ };
74
+
75
+ return ProbeConfigSchema.parse(config);
76
+ }
77
+
78
+ /**
79
+ * Validate ProbeConfig
80
+ *
81
+ * @param {unknown} config - Config to validate
82
+ * @returns {ProbeConfig}
83
+ * @throws {z.ZodError} if validation fails
84
+ */
85
+ export function validateConfig(config) {
86
+ return ProbeConfigSchema.parse(config);
87
+ }
88
+
89
+ /**
90
+ * Create default ProbeConfig
91
+ *
92
+ * @returns {ProbeConfig}
93
+ */
94
+ export function createDefaultConfig() {
95
+ return ProbeConfigSchema.parse({});
96
+ }
97
+
98
+ /**
99
+ * Merge configs (right overrides left)
100
+ *
101
+ * @param {Partial<ProbeConfig>} base - Base config
102
+ * @param {Partial<ProbeConfig>} override - Override config
103
+ * @returns {ProbeConfig}
104
+ */
105
+ export function mergeConfigs(base, override) {
106
+ return ProbeConfigSchema.parse({ ...base, ...override });
107
+ }
108
+
109
+ export default {
110
+ ProbeConfigSchema,
111
+ parseConfig,
112
+ validateConfig,
113
+ createDefaultConfig,
114
+ mergeConfigs
115
+ };