@native-sdk/cli 0.5.0 → 0.5.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.
Files changed (60) hide show
  1. package/build/app.zig +198 -13
  2. package/build/ts_run.mjs +58 -19
  3. package/build.zig +42 -0
  4. package/package.json +13 -9
  5. package/packages/core/package-lock.json +4 -3
  6. package/packages/core/package.json +3 -2
  7. package/packages/core/src/typed_ast.ts +11 -1
  8. package/skill-data/native-ui/SKILL.md +2 -2
  9. package/src/platform/types.zig +9 -0
  10. package/src/platform/windows/webview2_host.cpp +24 -0
  11. package/src/primitives/canvas/layout_audit.zig +5 -5
  12. package/src/primitives/canvas/root.zig +6 -0
  13. package/src/primitives/canvas/tokens.zig +185 -7
  14. package/src/primitives/canvas/ui.zig +27 -5
  15. package/src/primitives/canvas/ui_markup.zig +27 -4
  16. package/src/primitives/canvas/ui_markup_compiled.zig +9 -1
  17. package/src/primitives/canvas/ui_markup_compiled_tests.zig +127 -0
  18. package/src/primitives/canvas/ui_markup_tests.zig +47 -0
  19. package/src/primitives/canvas/ui_markup_view.zig +10 -1
  20. package/src/primitives/canvas/ui_markup_view_tests.zig +78 -0
  21. package/src/primitives/canvas/ui_schema.zig +15 -1
  22. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  23. package/src/primitives/canvas/widget_builtin_tests.zig +439 -5
  24. package/src/primitives/canvas/widget_layout.zig +142 -3
  25. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  26. package/src/primitives/canvas/widget_metrics.zig +16 -8
  27. package/src/primitives/canvas/widget_tree.zig +14 -2
  28. package/src/primitives/canvas/widgets.zig +12 -0
  29. package/src/runtime/automation_widget_dispatch.zig +95 -28
  30. package/src/runtime/canvas_frame.zig +15 -0
  31. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  32. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  33. package/src/runtime/canvas_widget_events.zig +1271 -3
  34. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  35. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  36. package/src/runtime/canvas_widget_state.zig +82 -5
  37. package/src/runtime/core.zig +30 -0
  38. package/src/runtime/effects.zig +1037 -133
  39. package/src/runtime/effects_fetch_tests.zig +42 -0
  40. package/src/runtime/effects_file_tests.zig +220 -0
  41. package/src/runtime/effects_tests.zig +287 -0
  42. package/src/runtime/flow.zig +24 -1
  43. package/src/runtime/gpu_surface_events.zig +44 -5
  44. package/src/runtime/session_journal.zig +25 -13
  45. package/src/runtime/session_record.zig +166 -0
  46. package/src/runtime/session_tests.zig +453 -0
  47. package/src/runtime/ui_app.zig +137 -39
  48. package/src/runtime/ui_app_tests.zig +681 -0
  49. package/src/runtime/ui_app_window_tests.zig +182 -0
  50. package/src/runtime/view.zig +86 -0
  51. package/src/runtime/view_widget_text.zig +18 -1
  52. package/src/runtime/view_widget_tree.zig +339 -20
  53. package/src/runtime/widget_bridge.zig +26 -0
  54. package/src/runtime/window_storage.zig +61 -15
  55. package/src/runtime/window_view_runtime.zig +13 -0
  56. package/src/runtime/window_views.zig +6 -0
  57. package/src/tooling/package.zig +311 -8
  58. package/src/tooling/templates.zig +97 -9
  59. package/src/tooling/ts_core.zig +626 -13
  60. package/src/tooling/verbs.zig +88 -2
@@ -534,11 +534,18 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
534
534
  for (self.views[0..self.view_count], 0..) |*view, view_index| {
535
535
  if (view.window_id != window_id) continue;
536
536
  const previous_state = view.canvasWidgetRenderState();
537
+ const was_focused = view.focused;
537
538
  view.focused = std.mem.eql(u8, view.label, label);
538
539
  const next_state = view.canvasWidgetRenderState();
539
540
  if (!CanvasWidgetEventMethods.canvasWidgetRenderStatesEqual(previous_state, next_state)) {
540
541
  try CanvasWidgetEventMethods.invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, next_state);
541
542
  }
