@sdkurjnk/blackshell 0.4.0 → 0.6.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 (2) hide show
  1. package/dist/cli.js +108 -32
  2. package/package.json +6 -6
package/dist/cli.js CHANGED
@@ -1,19 +1,21 @@
1
1
  #!/usr/bin/env node
2
- // BlackShell Launcher (TS). Owns the CLI: parsing, the command set, the staging
3
- // index and message rendering (docs/Architecture.md §5 hybrid). The C++ engine
4
- // exposes per-unit primitives (check, enshell); the Launcher composes them — e.g.
5
- // `enshell` seals every staged unit by calling the engine's enshell per unit.
2
+ // BlackShell Launcher (TS).
3
+ // Owns the CLI: parsing, the command set, the staging index and message rendering (docs/Architecture.md §5 hybrid).
4
+ // The C++ engine exposes per-unit primitives (check, enshell); the Launcher composes them
5
+ // e.g. `enshell` seals every staged unit by calling the engine's enshell per unit.
6
6
  import { spawnSync } from "node:child_process";
7
7
  import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
8
8
  import { fileURLToPath } from "node:url";
9
9
  import { createRequire } from "node:module";
10
- import { dirname, join } from "node:path";
10
+ import { dirname, join, extname, basename } from "node:path";
11
11
  const here = (p) => fileURLToPath(new URL(p, import.meta.url));
12
12
  const pkg = JSON.parse(readFileSync(here("../package.json"), "utf8"));
