sentinelayer-cli 0.6.2 → 0.8.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 (159) hide show
  1. package/README.md +996 -996
  2. package/bin/create-sentinelayer.js +5 -5
  3. package/bin/sentinelayer-cli.js +4 -4
  4. package/bin/sl.js +5 -5
  5. package/package.json +64 -63
  6. package/src/agents/jules/config/definition.js +160 -160
  7. package/src/agents/jules/config/system-prompt.js +182 -182
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +17 -17
  10. package/src/agents/jules/loop.js +457 -450
  11. package/src/agents/jules/pulse.js +10 -10
  12. package/src/agents/jules/stream.js +187 -186
  13. package/src/agents/jules/swarm/file-scanner.js +74 -74
  14. package/src/agents/jules/swarm/index.js +11 -11
  15. package/src/agents/jules/swarm/orchestrator.js +362 -362
  16. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  17. package/src/agents/jules/swarm/sub-agent.js +311 -309
  18. package/src/agents/jules/tools/aidenid-email.js +189 -189
  19. package/src/agents/jules/tools/auth-audit.js +1699 -1691
  20. package/src/agents/jules/tools/dispatch.js +340 -335
  21. package/src/agents/jules/tools/file-edit.js +2 -2
  22. package/src/agents/jules/tools/file-read.js +2 -2
  23. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  24. package/src/agents/jules/tools/glob.js +2 -2
  25. package/src/agents/jules/tools/grep.js +2 -2
  26. package/src/agents/jules/tools/index.js +29 -29
  27. package/src/agents/jules/tools/path-guards.js +2 -2
  28. package/src/agents/jules/tools/runtime-audit.js +507 -507
  29. package/src/agents/jules/tools/shell.js +2 -2
  30. package/src/agents/jules/tools/url-policy.js +100 -100
  31. package/src/agents/persona-visuals.js +64 -61
  32. package/src/agents/shared-tools/dispatch-core.js +320 -315
  33. package/src/agents/shared-tools/file-edit.js +180 -180
  34. package/src/agents/shared-tools/file-read.js +100 -100
  35. package/src/agents/shared-tools/glob.js +168 -168
  36. package/src/agents/shared-tools/grep.js +228 -228
  37. package/src/agents/shared-tools/index.js +46 -46
  38. package/src/agents/shared-tools/path-guards.js +161 -161
  39. package/src/agents/shared-tools/shell.js +383 -383
  40. package/src/ai/aidenid.js +1021 -1009
  41. package/src/ai/client.js +553 -553
  42. package/src/ai/domain-target-store.js +268 -268
  43. package/src/ai/identity-store.js +270 -270
  44. package/src/ai/proxy.js +137 -137
  45. package/src/ai/site-store.js +145 -145
  46. package/src/audit/agents/architecture.js +180 -180
  47. package/src/audit/agents/compliance.js +179 -179
  48. package/src/audit/agents/documentation.js +165 -165
  49. package/src/audit/agents/performance.js +145 -145
  50. package/src/audit/agents/security.js +215 -215
  51. package/src/audit/agents/testing.js +172 -172
  52. package/src/audit/orchestrator.js +557 -557
  53. package/src/audit/package.js +204 -204
  54. package/src/audit/registry.js +284 -284
  55. package/src/audit/replay.js +103 -103
  56. package/src/auth/gate.js +400 -371
  57. package/src/auth/http.js +681 -611
  58. package/src/auth/service.js +1106 -1106
  59. package/src/auth/session-store.js +813 -813
  60. package/src/cli.js +257 -252
  61. package/src/commands/ai/identity-lifecycle.js +1338 -1338
  62. package/src/commands/ai/provision-governance.js +1272 -1272
  63. package/src/commands/ai/shared.js +147 -147
  64. package/src/commands/ai.js +11 -11
  65. package/src/commands/apply.js +12 -12
  66. package/src/commands/audit.js +1171 -1166
  67. package/src/commands/auth.js +419 -419
  68. package/src/commands/chat.js +191 -191
  69. package/src/commands/config.js +184 -184
  70. package/src/commands/cost.js +311 -311
  71. package/src/commands/daemon/core.js +850 -850
  72. package/src/commands/daemon/extended.js +1048 -1048
  73. package/src/commands/daemon/shared.js +213 -213
  74. package/src/commands/daemon.js +11 -11
  75. package/src/commands/guide.js +174 -174
  76. package/src/commands/ingest.js +58 -58
  77. package/src/commands/init.js +55 -55
  78. package/src/commands/legacy-args.js +10 -10
  79. package/src/commands/mcp.js +461 -461
  80. package/src/commands/omargate.js +29 -29
  81. package/src/commands/persona.js +20 -20
  82. package/src/commands/plugin.js +260 -260
  83. package/src/commands/policy.js +132 -132
  84. package/src/commands/prompt.js +238 -238
  85. package/src/commands/review.js +704 -704
  86. package/src/commands/scan.js +872 -872
  87. package/src/commands/session.js +590 -0
  88. package/src/commands/spec.js +778 -716
  89. package/src/commands/swarm.js +651 -651
  90. package/src/commands/telemetry.js +202 -202
  91. package/src/commands/watch.js +511 -511
  92. package/src/config/agent-dictionary.js +182 -182
  93. package/src/config/io.js +56 -56
  94. package/src/config/paths.js +18 -18
  95. package/src/config/schema.js +55 -55
  96. package/src/config/service.js +184 -184
  97. package/src/cost/budget.js +235 -235
  98. package/src/cost/history.js +188 -188
  99. package/src/cost/tracker.js +171 -171
  100. package/src/daemon/artifact-lineage.js +534 -534
  101. package/src/daemon/assignment-ledger.js +966 -770
  102. package/src/daemon/ast-parser-layer.js +258 -258
  103. package/src/daemon/budget-governor.js +633 -633
  104. package/src/daemon/callgraph-overlay.js +646 -646
  105. package/src/daemon/error-worker.js +1209 -626
  106. package/src/daemon/fix-cycle.js +384 -377
  107. package/src/daemon/hybrid-mapper.js +929 -929
  108. package/src/daemon/ingest-refresh.js +10 -9
  109. package/src/daemon/jira-lifecycle.js +767 -632
  110. package/src/daemon/operator-control.js +657 -657
  111. package/src/daemon/pulse.js +327 -327
  112. package/src/daemon/reliability-lane.js +471 -471
  113. package/src/daemon/scope-engine.js +1068 -0
  114. package/src/daemon/watchdog.js +971 -971
  115. package/src/events/schema.js +190 -0
  116. package/src/guide/generator.js +316 -316
  117. package/src/ingest/engine.js +918 -918
  118. package/src/interactive/index.js +97 -97
  119. package/src/legacy-cli.js +3161 -2994
  120. package/src/mcp/registry.js +695 -695
  121. package/src/memory/blackboard.js +301 -301
  122. package/src/memory/retrieval.js +581 -581
  123. package/src/plugin/manifest.js +553 -553
  124. package/src/policy/packs.js +144 -144
  125. package/src/prompt/generator.js +136 -118
  126. package/src/review/ai-review.js +679 -679
  127. package/src/review/local-review.js +1351 -1305
  128. package/src/review/omargate-interactive.js +68 -68
  129. package/src/review/omargate-orchestrator.js +404 -300
  130. package/src/review/persona-prompts.js +296 -296
  131. package/src/review/replay.js +235 -235
  132. package/src/review/report.js +664 -664
  133. package/src/review/scan-modes.js +48 -42
  134. package/src/review/spec-binding.js +487 -487
  135. package/src/scaffold/generator.js +67 -67
  136. package/src/scaffold/templates.js +150 -150
  137. package/src/scan/generator.js +418 -418
  138. package/src/scan/gh-secrets.js +107 -107
  139. package/src/session/agent-registry.js +352 -0
  140. package/src/session/daemon.js +801 -0
  141. package/src/session/paths.js +33 -0
  142. package/src/session/runtime-bridge.js +739 -0
  143. package/src/session/store.js +388 -0
  144. package/src/session/stream.js +325 -0
  145. package/src/spec/generator.js +619 -519
  146. package/src/spec/regenerate.js +237 -237
  147. package/src/spec/templates.js +91 -91
  148. package/src/swarm/dashboard.js +247 -247
  149. package/src/swarm/factory.js +363 -363
  150. package/src/swarm/pentest.js +934 -934
  151. package/src/swarm/registry.js +419 -419
  152. package/src/swarm/report.js +158 -158
  153. package/src/swarm/runtime.js +576 -576
  154. package/src/swarm/scenario-dsl.js +272 -272
  155. package/src/telemetry/ledger.js +302 -302
  156. package/src/telemetry/session-tracker.js +234 -234
  157. package/src/telemetry/sync.js +203 -203
  158. package/src/ui/command-hints.js +13 -13
  159. package/src/ui/markdown.js +220 -220