543
+ // A view losing focus drops its tooltip state and
544
+ // re-stamps hidden — no stale tooltip may keep floating
545
+ // in a view the keyboard just left.
546
+ if (was_focused and !view.focused) {
547
+ try CanvasWidgetEventMethods.resetCanvasTooltipIntentForViewBlur(self, view_index);
548
+ }
542
549
  }
543
550
  for (self.webviews[0..self.webview_count]) |*webview| {
544
551
  if (webview.window_id == window_id) webview.focused = std.mem.eql(u8, webview.label, label);
@@ -552,11 +559,17 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
552
559
  for (self.views[0..self.view_count], 0..) |*view, view_index| {
553
560
  if (view.window_id != window_id) continue;
554
561
  const previous_state = view.canvasWidgetRenderState();
562
+ const was_focused = view.focused;
555
563
  view.focused = false;
556
564
  const next_state = view.canvasWidgetRenderState();
557
565
  if (!CanvasWidgetEventMethods.canvasWidgetRenderStatesEqual(previous_state, next_state)) {
558
566
  try CanvasWidgetEventMethods.invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, next_state);
559
567
  }
568
+ // Window-level focus loss blurs every view: same tooltip
569
+ // reset as a per-view focus move.
570
+ if (was_focused) {
571
+ try CanvasWidgetEventMethods.resetCanvasTooltipIntentForViewBlur(self, view_index);
572
+ }
560
573
  }