13
13
  const USAGE = `blackshell <command> [options]
14
14
 
15
15
  check <source> Gate verdict only (dummy: accept-all)
16
- add <source> [--level N] Stage a unit for sealing (N = strength 1..3, cumulative L1..LN; default 3)
16
+ add <source> [--level N] [--name NAME]
17
+ Stage a unit for sealing (N = strength 1..3, cumulative
18
+ L1..LN; default 3). --name sets the output to NAME.o.
17
19
  drop <source> Unstage a unit
18
20
  list Show staged units and their levels
19
21
  enshell Seal every staged unit -> object files
@@ -31,6 +33,12 @@ export function writeStage(units) {
31
33
  mkdirSync(".blackshell", { recursive: true });
32
34
  writeFileSync(STAGE_FILE, JSON.stringify(units, null, 2) + "\n");
33
35
  }
36
+ // Source extensions the Clang frontend accepts. MVP = C/C++; a new frontend
37
+ // module later just adds its extensions here. Lowercase, with the dot.
38
+ const TARGET_EXTENSIONS = [".c", ".cpp", ".cc", ".cxx"];
39
+ export function isTargetSource(path) {
40
+ return TARGET_EXTENSIONS.includes(extname(path).toLowerCase());
41
+ }
34
42
  const MAX_LEVEL = 3; // L1..L3 are the noising dials defined in the plan §6.
35
43
  // User-facing dial "N" -> the cumulative set {L1..LN} the engine consumes.
36
44
  // The levels stay an orthogonal set in the engine/harness (§9 per-pass toggle);
@@ -49,10 +57,10 @@ export function showLevels(levels) {
49
57
  }
50
58
  return levels.join(",");
51
59
  }
52
- // Resolve the engine binary. The published package pulls the matching engine as an
53
- // optional dependency @sdkurjnk/blackshell-<platform>-<arch> (os/cpu-gated, so npm
54
- // installs only the host's); resolve its binary. A local dev build in build/ is the
55
- // fallback when no platform package is installed.
60
+ // Resolve the engine binary.
61
+ // The published package pulls the matching engine as an optional dependency @sdkurjnk/blackshell-<platform>-<arch>
62
+ // (os/cpu-gated, so npm installs only the host's);
63
+ // resolve its binary. A local dev build in build/ is the fallback when no platform package is installed.
56
64
  function enginePath() {
57
65
  const exe = process.platform === "win32" ? "blackshell-engine.exe" : "blackshell-engine";
58
66
  const pkg = `@sdkurjnk/blackshell-${process.platform}-${process.arch}`;
@@ -75,44 +83,45 @@ function engine(args) {
75
83
  const token = (res.stdout ?? "").trim().split("\n").pop() ?? "";
76
84
  return { token, status: res.status ?? 1 };
77
85
  }
78
- // engine token -> rendered message. The engine never renders.
79
- export function render(token) {
80
- const [key, ...a] = token.split(" ");
81
- switch (key) {
86
+ // engine code -> rendered message. The engine emits a bare token;
87
+ // we fill the source and object paths from the staged unit (ctx),
88
+ // so the engine never has to echo them. The engine never renders.
89
+ export function render(token, ctx) {
90
+ switch (token) {
82
91
  case "gate.ok": {
83
- return { line: `pass: ${a[0]} is eligible`, err: false };
92
+ return { line: `pass: ${ctx.src} is eligible`, err: false };
84
93
  }
85
94
  case "gate.reject": {
86
- return { line: `reject: ${a[0]} — ineligible`, err: true };
95
+ return { line: `reject: ${ctx.src} — ineligible`, err: true };
87
96
  }
88
97
  case "sealed": {
89
- return { line: `sealed: ${a[0]} -> ${a[1]}`, err: false };
98
+ return { line: `sealed: ${ctx.src} -> ${ctx.obj}`, err: false };
90
99
  }
91
100
  case "frontend.failed": {
92
- return { line: `error: clang (frontend) failed on ${a[0]}`, err: true };
101
+ return { line: `error: clang (frontend) failed on ${ctx.src}`, err: true };
93
102
  }
94
103
  case "backend.failed": {
95
- return { line: `error: llc (backend) failed on ${a[0]}`, err: true };
104
+ return { line: `error: llc (backend) failed on ${ctx.src}`, err: true };
96
105
  }
97
106
  case "verify.broken": {
98
- return { line: `error: noising broke equivalence on ${a[0]}`, err: true };
107
+ return { line: `error: noising broke equivalence on ${ctx.src}`, err: true };
99
108
  }
100
109
  case "arg.one_positional": {
101
110
  return { line: `error: expected exactly one <source> argument`, err: true };
102
111
  }
103
112
  case "cmd.unknown": {
104
- return { line: `error: unknown command: ${a[0]}`, err: true };
113
+ return { line: `error: unknown command`, err: true };
105
114
  }
106
115
  default: {
107
116
  return { line: token, err: true };
108
117
  }
109
118
  }
110
119
  }
111
- function emit(token) {
120
+ function emit(token, ctx) {
112
121
  if (!token) {
113
122
  return;
114
123
  }
115
- const { line, err } = render(token);
124
+ const { line, err } = render(token, ctx);
116
125
  if (err) {
117
126
  console.error(line);
118
127
  }
@@ -135,28 +144,64 @@ export function onePositional(rest) {
135
144
  }
136
145
  return positionals[0];
137
146
  }
138
- // engine argv for sealing one unit; forwards the staged levels (empty = L0).
147
+ // First leftover "--flag" once the command's known options are popped off.
148
+ // A command calls this on its remaining args to reject typos/unsupported flags
149
+ // instead of silently dropping them (onePositional filters "--" tokens out).
150
+ export function strayOption(rest) {
151
+ return rest.find((a) => a.startsWith("--"));
152
+ }
153
+ // engine argv for sealing one unit; forwards the staged levels (empty = L0) and,
154
+ // if named, the output path (engine owns file I/O; we pass an opaque path).
139
155
  export function enshellArgs(u) {
140
156
  const args = ["enshell", u.path];
141
157
  if (u.levels.length > 0) {
142
158
  args.push("--level", u.levels.join(","));
143
159
  }
160
+ if (u.name !== undefined) {
161
+ args.push("--out", `${u.name}.o`);
162
+ }
144
163
  return args;
145
164
  }
165
+ // The object path the engine writes for a unit, mirroring the engine's default(<stem>.o):
166
+ // the --name stem if set, else the source's basename stem.
167
+ // Used to render the sealed line, since the engine no longer echoes the path.
168
+ export function objOf(u) {
169
+ const stem = u.name ?? basename(u.path, extname(u.path));
170
+ return `${stem}.o`;
171
+ }
146
172
  // --- commands ---
147
173
  function cmdCheck(rest) {
174
+ const stray = strayOption(rest);
175
+ if (stray !== undefined) {
176
+ console.error(`error: unknown option: ${stray}`);
177
+ return 1;
178
+ }
148
179
  const src = onePositional(rest);
149
180
  if (src === undefined) {
150
181
  console.error("error: expected exactly one <source> argument");
151
182
  return 1;
152
183
  }
184
+ if (!existsSync(src)) {
185
+ console.error(`error: file not found: ${src}`);
186
+ return 1;
187
+ }
188
+ if (!isTargetSource(src)) {
189
+ console.error(`error: unsupported source type: ${src} (target: ${TARGET_EXTENSIONS.join(" ")})`);
190
+ return 1;
191
+ }
153
192
  const { token, status } = engine(["check", src]);
154
- emit(token);
193
+ emit(token, { src });
155
194
  return status;
156
195
  }
157
196
  function cmdAdd(rest) {
158
- const opt = popOption(rest, "--level");
159
- const src = onePositional(opt.rest);
197
+ const levelOpt = popOption(rest, "--level");
198
+ const nameOpt = popOption(levelOpt.rest, "--name");
199
+ const stray = strayOption(nameOpt.rest);
200
+ if (stray !== undefined) {
201
+ console.error(`error: unknown option: ${stray}`);
202
+ return 1;
203
+ }
204
+ const src = onePositional(nameOpt.rest);
160
205
  if (src === undefined) {
161
206
  console.error("error: expected exactly one <source> argument");
162
207
  return 1;
@@ -165,18 +210,48 @@ function cmdAdd(rest) {
165
210
  console.error(`error: file not found: ${src}`);
166
211
  return 1;
167
212
  }
168
- const levels = expandLevel(opt.value);
213
+ if (!isTargetSource(src)) {
214
+ console.error(`error: unsupported source type: ${src} (target: ${TARGET_EXTENSIONS.join(" ")})`);
215
+ return 1;
216
+ }
217
+ const levels = expandLevel(levelOpt.value);
169
218
  if (levels === null) {
170
219
  console.error(`error: --level must be an integer 1..${MAX_LEVEL}`);
171
220
  return 1;
172
221
  }
222
+ // --name was given but swallowed the next flag / had no value.
223
+ if (rest.includes("--name") && (nameOpt.value === undefined || nameOpt.value === "" || nameOpt.value.startsWith("--"))) {
224
+ console.error("error: --name requires a value");
225
+ return 1;
226
+ }
227
+ // --name is a bare object stem, not a path:
228
+ // reject separators so the output can't escape the working dir (we hand it to the engine as the object path).
229
+ if (nameOpt.value !== undefined && /[/\\]/.test(nameOpt.value)) {
230
+ console.error("error: --name must be a bare filename (no path separators)");
231
+ return 1;
232
+ }
233
+ const unit = { path: src, levels };
234
+ if (nameOpt.value !== undefined) {
235
+ unit.name = nameOpt.value;
236
+ }
173
237
  const units = readStage().filter((u) => u.path !== src);
174
- units.push({ path: src, levels });
238
+ // Two units sharing an output name would clobber each other at enshell time.
239
+ if (unit.name !== undefined && units.some((u) => u.name === unit.name)) {
240
+ console.error(`error: output name already staged: ${unit.name}.o`);
241
+ return 1;
242
+ }
243
+ units.push(unit);
175
244
  writeStage(units);
176
- console.log(`staged: ${src} [${showLevels(levels)}]`);
245
+ const out = unit.name === undefined ? "" : ` -> ${unit.name}.o`;
246
+ console.log(`staged: ${src} [${showLevels(levels)}]${out}`);
177
247
  return 0;
178
248
  }
179
249
  function cmdDrop(rest) {
250
+ const stray = strayOption(rest);
251
+ if (stray !== undefined) {
252
+ console.error(`error: unknown option: ${stray}`);
253
+ return 1;
254
+ }
180
255
  const src = onePositional(rest);
181
256
  if (src === undefined) {
182
257
  console.error("error: expected exactly one <source> argument");
@@ -199,7 +274,8 @@ function cmdList() {
199
274
  return 0;
200
275
  }
201
276
  for (const u of units) {
202
- console.log(` ${u.path} [${showLevels(u.levels)}]`);
277
+ const out = u.name === undefined ? "" : ` -> ${u.name}.o`;
278
+ console.log(` ${u.path} [${showLevels(u.levels)}]${out}`);
203
279
  }
204
280
  return 0;
205
281
  }
@@ -212,7 +288,7 @@ function cmdEnshell() {
212
288
  let code = 0;
213
289
  for (const u of units) {
214
290
  const { token, status } = engine(enshellArgs(u));
215
- emit(token);
291
+ emit(token, { src: u.path, obj: objOf(u) });
216
292
  if (status !== 0) {
217
293
  code = status;
218
294
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdkurjnk/blackshell",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Build-stage protection tool that seals critical code regions at the compiler IR level, communicating with the outside only by value.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -45,10 +45,10 @@
45
45
  "typescript": "^7.0.2"
46
46
  },
47
47
  "optionalDependencies": {
48
- "@sdkurjnk/blackshell-darwin-arm64": "0.4.0",
49
- "@sdkurjnk/blackshell-darwin-x64": "0.4.0",
50
- "@sdkurjnk/blackshell-linux-arm64": "0.4.0",
51
- "@sdkurjnk/blackshell-linux-x64": "0.4.0",
52
- "@sdkurjnk/blackshell-win32-x64": "0.4.0"
48
+ "@sdkurjnk/blackshell-darwin-arm64": "0.6.0",
49
+ "@sdkurjnk/blackshell-darwin-x64": "0.6.0",
50
+ "@sdkurjnk/blackshell-linux-arm64": "0.6.0",
51
+ "@sdkurjnk/blackshell-linux-x64": "0.6.0",
52
+ "@sdkurjnk/blackshell-win32-x64": "0.6.0"
53
53
  }
54
54
  }