@@ -1,29 +1,29 @@
1
- import { buildLegacyArgs } from "./legacy-args.js";
2
-
3
- export function registerOmarGateCommand(program, invokeLegacy) {
4
- const omargate = program
5
- .command("omargate")
6
- .description("Run local Omar Gate security analysis (deterministic + AI persona swarm)");
7
-
8
- omargate
9
- .command("deep")
10
- .description("Run full Omar Gate deep scan with 22-rule deterministic + multi-persona AI analysis")
11
- .option("--path <path>", "Target repository path")
12
- .option("--output-dir <path>", "Artifact root for report output")
13
- .option("--no-ai", "Skip AI review layer (deterministic only)")
14
- .option("--ai-dry-run", "Run AI layer in dry-run mode (no LLM call)")
15
- .option("--scan-mode <mode>", "Scan depth: baseline (1 persona), deep (6), full-depth (13)")
16
- .option("--max-parallel <n>", "Max concurrent persona calls (default: 4)")
17
- .option("--model <id>", "LLM model override (default: gpt-5.3-codex)")
18
- .option("--provider <name>", "LLM provider: sentinelayer, openai, anthropic, google")
19
- .option("--max-cost <usd>", "Maximum AI layer cost in USD (default: 5.0)")
20
- .option("--stream", "Emit NDJSON events to stdout as personas run")
21
- .option("--json", "Emit machine-readable output")
22
- .action(async (options, command) => {
23
- const legacyArgs = buildLegacyArgs(["/omargate", "deep"], {
24
- commandOptions: options,
25
- command,
26
- });
27
- await invokeLegacy(legacyArgs);
28
- });
29
- }
1
+ import { buildLegacyArgs } from "./legacy-args.js";
2
+
3
+ export function registerOmarGateCommand(program, invokeLegacy) {
4
+ const omargate = program
5
+ .command("omargate")
6
+ .description("Run local Omar Gate security analysis (deterministic + AI persona swarm)");
7
+
8
+ omargate
9
+ .command("deep")
10
+ .description("Run full Omar Gate deep scan with 22-rule deterministic + multi-persona AI analysis")
11
+ .option("--path <path>", "Target repository path")
12
+ .option("--output-dir <path>", "Artifact root for report output")
13
+ .option("--no-ai", "Skip AI review layer (deterministic only)")
14
+ .option("--ai-dry-run", "Run AI layer in dry-run mode (no LLM call)")
15
+ .option("--scan-mode <mode>", "Scan depth: baseline (1 persona), deep (6), full-depth (13)")
16
+ .option("--max-parallel <n>", "Max concurrent persona calls (default: 4)")
17
+ .option("--model <id>", "LLM model override (default: gpt-5.3-codex)")
18
+ .option("--provider <name>", "LLM provider: sentinelayer, openai, anthropic, google")
19
+ .option("--max-cost <usd>", "Maximum AI layer cost in USD (default: 5.0)")
20
+ .option("--stream", "Emit NDJSON events to stdout as personas run")
21
+ .option("--json", "Emit machine-readable output")
22
+ .action(async (options, command) => {
23
+ const legacyArgs = buildLegacyArgs(["/omargate", "deep"], {
24
+ commandOptions: options,
25
+ command,
26
+ });
27
+ await invokeLegacy(legacyArgs);
28
+ });
29
+ }
@@ -1,10 +1,10 @@
1
- import { buildLegacyArgs } from "./legacy-args.js";
2
-
3
- export function registerPersonaCommand(program, invokeLegacy) {
4
- const persona = program
5
- .command("persona")
6
- .description("Generate orchestrator persona context");
7
-
1
+ import { buildLegacyArgs } from "./legacy-args.js";
2
+
3
+ export function registerPersonaCommand(program, invokeLegacy) {
4
+ const persona = program
5
+ .command("persona")
6
+ .description("Generate orchestrator persona context");
7
+
8
8
  persona
9
9
  .command("orchestrator")
10
10
  .description("Generate mode-specific orchestrator report")
@@ -12,16 +12,16 @@ export function registerPersonaCommand(program, invokeLegacy) {
12
12
  .option("--path <path>", "Target repository path")
13
13
  .option("--output-dir <path>", "Artifact root for report output")
14
14
  .option("--json", "Emit machine-readable output")
15
- .action(async (options, command) => {
16
- const legacyArgs = buildLegacyArgs([
17
- "/persona",
18
- "orchestrator",
19
- "--mode",
20
- String(options.mode || "builder"),
21
- ], {
22
- commandOptions: options,
23
- command,
24
- });
25
- await invokeLegacy(legacyArgs);
26
- });
27
- }
15
+ .action(async (options, command) => {
16
+ const legacyArgs = buildLegacyArgs([
17
+ "/persona",
18
+ "orchestrator",
19
+ "--mode",
20
+ String(options.mode || "builder"),
21
+ ], {
22
+ commandOptions: options,
23
+ command,
24
+ });
25
+ await invokeLegacy(legacyArgs);
26
+ });
27
+ }
@@ -1,260 +1,260 @@
1
- import path from "node:path";
2
- import process from "node:process";
3
-
4
- import pc from "picocolors";
5
-
6
- import {
7
- buildPluginManifestTemplate,
8
- computePluginLoadOrder,
9
- listPluginManifests,
10
- normalizePluginId,
11
- normalizePluginLoadStage,
12
- normalizePluginPackType,
13
- PLUGIN_LOAD_STAGES,
14
- PLUGIN_PACK_TYPES,
15
- readJsonFile,
16
- resolveDefaultPluginManifestPath,
17
- summarizePluginValidationError,
18
- validatePluginManifest,
19
- writeJsonFile,
20
- } from "../plugin/manifest.js";
21
-
22
- function shouldEmitJson(options, command) {
23
- const local = Boolean(options && options.json);
24
- const globalFromCommand =
25
- command && command.optsWithGlobals ? Boolean(command.optsWithGlobals().json) : false;
26
- return local || globalFromCommand;
27
- }
28
-
29
- function normalizeOutputPath(rawValue, fallbackPath) {
30
- const candidate = String(rawValue || "").trim();
31
- if (!candidate) {
32
- return fallbackPath;
33
- }
34
- return path.resolve(process.cwd(), candidate);
35
- }
36
-
37
- export function registerPluginCommand(program) {
38
- const plugin = program
39
- .command("plugin")
40
- .description("Manage Sentinelayer plugin manifests and extension-pack contracts");
41
-
42
- plugin
43
- .command("init")
44
- .description("Initialize a deterministic plugin manifest scaffold")
45
- .requiredOption("--id <plugin-id>", "Unique plugin id (lowercase)")
46
- .option(
47
- "--pack-type <type>",
48
- `Pack boundary type (${PLUGIN_PACK_TYPES.join("|")})`,
49
- "plugin"
50
- )
51
- .option(
52
- "--stage <stage>",
53
- `Load-order stage (${PLUGIN_LOAD_STAGES.join("|")})`,
54
- "scan"
55
- )
56
- .option("--path <path>", "Destination file path override")
57
- .option("--output-dir <path>", "Optional artifact output root override")
58
- .option("--force", "Overwrite destination file if it already exists")
59
- .option("--json", "Emit machine-readable output")
60
- .action(async (options, command) => {
61
- const pluginId = normalizePluginId(options.id);
62
- const packType = normalizePluginPackType(options.packType);
63
- const stage = normalizePluginLoadStage(options.stage);
64
- const defaultPath = await resolveDefaultPluginManifestPath({
65
- cwd: process.cwd(),
66
- outputDir: options.outputDir,
67
- env: process.env,
68
- pluginId,
69
- });
70
- const outputPath = normalizeOutputPath(options.path, defaultPath);
71
- const template = buildPluginManifestTemplate({
72
- pluginId,
73
- packType,
74
- stage,
75
- });
76
- const manifest = validatePluginManifest(template);
77
- const writtenPath = await writeJsonFile(outputPath, manifest, { force: Boolean(options.force) });
78
-
79
- const payload = {
80
- command: "plugin init",
81
- pluginId,
82
- packType: manifest.pack_type,
83
- stage: manifest.load_order.stage,
84
- outputPath: writtenPath,
85
- schemaVersion: manifest.schema_version,
86
- kind: manifest.kind,
87
- };
88
- if (shouldEmitJson(options, command)) {
89
- console.log(JSON.stringify(payload, null, 2));
90
- return;
91
- }
92
-
93
- console.log(pc.green(`Wrote plugin manifest: ${writtenPath}`));
94
- console.log(pc.gray(`Plugin id: ${pluginId}`));
95
- console.log(pc.gray(`Pack type: ${manifest.pack_type}`));
96
- console.log(pc.gray(`Stage: ${manifest.load_order.stage}`));
97
- console.log(pc.gray("Next: edit capabilities/security/load_order and validate before use."));
98
- });
99
-
100
- plugin
101
- .command("validate")
102
- .description("Validate a plugin manifest against Sentinelayer contract")
103
- .requiredOption("--file <path>", "Manifest file to validate")
104
- .option("--json", "Emit machine-readable output")
105
- .action(async (options, command) => {
106
- const inputPath = path.resolve(process.cwd(), String(options.file || "").trim());
107
- const loaded = await readJsonFile(inputPath);
108
- try {
109
- const manifest = validatePluginManifest(loaded.data);
110
- const payload = {
111
- command: "plugin validate",
112
- valid: true,
113
- filePath: loaded.path,
114
- pluginId: manifest.id,
115
- version: manifest.version,
116
- packType: manifest.pack_type,
117
- stage: manifest.load_order.stage,
118
- };
119
- if (shouldEmitJson(options, command)) {
120
- console.log(JSON.stringify(payload, null, 2));
121
- return;
122
- }
123
-
124
- console.log(pc.green(`Manifest valid (${manifest.id}@${manifest.version})`));
125
- console.log(pc.gray(`File: ${loaded.path}`));
126
- } catch (error) {
127
- const payload = {
128
- command: "plugin validate",
129
- valid: false,
130
- filePath: loaded.path,
131
- error: summarizePluginValidationError(error),
132
- };
133
- if (shouldEmitJson(options, command)) {
134
- console.log(JSON.stringify(payload, null, 2));
135
- } else {
136
- console.log(pc.red(`Manifest invalid: ${payload.error}`));
137
- console.log(pc.gray(`File: ${loaded.path}`));
138
- }
139
- process.exitCode = 2;
140
- }
141
- });
142
-
143
- plugin
144
- .command("list")
145
- .description("List plugin manifests discovered under local artifact root")
146
- .option("--path <path>", "Workspace path for config resolution", ".")
147
- .option("--output-dir <path>", "Optional artifact output root override")
148
- .option("--json", "Emit machine-readable output")
149
- .action(async (options, command) => {
150
- const targetPath = path.resolve(process.cwd(), String(options.path || "."));
151
- const listing = await listPluginManifests({
152
- cwd: targetPath,
153
- outputDir: options.outputDir,
154
- env: process.env,
155
- });
156
-
157
- const payload = {
158
- command: "plugin list",
159
- pluginsRoot: listing.pluginsRoot,
160
- pluginCount: listing.plugins.length,
161
- invalidCount: listing.invalid.length,
162
- plugins: listing.plugins,
163
- invalid: listing.invalid,
164
- };
165
-
166
- if (shouldEmitJson(options, command)) {
167
- console.log(JSON.stringify(payload, null, 2));
168
- } else {
169
- console.log(pc.bold("Plugin manifests"));
170
- console.log(pc.gray(`Root: ${listing.pluginsRoot}`));
171
- if (!listing.plugins.length && !listing.invalid.length) {
172
- console.log(pc.gray("(no plugin manifests found)"));
173
- return;
174
- }
175
-
176
- for (const pluginEntry of listing.plugins) {
177
- console.log(
178
- `${pluginEntry.id}@${pluginEntry.version} | type=${pluginEntry.packType} | stage=${pluginEntry.stage} | commands=${pluginEntry.commandCount} | templates=${pluginEntry.templateCount} | policies=${pluginEntry.policyCount}`
179
- );
180
- console.log(pc.gray(` ${pluginEntry.path}`));
181
- }
182
-
183
- for (const invalidEntry of listing.invalid) {
184
- console.log(pc.yellow(`invalid manifest: ${invalidEntry.path}`));
185
- console.log(pc.gray(` ${invalidEntry.error}`));
186
- }
187
- }
188
-
189
- if (listing.invalid.length > 0) {
190
- process.exitCode = 2;
191
- }
192
- });
193
-
194
- plugin
195
- .command("order")
196
- .description("Resolve deterministic plugin load order by stage")
197
- .option("--path <path>", "Workspace path for config resolution", ".")
198
- .option(
199
- "--stage <stage>",
200
- `Optional single-stage filter (${PLUGIN_LOAD_STAGES.join("|")})`
201
- )
202
- .option("--output-dir <path>", "Optional artifact output root override")
203
- .option("--json", "Emit machine-readable output")
204
- .action(async (options, command) => {
205
- const targetPath = path.resolve(process.cwd(), String(options.path || "."));
206
- const stage = options.stage ? normalizePluginLoadStage(options.stage) : "";
207
- const ordering = await computePluginLoadOrder({
208
- cwd: targetPath,
209
- outputDir: options.outputDir,
210
- env: process.env,
211
- stage,
212
- });
213
-
214
- const payload = {
215
- command: "plugin order",
216
- pluginsRoot: ordering.pluginsRoot,
217
- invalidCount: ordering.invalidCount,
218
- hasBlockingIssues: ordering.hasBlockingIssues,
219
- stages: ordering.stages,
220
- invalid: ordering.invalid,
221
- };
222
-
223
- if (shouldEmitJson(options, command)) {
224
- console.log(JSON.stringify(payload, null, 2));
225
- } else {
226
- console.log(pc.bold("Plugin load order"));
227
- console.log(pc.gray(`Root: ${ordering.pluginsRoot}`));
228
- for (const stageEntry of ordering.stages) {
229
- console.log(
230
- `${stageEntry.stage}: plugins=${stageEntry.pluginCount} cycle=${stageEntry.cycleDetected ? "yes" : "no"}`
231
- );
232
- if (stageEntry.order.length > 0) {
233
- console.log(pc.gray(` order: ${stageEntry.order.join(" -> ")}`));
234
- }
235
- if (stageEntry.unresolvedReferences.length > 0) {
236
- for (const unresolved of stageEntry.unresolvedReferences) {
237
- console.log(
238
- pc.yellow(
239
- ` unresolved ${unresolved.relation}: ${unresolved.pluginId} -> ${unresolved.dependencyId}`
240
- )
241
- );
242
- }
243
- }
244
- if (stageEntry.cycleNodes.length > 0) {
245
- console.log(pc.red(` cycle nodes: ${stageEntry.cycleNodes.join(", ")}`));
246
- }
247
- }
248
- if (ordering.invalid.length > 0) {
249
- for (const invalidEntry of ordering.invalid) {
250
- console.log(pc.yellow(`invalid manifest: ${invalidEntry.path}`));
251
- console.log(pc.gray(` ${invalidEntry.error}`));
252
- }
253
- }
254
- }
255
-
256
- if (ordering.hasBlockingIssues) {
257
- process.exitCode = 2;
258
- }
259
- });
260
- }
1
+ import path from "node:path";
2
+ import process from "node:process";
3
+
4
+ import pc from "picocolors";
5
+
6
+ import {
7
+ buildPluginManifestTemplate,
8
+ computePluginLoadOrder,
9
+ listPluginManifests,
10
+ normalizePluginId,
11
+ normalizePluginLoadStage,
12
+ normalizePluginPackType,
13
+ PLUGIN_LOAD_STAGES,
14
+ PLUGIN_PACK_TYPES,
15
+ readJsonFile,
16
+ resolveDefaultPluginManifestPath,
17
+ summarizePluginValidationError,
18
+ validatePluginManifest,
19
+ writeJsonFile,
20
+ } from "../plugin/manifest.js";
21
+
22
+ function shouldEmitJson(options, command) {
23
+ const local = Boolean(options && options.json);
24
+ const globalFromCommand =
25
+ command && command.optsWithGlobals ? Boolean(command.optsWithGlobals().json) : false;
26
+ return local || globalFromCommand;
27
+ }
28
+
29
+ function normalizeOutputPath(rawValue, fallbackPath) {
30
+ const candidate = String(rawValue || "").trim();
31
+ if (!candidate) {
32
+ return fallbackPath;
33
+ }
34
+ return path.resolve(process.cwd(), candidate);
35
+ }
36
+
37
+ export function registerPluginCommand(program) {
38
+ const plugin = program
39
+ .command("plugin")
40
+ .description("Manage Sentinelayer plugin manifests and extension-pack contracts");
41
+
42
+ plugin
43
+ .command("init")
44
+ .description("Initialize a deterministic plugin manifest scaffold")
45
+ .requiredOption("--id <plugin-id>", "Unique plugin id (lowercase)")
46
+ .option(
47
+ "--pack-type <type>",
48
+ `Pack boundary type (${PLUGIN_PACK_TYPES.join("|")})`,
49
+ "plugin"
50
+ )
51
+ .option(
52
+ "--stage <stage>",
53
+ `Load-order stage (${PLUGIN_LOAD_STAGES.join("|")})`,
54
+ "scan"
55
+ )
56
+ .option("--path <path>", "Destination file path override")
57
+ .option("--output-dir <path>", "Optional artifact output root override")
58
+ .option("--force", "Overwrite destination file if it already exists")
59
+ .option("--json", "Emit machine-readable output")
60
+ .action(async (options, command) => {
61
+ const pluginId = normalizePluginId(options.id);
62
+ const packType = normalizePluginPackType(options.packType);
63
+ const stage = normalizePluginLoadStage(options.stage);
64
+ const defaultPath = await resolveDefaultPluginManifestPath({
65
+ cwd: process.cwd(),
66
+ outputDir: options.outputDir,
67
+ env: process.env,
68
+ pluginId,
69
+ });
70
+ const outputPath = normalizeOutputPath(options.path, defaultPath);
71
+ const template = buildPluginManifestTemplate({
72
+ pluginId,
73
+ packType,
74
+ stage,
75
+ });
76
+ const manifest = validatePluginManifest(template);
77
+ const writtenPath = await writeJsonFile(outputPath, manifest, { force: Boolean(options.force) });
78
+
79
+ const payload = {
80
+ command: "plugin init",
81
+ pluginId,
82
+ packType: manifest.pack_type,
83
+ stage: manifest.load_order.stage,
84
+ outputPath: writtenPath,
85
+ schemaVersion: manifest.schema_version,
86
+ kind: manifest.kind,
87
+ };
88
+ if (shouldEmitJson(options, command)) {
89
+ console.log(JSON.stringify(payload, null, 2));
90
+ return;
91
+ }
92
+
93
+ console.log(pc.green(`Wrote plugin manifest: ${writtenPath}`));
94
+ console.log(pc.gray(`Plugin id: ${pluginId}`));
95
+ console.log(pc.gray(`Pack type: ${manifest.pack_type}`));
96
+ console.log(pc.gray(`Stage: ${manifest.load_order.stage}`));
97
+ console.log(pc.gray("Next: edit capabilities/security/load_order and validate before use."));
98
+ });
99
+
100
+ plugin
101
+ .command("validate")
102
+ .description("Validate a plugin manifest against Sentinelayer contract")
103
+ .requiredOption("--file <path>", "Manifest file to validate")
104
+ .option("--json", "Emit machine-readable output")
105
+ .action(async (options, command) => {
106
+ const inputPath = path.resolve(process.cwd(), String(options.file || "").trim());
107
+ const loaded = await readJsonFile(inputPath);
108
+ try {
109
+ const manifest = validatePluginManifest(loaded.data);
110
+ const payload = {
111
+ command: "plugin validate",
112
+ valid: true,
113
+ filePath: loaded.path,
114
+ pluginId: manifest.id,
115
+ version: manifest.version,
116
+ packType: manifest.pack_type,
117
+ stage: manifest.load_order.stage,
118
+ };
119
+ if (shouldEmitJson(options, command)) {
120
+ console.log(JSON.stringify(payload, null, 2));
121
+ return;
122
+ }
123
+
124
+ console.log(pc.green(`Manifest valid (${manifest.id}@${manifest.version})`));
125
+ console.log(pc.gray(`File: ${loaded.path}`));
126
+ } catch (error) {
127
+ const payload = {
128
+ command: "plugin validate",
129
+ valid: false,
130
+ filePath: loaded.path,
131
+ error: summarizePluginValidationError(error),
132
+ };
133
+ if (shouldEmitJson(options, command)) {
134
+ console.log(JSON.stringify(payload, null, 2));
135
+ } else {
136
+ console.log(pc.red(`Manifest invalid: ${payload.error}`));
137
+ console.log(pc.gray(`File: ${loaded.path}`));
138
+ }
139
+ process.exitCode = 2;
140
+ }
141
+ });
142
+
143
+ plugin
144
+ .command("list")
145
+ .description("List plugin manifests discovered under local artifact root")
146
+ .option("--path <path>", "Workspace path for config resolution", ".")
147
+ .option("--output-dir <path>", "Optional artifact output root override")
148
+ .option("--json", "Emit machine-readable output")
149
+ .action(async (options, command) => {
150
+ const targetPath = path.resolve(process.cwd(), String(options.path || "."));
151
+ const listing = await listPluginManifests({
152
+ cwd: targetPath,
153
+ outputDir: options.outputDir,
154
+ env: process.env,
155
+ });
156
+
157
+ const payload = {
158
+ command: "plugin list",
159
+ pluginsRoot: listing.pluginsRoot,
160
+ pluginCount: listing.plugins.length,
161
+ invalidCount: listing.invalid.length,
162
+ plugins: listing.plugins,
163
+ invalid: listing.invalid,
164
+ };
165
+
166
+ if (shouldEmitJson(options, command)) {
167
+ console.log(JSON.stringify(payload, null, 2));
168
+ } else {
169
+ console.log(pc.bold("Plugin manifests"));
170
+ console.log(pc.gray(`Root: ${listing.pluginsRoot}`));
171
+ if (!listing.plugins.length && !listing.invalid.length) {
172
+ console.log(pc.gray("(no plugin manifests found)"));
173
+ return;
174
+ }
175
+
176
+ for (const pluginEntry of listing.plugins) {
177
+ console.log(
178
+ `${pluginEntry.id}@${pluginEntry.version} | type=${pluginEntry.packType} | stage=${pluginEntry.stage} | commands=${pluginEntry.commandCount} | templates=${pluginEntry.templateCount} | policies=${pluginEntry.policyCount}`
179
+ );
180
+ console.log(pc.gray(` ${pluginEntry.path}`));
181
+ }
182
+
183
+ for (const invalidEntry of listing.invalid) {
184
+ console.log(pc.yellow(`invalid manifest: ${invalidEntry.path}`));
185
+ console.log(pc.gray(` ${invalidEntry.error}`));
186
+ }
187
+ }
188
+
189
+ if (listing.invalid.length > 0) {
190
+ process.exitCode = 2;
191
+ }
192
+ });
193
+
194
+ plugin
195
+ .command("order")
196
+ .description("Resolve deterministic plugin load order by stage")
197
+ .option("--path <path>", "Workspace path for config resolution", ".")
198
+ .option(
199
+ "--stage <stage>",
200
+ `Optional single-stage filter (${PLUGIN_LOAD_STAGES.join("|")})`
201
+ )
202
+ .option("--output-dir <path>", "Optional artifact output root override")
203
+ .option("--json", "Emit machine-readable output")
204
+ .action(async (options, command) => {
205
+ const targetPath = path.resolve(process.cwd(), String(options.path || "."));
206
+ const stage = options.stage ? normalizePluginLoadStage(options.stage) : "";
207
+ const ordering = await computePluginLoadOrder({
208
+ cwd: targetPath,
209
+ outputDir: options.outputDir,
210
+ env: process.env,
211
+ stage,
212
+ });
213
+
214
+ const payload = {
215
+ command: "plugin order",
216
+ pluginsRoot: ordering.pluginsRoot,
217
+ invalidCount: ordering.invalidCount,
218
+ hasBlockingIssues: ordering.hasBlockingIssues,
219
+ stages: ordering.stages,
220
+ invalid: ordering.invalid,
221
+ };
222
+
223
+ if (shouldEmitJson(options, command)) {
224
+ console.log(JSON.stringify(payload, null, 2));
225
+ } else {
226
+ console.log(pc.bold("Plugin load order"));
227
+ console.log(pc.gray(`Root: ${ordering.pluginsRoot}`));
228
+ for (const stageEntry of ordering.stages) {
229
+ console.log(
230
+ `${stageEntry.stage}: plugins=${stageEntry.pluginCount} cycle=${stageEntry.cycleDetected ? "yes" : "no"}`
231
+ );
232
+ if (stageEntry.order.length > 0) {
233
+ console.log(pc.gray(` order: ${stageEntry.order.join(" -> ")}`));
234
+ }
235
+ if (stageEntry.unresolvedReferences.length > 0) {
236
+ for (const unresolved of stageEntry.unresolvedReferences) {
237
+ console.log(
238
+ pc.yellow(
239
+ ` unresolved ${unresolved.relation}: ${unresolved.pluginId} -> ${unresolved.dependencyId}`
240
+ )
241
+ );
242
+ }
243
+ }
244
+ if (stageEntry.cycleNodes.length > 0) {
245
+ console.log(pc.red(` cycle nodes: ${stageEntry.cycleNodes.join(", ")}`));
246
+ }
247
+ }
248
+ if (ordering.invalid.length > 0) {
249
+ for (const invalidEntry of ordering.invalid) {
250
+ console.log(pc.yellow(`invalid manifest: ${invalidEntry.path}`));
251
+ console.log(pc.gray(` ${invalidEntry.error}`));
252
+ }
253
+ }
254
+ }
255
+
256
+ if (ordering.hasBlockingIssues) {
257
+ process.exitCode = 2;
258
+ }
259
+ });
260
+ }