561
574
  for (self.webviews[0..self.webview_count]) |*webview| {
562
575
  if (webview.window_id == window_id) webview.focused = false;
@@ -105,6 +105,12 @@ pub fn RuntimeWindowViews(comptime Runtime: type) type {
105
105
  // open=false event, and the open->closed TRANSITION — which
106
106
  // dispatches the `window_closed` app event — must stay
107
107
  // reserved for closes the app did not initiate.
108
+ // The `focused` flip deliberately bypasses the
109
+ // `setWindowFocused` seam (see window_storage.zig): on
110
+ // success the window's views — and any tooltip state in
111
+ // them — are removed below, and the rollback on platform
112
+ // failure must not have fired a key-loss tooltip reset for
113
+ // a window that never lost key.
108
114
  const was_open = self.windows[index].info.open;
109
115
  const was_focused = self.windows[index].info.focused;
110
116
  self.windows[index].info.open = false;
@@ -84,6 +84,15 @@ pub const PackageOptions = struct {
84
84
  env_map: ?*std.process.Environ.Map = null,
85
85
  };
86
86
 
87
+ /// The Windows subsystem verdict the packaging posture check reaches:
88
+ /// `gui` and `console` only when the PE optional header actually said
89
+ /// so, `unknown` when the probe proved nothing — non-PE or truncated
90
+ /// bytes, a headerless image, an `e_lfanew` past the header ceiling, a
91
+ /// benign read error, or some other declared subsystem entirely
92
+ /// (native, EFI, ...). No surface may claim a subsystem the parse
93
+ /// never established.
94
+ pub const WindowsSubsystem = enum { gui, console, unknown };
95
+
87
96
  pub const PackageStats = struct {
88
97
  path: []const u8,
89
98
  artifact_name: []const u8 = "",
@@ -98,6 +107,16 @@ pub const PackageStats = struct {
98
107
  web_engine: WebEngine = .system,
99
108
  web_layer: ?manifest_tool.WebLayer = null,
100
109
  archive_path: ?[]const u8 = null,
110
+ /// The Windows subsystem verdict: `console` when the package
111
+ /// wrapped a CONSOLE-subsystem exe (the app will flash a terminal
112
+ /// window behind itself on every launch), `gui` for a GUI-subsystem
113
+ /// exe, `unknown` when the probe could not establish either, and
114
+ /// null when the check never ran (non-Windows target, or no binary
115
+ /// to probe). Release-shaped `native build` exes are GUI-subsystem,
116
+ /// so console only fires for stale or hand-supplied binaries — the
117
+ /// packager warns and the report carries the truth (pinned by tests
118
+ /// over a synthetic PE header).
119
+ windows_subsystem: ?WindowsSubsystem = null,
101
120
  };
102
121
 
103
122
  /// The web-layer verdict for a package, from the same declare-to-use
@@ -182,6 +201,13 @@ pub fn printDiagnostic(stats: PackageStats) void {
182
201
  if (stats.signing_verified) {
183
202
  std.debug.print(" signing: {s} (signed, verified)\n", .{@tagName(stats.signing_mode)});
184
203
  }
204
+ if (stats.windows_subsystem) |subsystem| {
205
+ switch (subsystem) {
206
+ .console => std.debug.print(" subsystem: console (a terminal window opens behind the app - rebuild with `native build`)\n", .{}),
207
+ .gui => std.debug.print(" subsystem: gui\n", .{}),
208
+ .unknown => std.debug.print(" subsystem: unknown (unrecognized executable format)\n", .{}),
209
+ }
210
+ }
185
211
  if (stats.archive_path) |archive| {
186
212
  std.debug.print(" archive: {s}\n", .{archive});
187
213
  }
@@ -228,7 +254,7 @@ pub fn createMacosApp(allocator: std.mem.Allocator, io: std.Io, options: Package
228
254
  const bundle_stats = try assets_tool.bundle(allocator, io, options.assets_dir, assets_output);
229
255
  try copyMacosIcon(allocator, io, package_dir, options);
230
256
  try copyMacosDocumentIcons(allocator, io, package_dir, options.metadata);
231
- try writeReport(allocator, package_dir, io, "Contents/Resources/package-manifest.zon", options, executable_name, bundle_stats.asset_count);
257
+ try writeReport(allocator, package_dir, io, "Contents/Resources/package-manifest.zon", options, executable_name, bundle_stats.asset_count, null);
232
258
  if (options.web_engine == .chromium) {
233
259
  try cef.ensureLayout(io, options.cef_dir);
234
260
  try copyMacosCefRuntime(allocator, io, package_dir, options.cef_dir);
@@ -295,6 +321,21 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
295
321
  }
296
322
  }
297
323
  }
324
+ // The subsystem posture check: `native build` produces GUI-subsystem
325
+ // release exes on Windows, so a console-subsystem exe here is a stale
326
+ // zig-out binary or a hand-supplied --binary — packaged as-is it
327
+ // flashes a terminal window behind the app on every launch. Warn and
328
+ // carry the truth in the stats/report; the package still builds
329
+ // (the app works, it just looks unfinished). Null means the check
330
+ // never ran: a non-Windows target, or no binary to probe.
331
+ const windows_subsystem: ?WindowsSubsystem = if (options.target == .windows)
332
+ if (options.binary_path) |binary_path| try peSubsystemVerdictAtPath(allocator, io, binary_path) else null
333
+ else
334
+ null;
335
+ if (windows_subsystem == .console) {
336
+ std.debug.print("warning[package.console-subsystem]: {s} is a console-subsystem exe, so a terminal window will open behind the app on every launch\n" ++
337
+ " rebuild with `native build` (release exes are GUI-subsystem) or pass a GUI-subsystem --binary\n", .{options.binary_path.?});
338
+ }
298
339
  if (options.binary_path) |binary_path| {
299
340
  const binary_subpath = try std.fmt.allocPrint(allocator, "bin/{s}", .{executable_name});
300
341
  defer allocator.free(binary_subpath);
@@ -343,8 +384,8 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
343
384
  try cef.ensureLayoutFor(io, cef_platform, options.cef_dir);
344
385
  try copyDesktopCefRuntime(allocator, io, dir, options.target, options.cef_dir);
345
386
  }
346
- try writeReport(allocator, dir, io, "package-manifest.zon", options, executable_name, bundle_stats.asset_count);
347
- return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = options.target, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine, .web_layer = webLayerFor(options) };
387
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, executable_name, bundle_stats.asset_count, windows_subsystem);
388
+ return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = options.target, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine, .web_layer = webLayerFor(options), .windows_subsystem = windows_subsystem };
348
389
  }
349
390
 
350
391
  /// The iOS host tier: a COMPLETE Xcode project the user never edits —
@@ -415,7 +456,7 @@ fn createIosArtifact(allocator: std.mem.Allocator, io: std.Io, options: PackageO
415
456
  const readme = try iosProjectReadme(allocator, options.metadata);
416
457
  defer allocator.free(readme);
417
458
  try writeFile(dir, io, "README.md", readme);
418
- try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
459
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count, null);
419
460
  return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = .ios, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine, .web_layer = webLayerFor(options) };
420
461
  }
421
462
 
@@ -477,7 +518,7 @@ fn createAndroidArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
477
518
  const readme = try androidProjectReadme(allocator, options.metadata);
478
519
  defer allocator.free(readme);
479
520
  try writeFile(dir, io, "README.md", readme);
480
- try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
521
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count, null);
481
522
 
482
523
  var artifact_name: []const u8 = std.fs.path.basename(options.output_path);
483
524
  if (try assembleAndroidApk(allocator, io, options)) |apk_name| {
@@ -1071,6 +1112,84 @@ fn peReferencesWebView2Loader(allocator: std.mem.Allocator, io: std.Io, path: []
1071
1112
  std.mem.indexOf(u8, bytes, needle_ascii) != null;
1072
1113
  }
1073
1114
 
1115
+ /// The Windows subsystem a PE executable declares (IMAGE_OPTIONAL_HEADER
1116
+ /// Subsystem: 2 = GUI, 3 = console), or null when the file is not a PE
1117
+ /// with an optional header large enough to say. The packaging check
1118
+ /// reads this because a console-subsystem GUI app flashes a terminal
1119
+ /// window behind itself on every launch — `native build` produces
1120
+ /// GUI-subsystem release exes, so a console one here means a stale or
1121
+ /// hand-built binary.
1122
+ pub fn peSubsystem(bytes: []const u8) ?u16 {
1123
+ if (bytes.len < 0x40 or bytes[0] != 'M' or bytes[1] != 'Z') return null;
1124
+ const pe_offset: usize = std.mem.readInt(u32, bytes[0x3c..0x40], .little);
1125
+ if (pe_offset + 24 > bytes.len) return null;
1126
+ if (!std.mem.eql(u8, bytes[pe_offset..][0..4], "PE\x00\x00")) return null;
1127
+ const optional_size = std.mem.readInt(u16, bytes[pe_offset + 20 ..][0..2], .little);
1128
+ // Subsystem sits 68 bytes into the optional header (same offset in
1129
+ // PE32 and PE32+).
1130
+ if (optional_size < 70) return null;
1131
+ const subsystem_offset = pe_offset + 24 + 68;
1132
+ if (subsystem_offset + 2 > bytes.len) return null;
1133
+ return std.mem.readInt(u16, bytes[subsystem_offset..][0..2], .little);
1134
+ }
1135
+
1136
+ const pe_subsystem_gui: u16 = 2;
1137
+ const pe_subsystem_console: u16 = 3;
1138
+
1139
+ /// The farthest into a file a real executable's PE header is allowed to
1140
+ /// start for the subsystem probe: linkers place `e_lfanew` right after
1141
+ /// the DOS stub (well under a page), so an offset past this ceiling is
1142
+ /// not a real Windows exe and proves nothing about the subsystem —
1143
+ /// exactly like non-PE bytes.
1144
+ const max_pe_header_offset: usize = 1024 * 1024;
1145
+
1146
+ /// The Subsystem field's distance past the PE signature: 24 header
1147
+ /// bytes (signature + COFF), then 68 bytes into the optional header,
1148
+ /// plus the field's own 2 bytes.
1149
+ const pe_subsystem_span: usize = 24 + 70;
1150
+
1151
+ /// `peSubsystem` over a file, reading ONLY the headers: the DOS header
1152
+ /// names where the PE header starts (`e_lfanew` at 0x3c), and the
1153
+ /// Subsystem field sits a fixed `pe_subsystem_span` past that — so
1154
+ /// `min(file size, e_lfanew + span)` bytes bound the read no matter how
1155
+ /// large the executable is. The whole-file slurp this replaces
1156
+ /// (`readPath`, capped at 128 MiB) allocated the entire binary to read
1157
+ /// 2 bytes and silently skipped exes over its cap.
1158
+ fn peSubsystemAtPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !?u16 {
1159
+ var file = try std.Io.Dir.cwd().openFile(io, path, .{});
1160
+ defer file.close(io);
1161
+ var read_buffer: [4096]u8 = undefined;
1162
+ var reader = file.reader(io, &read_buffer);
1163
+ var dos_header: [0x40]u8 = undefined;
1164
+ if (try reader.interface.readSliceShort(&dos_header) < dos_header.len) return null;
1165
+ if (dos_header[0] != 'M' or dos_header[1] != 'Z') return null;
1166
+ const pe_offset: usize = std.mem.readInt(u32, dos_header[0x3c..0x40], .little);
1167
+ if (pe_offset > max_pe_header_offset) return null;
1168
+ const bytes = try allocator.alloc(u8, pe_offset + pe_subsystem_span);
1169
+ defer allocator.free(bytes);
1170
+ @memcpy(bytes[0..dos_header.len], &dos_header);
1171
+ const rest = try reader.interface.readSliceShort(bytes[dos_header.len..]);
1172
+ return peSubsystem(bytes[0 .. dos_header.len + rest]);
1173
+ }
1174
+
1175
+ /// The posture check's verdict over a file: only a parsed Subsystem of
1176
+ /// GUI (2) or console (3) earns a named claim; everything else — a file
1177
+ /// the parse proves non-PE or headerless, an unreadable file, or a
1178
+ /// declared subsystem this check does not model (native, EFI, ...) —
1179
+ /// is honestly `unknown`. OutOfMemory still propagates: an allocation
1180
+ /// failure says nothing about the exe.
1181
+ fn peSubsystemVerdictAtPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !WindowsSubsystem {
1182
+ const subsystem = peSubsystemAtPath(allocator, io, path) catch |err| switch (err) {
1183
+ error.OutOfMemory => return err,
1184
+ else => return .unknown,
1185
+ };
1186
+ return switch (subsystem orelse return .unknown) {
1187
+ pe_subsystem_gui => .gui,
1188
+ pe_subsystem_console => .console,
1189
+ else => .unknown,
1190
+ };
1191
+ }
1192
+
1074
1193
  /// Whether a Linux executable carries the embedded web layer: the GTK
1075
1194
  /// host links webkitgtk-6.0 directly, so the honest evidence is a
1076
1195
  /// libwebkitgtk/libjavascriptcoregtk DT_NEEDED entry or a webkit_*/jsc_*
@@ -1186,11 +1305,13 @@ fn readPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) ![]u8 {
1186
1305
  return reader.interface.allocRemaining(allocator, .limited(128 * 1024 * 1024));
1187
1306
  }
1188
1307
 
1189
- fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpath: []const u8, options: PackageOptions, executable_name: []const u8, asset_count: usize) !void {
1308
+ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpath: []const u8, options: PackageOptions, executable_name: []const u8, asset_count: usize, windows_subsystem: ?WindowsSubsystem) !void {
1190
1309
  const capabilities = try capabilityLines(allocator, options.metadata.capabilities);
1191
1310
  defer allocator.free(capabilities);
1192
1311
  const frontend = try frontendLines(allocator, options.frontend);
1193
1312
  defer allocator.free(frontend);
1313
+ const subsystem = try subsystemLine(allocator, windows_subsystem);
1314
+ defer allocator.free(subsystem);
1194
1315
  const artifact = try zonStringAlloc(allocator, std.fs.path.basename(options.output_path));
1195
1316
  defer allocator.free(artifact);
1196
1317
  const target = try zonStringAlloc(allocator, @tagName(options.target));
@@ -1227,7 +1348,7 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1227
1348
  \\ .web_engine = {s},
1228
1349
  \\ .web_layer = {s},
1229
1350
  \\ .signing = {s},
1230
- \\ .asset_count = {d},
1351
+ \\{s} .asset_count = {d},
1231
1352
  \\{s}
1232
1353
  \\ .capabilities = .{{
1233
1354
  \\{s}
@@ -1244,6 +1365,7 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1244
1365
  web_engine,
1245
1366
  web_layer,
1246
1367
  signing,
1368
+ subsystem,
1247
1369
  asset_count,
1248
1370
  frontend,
1249
1371
  capabilities,
@@ -1265,6 +1387,18 @@ fn capabilityLines(allocator: std.mem.Allocator, capabilities: []const []const u
1265
1387
  return out.toOwnedSlice(allocator);
1266
1388
  }
1267
1389
 
1390
+ /// The report's subsystem-verdict line, present only when the posture
1391
+ /// check actually ran (a Windows package with a binary to probe): the
1392
+ /// durable twin of the transient warning[package.console-subsystem],
1393
+ /// in the same string style the report's other verdicts use.
1394
+ fn subsystemLine(allocator: std.mem.Allocator, windows_subsystem: ?WindowsSubsystem) ![]const u8 {
1395
+ const subsystem = windows_subsystem orelse return allocator.dupe(u8, "");
1396
+ return std.fmt.allocPrint(allocator,
1397
+ \\ .subsystem = "{s}",
1398
+ \\
1399
+ , .{@tagName(subsystem)});
1400
+ }
1401
+
1268
1402
  fn frontendLines(allocator: std.mem.Allocator, frontend: ?manifest_tool.FrontendMetadata) ![]const u8 {
1269
1403
  if (frontend) |config| {
1270
1404
  const dist = try zonStringAlloc(allocator, config.dist);
@@ -2369,13 +2503,15 @@ test "package report records target signing and assets" {
2369
2503
  .target = .linux,
2370
2504
  .output_path = ".zig-cache/test-package-report",
2371
2505
  .signing = .{ .mode = .none },
2372
- }, "demo", 2);
2506
+ }, "demo", 2, null);
2373
2507
  var buffer: [512]u8 = undefined;
2374
2508
  var file = try dir.openFile(std.testing.io, "package-manifest.zon", .{});
2375
2509
  defer file.close(std.testing.io);
2376
2510
  const len = try file.readPositionalAll(std.testing.io, &buffer, 0);
2377
2511
  try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".target = \"linux\"") != null);
2378
2512
  try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".asset_count = 2") != null);
2513
+ // No subsystem check ran, so the report makes no subsystem claim.
2514
+ try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".subsystem") == null);
2379
2515
  }
2380
2516
 
2381
2517
  test "adhoc packaging into a spaced output path signs and verifies" {
@@ -2514,10 +2650,13 @@ test "webview-declaring package reports the web layer as declared" {
2514
2650
  });
2515
2651
  try std.testing.expect(stats.web_layer.?.enabled);
2516
2652
  try std.testing.expectEqual(manifest_tool.WebLayerReason.capability, stats.web_layer.?.reason);
2653
+ // No binary means the subsystem check never ran: no verdict to carry.
2654
+ try std.testing.expectEqual(@as(?WindowsSubsystem, null), stats.windows_subsystem);
2517
2655
 
2518
2656
  const report = try readPath(std.testing.allocator, std.testing.io, root ++ "/demo-windows/package-manifest.zon");
2519
2657
  defer std.testing.allocator.free(report);
2520
2658
  try std.testing.expect(std.mem.indexOf(u8, report, ".web_layer = \"webview2 (declared: capabilities)\"") != null);
2659
+ try std.testing.expect(std.mem.indexOf(u8, report, ".subsystem") == null);
2521
2660
  }
2522
2661
 
2523
2662
  test "package refuses a manifest that excludes the web layer while declaring web content" {
@@ -2599,6 +2738,170 @@ fn testWebLayerPeBytes() [0x80 + "WebView2Loader.dll".len * 2]u8 {
2599
2738
  return bytes;
2600
2739
  }
2601
2740
 
2741
+ /// A minimal Windows executable with a real optional header for the
2742
+ /// subsystem tests: MZ + PE + a COFF header declaring a 240-byte
2743
+ /// optional header, with the Subsystem field set as asked.
2744
+ fn testSubsystemPeBytes(subsystem: u16) [0x200]u8 {
2745
+ var bytes = [_]u8{0} ** 0x200;
2746
+ bytes[0] = 'M';
2747
+ bytes[1] = 'Z';
2748
+ std.mem.writeInt(u32, bytes[0x3c..0x40], 0x40, .little);
2749
+ bytes[0x40] = 'P';
2750
+ bytes[0x41] = 'E';
2751
+ // COFF SizeOfOptionalHeader (PE32+ is 240 bytes).
2752
+ std.mem.writeInt(u16, bytes[0x54..0x56], 240, .little);
2753
+ // Optional header starts at 0x58; Subsystem sits 68 bytes in.
2754
+ std.mem.writeInt(u16, bytes[0x9c..0x9e], subsystem, .little);
2755
+ return bytes;
2756
+ }
2757
+
2758
+ test "peSubsystem reads the optional header and refuses non-PE bytes" {
2759
+ const gui = testSubsystemPeBytes(2);
2760
+ try std.testing.expectEqual(@as(?u16, 2), peSubsystem(&gui));
2761
+ const console = testSubsystemPeBytes(3);
2762
+ try std.testing.expectEqual(@as(?u16, 3), peSubsystem(&console));
2763
+ // The loader-scan fixture has no optional header: no subsystem claim.
2764
+ const headerless = testWebLayerPeBytes();
2765
+ try std.testing.expectEqual(@as(?u16, null), peSubsystem(&headerless));
2766
+ try std.testing.expectEqual(@as(?u16, null), peSubsystem("not a pe"));
2767
+ }
2768
+
2769
+ test "the subsystem probe reads only the headers, so an exe past the old slurp cap still warns" {
2770
+ // A console-subsystem exe stretched past readPath's 128 MiB cap
2771
+ // (sparse: valid headers, then a hole). The whole-file slurp this
2772
+ // probe replaced hit the cap's StreamTooLong, swallowed it as
2773
+ // false, and packaged the exe WITHOUT the promised console warning
2774
+ // — the bounded header read answers from the first 94-ish bytes and
2775
+ // never sees the far end of the file.
2776
+ var cwd = std.Io.Dir.cwd();
2777
+ const root = ".zig-cache/test-package-subsystem-huge";
2778
+ try cwd.deleteTree(std.testing.io, root);
2779
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2780
+ try cwd.createDirPath(std.testing.io, root);
2781
+ const console_bytes = testSubsystemPeBytes(3);
2782
+ const path = root ++ "/huge-console.exe";
2783
+ try cwd.writeFile(std.testing.io, .{ .sub_path = path, .data = &console_bytes });
2784
+ {
2785
+ var file = try cwd.openFile(std.testing.io, path, .{ .mode = .read_write });
2786
+ defer file.close(std.testing.io);
2787
+ try file.setLength(std.testing.io, 128 * 1024 * 1024 + 4096);
2788
+ }
2789
+ try std.testing.expectEqual(WindowsSubsystem.console, try peSubsystemVerdictAtPath(std.testing.allocator, std.testing.io, path));
2790
+ }
2791
+
2792
+ test "a PE offset past the header ceiling proves nothing and allocates nothing" {
2793
+ // An `e_lfanew` claiming the PE header sits 8 MiB into the file is
2794
+ // not a real Windows exe (linkers put it right after the DOS stub):
2795
+ // the probe answers null before sizing any buffer to the claimed
2796
+ // offset — the failing allocator turns any allocation into a test
2797
+ // failure — and packaging treats it like non-PE bytes: an unknown
2798
+ // verdict, no warning, and never a gui claim.
2799
+ var cwd = std.Io.Dir.cwd();
2800
+ const root = ".zig-cache/test-package-subsystem-offset";
2801
+ try cwd.deleteTree(std.testing.io, root);
2802
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2803
+ try cwd.createDirPath(std.testing.io, root);
2804
+ var bytes = [_]u8{0} ** 0x40;
2805
+ bytes[0] = 'M';
2806
+ bytes[1] = 'Z';
2807
+ std.mem.writeInt(u32, bytes[0x3c..0x40], 8 * 1024 * 1024, .little);
2808
+ const path = root ++ "/bogus-offset.exe";
2809
+ try cwd.writeFile(std.testing.io, .{ .sub_path = path, .data = &bytes });
2810
+ try std.testing.expectEqual(WindowsSubsystem.unknown, try peSubsystemVerdictAtPath(std.testing.failing_allocator, std.testing.io, path));
2811
+ }
2812
+
2813
+ test "windows package pins the exe's subsystem: GUI is quiet, console warns in the stats" {
2814
+ var cwd = std.Io.Dir.cwd();
2815
+ const root = ".zig-cache/test-package-subsystem";
2816
+ try cwd.deleteTree(std.testing.io, root);
2817
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2818
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2819
+ const metadata: manifest_tool.Metadata = .{
2820
+ .id = "dev.example.subsystem",
2821
+ .name = "subsystem-demo",
2822
+ .version = "1.0.0",
2823
+ };
2824
+
2825
+ // The exe `native build` installs: GUI subsystem, no console flash —
2826
+ // the package carries no console finding.
2827
+ const gui_bytes = testSubsystemPeBytes(2);
2828
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/gui.exe", .data = &gui_bytes });
2829
+ const gui_stats = try createPackage(std.testing.allocator, std.testing.io, .{
2830
+ .metadata = metadata,
2831
+ .target = .windows,
2832
+ .output_path = root ++ "/gui-package",
2833
+ .binary_path = root ++ "/gui.exe",
2834
+ .assets_dir = root ++ "/assets",
2835
+ });
2836
+ try std.testing.expectEqual(@as(?WindowsSubsystem, .gui), gui_stats.windows_subsystem);
2837
+ const gui_report = try readPath(std.testing.allocator, std.testing.io, root ++ "/gui-package/package-manifest.zon");
2838
+ defer std.testing.allocator.free(gui_report);
2839
+ try std.testing.expect(std.mem.indexOf(u8, gui_report, ".subsystem = \"gui\"") != null);
2840
+
2841
+ // A stale or hand-built console-subsystem exe: packaged (the app
2842
+ // still works), but the stats carry the finding the warning teaches.
2843
+ const console_bytes = testSubsystemPeBytes(3);
2844
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/console.exe", .data = &console_bytes });
2845
+ const console_stats = try createPackage(std.testing.allocator, std.testing.io, .{
2846
+ .metadata = metadata,
2847
+ .target = .windows,
2848
+ .output_path = root ++ "/console-package",
2849
+ .binary_path = root ++ "/console.exe",
2850
+ .assets_dir = root ++ "/assets",
2851
+ });
2852
+ try std.testing.expectEqual(@as(?WindowsSubsystem, .console), console_stats.windows_subsystem);
2853
+ const console_report = try readPath(std.testing.allocator, std.testing.io, root ++ "/console-package/package-manifest.zon");
2854
+ defer std.testing.allocator.free(console_report);
2855
+ try std.testing.expect(std.mem.indexOf(u8, console_report, ".subsystem = \"console\"") != null);
2856
+ }
2857
+
2858
+ test "a binary the probe proves nothing about reports subsystem unknown, never gui" {
2859
+ // The parse failing is not evidence of a GUI exe: non-PE bytes and a
2860
+ // PE truncated before its optional header both earn an honest
2861
+ // "unknown" in the stats and report — the old bool verdict folded
2862
+ // these into false and the report affirmatively claimed "gui".
2863
+ var cwd = std.Io.Dir.cwd();
2864
+ const root = ".zig-cache/test-package-subsystem-unknown";
2865
+ try cwd.deleteTree(std.testing.io, root);
2866
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2867
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2868
+ const metadata: manifest_tool.Metadata = .{
2869
+ .id = "dev.example.subsystem-unknown",
2870
+ .name = "subsystem-unknown-demo",
2871
+ .version = "1.0.0",
2872
+ };
2873
+
2874
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/not-a-pe.exe", .data = "not a pe at all" });
2875
+ const full_bytes = testSubsystemPeBytes(3);
2876
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/truncated.exe", .data = full_bytes[0..0x50] });
2877
+ const fixtures = [_]struct { binary: []const u8, output: []const u8 }{
2878
+ .{ .binary = root ++ "/not-a-pe.exe", .output = root ++ "/not-a-pe-package" },
2879
+ .{ .binary = root ++ "/truncated.exe", .output = root ++ "/truncated-package" },
2880
+ };
2881
+ for (fixtures) |fixture| {
2882
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2883
+ .metadata = metadata,
2884
+ .target = .windows,
2885
+ .output_path = fixture.output,
2886
+ .binary_path = fixture.binary,
2887
+ .assets_dir = root ++ "/assets",
2888
+ });
2889
+ try std.testing.expectEqual(@as(?WindowsSubsystem, .unknown), stats.windows_subsystem);
2890
+ const report_path = try std.fmt.allocPrint(std.testing.allocator, "{s}/package-manifest.zon", .{fixture.output});
2891
+ defer std.testing.allocator.free(report_path);
2892
+ const report = try readPath(std.testing.allocator, std.testing.io, report_path);
2893
+ defer std.testing.allocator.free(report);
2894
+ try std.testing.expect(std.mem.indexOf(u8, report, ".subsystem = \"unknown\"") != null);
2895
+ try std.testing.expect(std.mem.indexOf(u8, report, ".subsystem = \"gui\"") == null);
2896
+ }
2897
+
2898
+ // A subsystem the check does not model (1 = native) parses fine but
2899
+ // still proves nothing about console-vs-GUI posture.
2900
+ const native_bytes = testSubsystemPeBytes(1);
2901
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/native.exe", .data = &native_bytes });
2902
+ try std.testing.expectEqual(WindowsSubsystem.unknown, try peSubsystemVerdictAtPath(std.testing.allocator, std.testing.io, root ++ "/native.exe"));
2903
+ }
2904
+
2602
2905
  test "package --web-layer include on a canvas manifest stages the loader and names the flag" {
2603
2906
  var cwd = std.Io.Dir.cwd();
2604
2907
  const root = ".zig-cache/test-package-web-layer-include-flag";