@native-sdk/cli 0.7.2 → 0.8.1
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/build/app.zig +265 -65
- package/build/ts_run.mjs +6 -8
- package/build.zig +292 -349
- package/package.json +13 -11
- package/packages/core/compile-surface/core.ts +559 -0
- package/packages/core/package-lock.json +375 -12
- package/packages/core/package.json +9 -6
- package/packages/core/scripts/compiler_typecheck.mjs +58 -0
- package/packages/core/scripts/gen_case_tables.mjs +130 -0
- package/packages/core/scripts/gen_declarations.mjs +97 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +108 -0
- package/packages/core/scripts/stage_external_core.mjs +148 -0
- package/packages/core/sdk/core.d.ts +363 -0
- package/packages/core/sdk/events.d.ts +65 -0
- package/packages/core/sdk/text.d.ts +51 -0
- package/packages/core/src/checker.ts +3 -3
- package/packages/core/src/cli.ts +40 -38
- package/packages/core/src/contract.ts +749 -0
- package/packages/core/src/devhost.ts +4 -3
- package/packages/core/src/diagnostics.ts +5 -7
- package/packages/core/src/{transpile.ts → frontend.ts} +38 -50
- package/packages/core/src/infer.ts +1 -1
- package/packages/core/src/typed_ast.ts +16 -15
- package/packages/core/src/wyhash.ts +79 -0
- package/skill-data/native-ui/SKILL.md +3 -2
- package/skill-data/ts-core/SKILL.md +29 -29
- package/src/platform/macos/appkit_host.h +4 -4
- package/src/platform/macos/appkit_host.m +24 -4
- package/src/platform/macos/cef_host.mm +22 -6
- package/src/platform/macos/root.zig +2 -2
- package/src/primitives/canvas/markdown.zig +2010 -57
- package/src/primitives/canvas/markdown_hostile_tests.zig +40 -0
- package/src/primitives/canvas/markdown_tests.zig +778 -3
- package/src/primitives/canvas/ui_markup.zig +9 -1
- package/src/primitives/canvas/ui_markup_compiled.zig +29 -0
- package/src/primitives/canvas/ui_markup_contract.zig +11 -0
- package/src/primitives/canvas/ui_markup_view.zig +20 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +13 -1
- package/src/primitives/canvas/ui_schema.zig +3 -0
- package/src/primitives/canvas/ui_schema_tests.zig +4 -3
- package/src/primitives/canvas/widget_layout.zig +26 -3
- package/src/primitives/canvas/widget_metrics.zig +21 -3
- package/src/primitives/canvas/widget_render.zig +6 -1
- package/src/runtime/effects.zig +9 -0
- package/src/runtime/effects_fetch_tests.zig +21 -0
- package/src/runtime/ts_core_host.zig +38 -38
- package/src/runtime/ts_ui_app.zig +12 -12
- package/src/tooling/manifest.zig +32 -0
- package/src/tooling/raw_manifest.zig +4 -0
- package/src/tooling/templates.zig +25 -20
- package/src/tooling/ts_core.zig +55 -34
- package/src/tooling/verbs.zig +1 -1
- package/tools/corewire/core_abi.zig +169 -0
- package/tools/corewire/emit.zig +2418 -0
- package/tools/corewire/emit_facade.zig +3984 -0
- package/tools/corewire/emit_profile.zig +501 -0
- package/tools/corewire/main.zig +561 -0
- package/tools/corewire/shim_rt.zig +632 -0
- package/tools/corewire/sidecar.zig +2507 -0
- package/packages/core/rt/rt.zig +0 -1986
- package/packages/core/src/emitter.ts +0 -11898
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
//! core_profile.json emission: the compiler profile that configures a
|
|
2
|
+
//! library-mode build of the generated facade. One sidecar, three
|
|
3
|
+
//! projections — the host imports core_shim.zig, a compiler consumes
|
|
4
|
+
//! core_facade.ts, and this profile tells that compiler's library mode
|
|
5
|
+
//! what to build: the entry module, the C symbol family (the ABI's
|
|
6
|
+
//! mode-provided symbols plus the export map binding every facade
|
|
7
|
+
//! export the sidecar attests to its marshalled signature), the
|
|
8
|
+
//! contract-sidecar emission the same invocation must produce, and the
|
|
9
|
+
//! SDK's determinism policy (fences, teachings, remediations).
|
|
10
|
+
//!
|
|
11
|
+
//! The emission is deterministic: a pure function of the contract facts
|
|
12
|
+
//! and the entry-module name, fixed key order, no timestamps or
|
|
13
|
+
//! environment — re-running the identical invocation reproduces a
|
|
14
|
+
//! byte-identical file, the same doctrine the sidecar emitter follows.
|
|
15
|
+
//!
|
|
16
|
+
//! Marshalled signatures use the v1 classes only (params f64/bool/
|
|
17
|
+
//! string/bytes/u8/u32/i32; returns those minus the integer plumbing
|
|
18
|
+
//! classes, plus void). The wire-shaped channel entries are expressible
|
|
19
|
+
//! in exactly these classes — bytes ride buffer parameters, the
|
|
20
|
+
//! modifier booleans u8, the pinch phase u32 — which is the point of
|
|
21
|
+
//! the wire shape: a union-typed parameter has no marshalling class and
|
|
22
|
+
//! never becomes one.
|
|
23
|
+
|
|
24
|
+
const std = @import("std");
|
|
25
|
+
const sidecar_mod = @import("sidecar.zig");
|
|
26
|
+
|
|
27
|
+
const Sidecar = sidecar_mod.Sidecar;
|
|
28
|
+
|
|
29
|
+
pub const Error = error{ Refused, OutOfMemory };
|
|
30
|
+
|
|
31
|
+
/// The entry-module spelling the profile declares when the caller
|
|
32
|
+
/// names none: the facade projection's conventional file name, beside
|
|
33
|
+
/// the profile.
|
|
34
|
+
pub const default_entry = "core_facade.ts";
|
|
35
|
+
|
|
36
|
+
/// One export-map row: the marshalled signature of a facade export, by
|
|
37
|
+
/// ABI suffix. The export name is the generated facade's own exported
|
|
38
|
+
/// function (the plain suffix; the conditional channel entries take the
|
|
39
|
+
/// facade's `abi_` spelling); the symbol takes the contract's prefix.
|
|
40
|
+
const ExportSignature = struct {
|
|
41
|
+
suffix: []const u8,
|
|
42
|
+
/// The facade's exported function name when it differs from the
|
|
43
|
+
/// suffix (the subscription and channel ABI entries).
|
|
44
|
+
export_name: ?[]const u8 = null,
|
|
45
|
+
params: []const []const u8,
|
|
46
|
+
returns: []const u8,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/// The marshalled signatures of every profile-mapped ABI entry, keyed
|
|
50
|
+
/// by export suffix. The four mode-provided symbols (init, the sink
|
|
51
|
+
/// registration, the result-arena reset, collect) ride the profile's
|
|
52
|
+
/// abi block instead, and the two identity getters are synthesized
|
|
53
|
+
/// from the profile's sidecar section — neither family appears here.
|
|
54
|
+
const export_signatures = [_]ExportSignature{
|
|
55
|
+
.{ .suffix = "boot_cmd", .params = &.{}, .returns = "bytes" },
|
|
56
|
+
.{ .suffix = "dispatch_void", .params = &.{"u8"}, .returns = "bytes" },
|
|
57
|
+
.{ .suffix = "dispatch_bytes", .params = &.{ "u8", "bytes" }, .returns = "bytes" },
|
|
58
|
+
.{ .suffix = "dispatch_number", .params = &.{ "u8", "f64" }, .returns = "bytes" },
|
|
59
|
+
.{ .suffix = "dispatch_number_bytes", .params = &.{ "u8", "f64", "bytes" }, .returns = "bytes" },
|
|
60
|
+
.{ .suffix = "dispatch_bool", .params = &.{ "u8", "u8" }, .returns = "bytes" },
|
|
61
|
+
.{ .suffix = "dispatch_enum", .params = &.{ "u8", "u32" }, .returns = "bytes" },
|
|
62
|
+
.{ .suffix = "dispatch_record", .params = &.{ "u8", "bytes" }, .returns = "bytes" },
|
|
63
|
+
.{ .suffix = "dispatch_text_input", .params = &.{ "u8", "bytes" }, .returns = "bytes" },
|
|
64
|
+
// The two-axis scroll record crosses as direct scalars: one
|
|
65
|
+
// offset/velocity/viewport/content quartet per axis.
|
|
66
|
+
.{ .suffix = "dispatch_scroll_state", .params = &.{ "u8", "f64", "f64", "f64", "f64", "f64", "f64", "f64", "f64" }, .returns = "bytes" },
|
|
67
|
+
.{ .suffix = "subscriptions", .export_name = "abi_subscriptions", .params = &.{}, .returns = "bytes" },
|
|
68
|
+
.{ .suffix = "model_snapshot", .params = &.{}, .returns = "bytes" },
|
|
69
|
+
.{ .suffix = "helper_call", .params = &.{ "u32", "bytes" }, .returns = "bytes" },
|
|
70
|
+
// The wire-shaped conditional channel entries (present exactly when
|
|
71
|
+
// the sidecar wires the channel): host-event params in, the channel
|
|
72
|
+
// bytes envelope out.
|
|
73
|
+
.{ .suffix = "command_msg", .export_name = "abi_command_msg", .params = &.{"bytes"}, .returns = "bytes" },
|
|
74
|
+
.{ .suffix = "frame_msg", .export_name = "abi_frame_msg", .params = &.{ "f64", "f64", "f64", "f64" }, .returns = "bytes" },
|
|
75
|
+
.{ .suffix = "key_msg", .export_name = "abi_key_msg", .params = &.{ "bytes", "u8", "u8", "u8", "u8" }, .returns = "bytes" },
|
|
76
|
+
.{ .suffix = "pinch_msg", .export_name = "abi_pinch_msg", .params = &.{ "f64", "bytes", "u32", "f64", "f64", "f64" }, .returns = "bytes" },
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/// Suffixes that never enter the export map: the mode-provided symbols
|
|
80
|
+
/// the abi block names, and the identity getters the sidecar section
|
|
81
|
+
/// synthesizes from profile constants.
|
|
82
|
+
const unmapped_suffixes = [_][]const u8{
|
|
83
|
+
"abi_version", "build_id", "set_panic_sink", "init", "collect", "frame_reset",
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/// One determinism fence: a surface denied at compile time when the
|
|
87
|
+
/// compiled module graph reaches it, with the SDK's teaching naming the
|
|
88
|
+
/// sanctioned effect route.
|
|
89
|
+
const Fence = struct {
|
|
90
|
+
/// Exactly one of `id` (one surface-manifest entry) or `prefix`
|
|
91
|
+
/// (every entry it prefixes) is set.
|
|
92
|
+
id: ?[]const u8 = null,
|
|
93
|
+
prefix: ?[]const u8 = null,
|
|
94
|
+
teaching: []const u8,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const randomness_teaching = "randomness is an effect: the core requests it through a command and the value arrives as a Msg, so a recorded session replays it exactly.";
|
|
98
|
+
const network_teaching = "network is an effect: declare requests as commands (Cmd.fetch, Cmd.request) and responses arrive as Msgs.";
|
|
99
|
+
|
|
100
|
+
const clock_teaching = "the wall clock is an effect: read it through the host's journaled clock — Cmd.now, Cmd.delay, and Sub.timer deliver fire times as Msgs, so a recorded session replays them exactly.";
|
|
101
|
+
|
|
102
|
+
/// The SDK determinism fences, keyed by the pinned compiler release's
|
|
103
|
+
/// surface-manifest ids. RELEASE-PINNED DATA: a fence id or prefix must
|
|
104
|
+
/// resolve against the pinned release's surface manifest, covering at
|
|
105
|
+
/// least one runtime-deniable entry — an unknown or unpoliceable
|
|
106
|
+
/// selector refuses the whole profile at load rather than riding
|
|
107
|
+
/// inert — so this table lists exactly what the pinned release can
|
|
108
|
+
/// fence, and it is the one place to grow when a release lands new
|
|
109
|
+
/// ids. Not fenceable at the pinned release, verified against its
|
|
110
|
+
/// loader: http/https/dgram/dns (module-level entries only, no
|
|
111
|
+
/// per-call trace to deny) and worker_threads/cluster (their members
|
|
112
|
+
/// fold to compile-time constants). Member prefixes carry the folded-
|
|
113
|
+
/// constant exemption, so a folded read (os.EOL) stays compilable
|
|
114
|
+
/// under its family's fence.
|
|
115
|
+
const determinism_fences = [_]Fence{
|
|
116
|
+
.{ .id = "stdlib.math.random", .teaching = randomness_teaching },
|
|
117
|
+
.{ .id = "node-builtin.crypto.randomBytes", .teaching = randomness_teaching },
|
|
118
|
+
.{ .id = "node-builtin.crypto.randomUUID", .teaching = randomness_teaching },
|
|
119
|
+
.{ .prefix = "stdlib.date.", .teaching = clock_teaching },
|
|
120
|
+
.{ .prefix = "node-builtin.perf_hooks.", .teaching = clock_teaching },
|
|
121
|
+
.{ .prefix = "node-builtin.fs.", .teaching = "files are effects: declare reads and writes as commands (Cmd.readFile, Cmd.writeFile) and results arrive as Msgs." },
|
|
122
|
+
.{ .prefix = "node-builtin.net.", .teaching = network_teaching },
|
|
123
|
+
.{ .prefix = "node-builtin.http2.", .teaching = network_teaching },
|
|
124
|
+
.{ .prefix = "node-builtin.tls.", .teaching = network_teaching },
|
|
125
|
+
.{ .prefix = "node-builtin.child_process.", .teaching = "processes are effects: run them through Cmd.spawn and their output arrives as Msgs." },
|
|
126
|
+
.{ .prefix = "node-builtin.timers.", .teaching = "timers are effects: schedule Cmd.delay or Sub.timer and fire times arrive as Msgs through the host's journaled clock." },
|
|
127
|
+
.{ .prefix = "node-builtin.os.", .teaching = "machine and session facts are host inputs: they reach a core as journaled Msgs, never as ambient reads." },
|
|
128
|
+
.{ .prefix = "node-builtin.process.", .teaching = "process and environment facts are host inputs: environment values arrive as journaled Msgs through the env channel, and every other session fact reaches a core only as a host-delivered Msg." },
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/// One rider entry for the teachings/remediations maps.
|
|
132
|
+
const Rider = struct {
|
|
133
|
+
code: []const u8,
|
|
134
|
+
text: []const u8,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/// Profile teachings on non-fence refusals. "async" is the shared key
|
|
138
|
+
/// both async-surface refusal codes read.
|
|
139
|
+
const determinism_teachings = [_]Rider{
|
|
140
|
+
.{ .code = "async", .text = "async, promises, and timer callbacks are unavailable in an app core: the host executes effects — declare a command (Cmd.fetch, Cmd.delay) or a subscription (Sub.timer) and results arrive as Msgs." },
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/// Remediations threaded into the structured trap encoding, one per
|
|
144
|
+
/// trap code where the SDK has a sanctioned recovery to name. A trapped
|
|
145
|
+
/// core is poisoned by contract, so every recovery goes through the
|
|
146
|
+
/// process, never through the wounded core.
|
|
147
|
+
const determinism_remediations = [_]Rider{
|
|
148
|
+
.{ .code = "SC4013", .text = "rebuild the app: an exception escaping a core entry is contract skew between the compiled core and its host bindings, and one build regenerates both from one contract." },
|
|
149
|
+
.{ .code = "SC4017", .text = "restart the app process: a trapped core is poisoned and never resumes in place, and the recorded session replays deterministically." },
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
pub fn emitProfile(arena: std.mem.Allocator, sidecar: Sidecar, entry: []const u8, diags: *sidecar_mod.Diagnostics) Error![]const u8 {
|
|
153
|
+
// The profile is ENFORCEMENT data: a compile that succeeds under its
|
|
154
|
+
// determinism fences attests deterministic, and the compilation mode
|
|
155
|
+
// is structurally async-free — so a contract attesting false for
|
|
156
|
+
// either cannot round-trip through this profile. Emitting one would
|
|
157
|
+
// move the contradiction downstream (the core refuses at compile, or
|
|
158
|
+
// its co-emitted contract silently flips the attestation); refuse
|
|
159
|
+
// here instead, where the teaching can name the attestation.
|
|
160
|
+
if (!sidecar.deterministic) {
|
|
161
|
+
diags.flag("deterministic", "the contract attests deterministic: false, but this profile enforces the determinism fences and a core that compiles under them attests true by construction — build the core under this profile and regenerate from the contract it co-emits", .{});
|
|
162
|
+
}
|
|
163
|
+
if (!sidecar.async_free) {
|
|
164
|
+
diags.flag("async_free", "the contract attests async_free: false, but this profile's compilation mode is structurally async-free — a core reaching async surface refuses under it, so this contract cannot have come from the profile it asks for; remove the async surface and regenerate the contract", .{});
|
|
165
|
+
}
|
|
166
|
+
if (diags.hasErrors()) return error.Refused;
|
|
167
|
+
var emitter = ProfileEmitter{ .arena = arena, .sidecar = sidecar, .out = .empty };
|
|
168
|
+
try emitter.run(entry);
|
|
169
|
+
return emitter.out.items;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const ProfileEmitter = struct {
|
|
173
|
+
arena: std.mem.Allocator,
|
|
174
|
+
sidecar: Sidecar,
|
|
175
|
+
out: std.ArrayListUnmanaged(u8),
|
|
176
|
+
|
|
177
|
+
fn print(self: *ProfileEmitter, comptime fmt: []const u8, args: anytype) Error!void {
|
|
178
|
+
const text = try std.fmt.allocPrint(self.arena, fmt, args);
|
|
179
|
+
try self.out.appendSlice(self.arena, text);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
fn raw(self: *ProfileEmitter, text: []const u8) Error!void {
|
|
183
|
+
try self.out.appendSlice(self.arena, text);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
fn run(self: *ProfileEmitter, entry: []const u8) Error!void {
|
|
187
|
+
const prefix = self.sidecar.abi.prefix;
|
|
188
|
+
try self.print(
|
|
189
|
+
\\{{
|
|
190
|
+
\\ "profile_format": 1,
|
|
191
|
+
\\ "name": "native-sdk-core",
|
|
192
|
+
\\ "entry": {s},
|
|
193
|
+
\\ "emission": "llvm",
|
|
194
|
+
\\ "abi": {{
|
|
195
|
+
\\ "prefix": {s},
|
|
196
|
+
\\ "init_symbol": {s},
|
|
197
|
+
\\ "sink_register_symbol": {s},
|
|
198
|
+
\\ "collect_symbol": {s},
|
|
199
|
+
\\ "result_reset_symbol": {s}
|
|
200
|
+
\\ }},
|
|
201
|
+
\\ "exports": [
|
|
202
|
+
\\
|
|
203
|
+
, .{
|
|
204
|
+
try self.jsonString(entry),
|
|
205
|
+
try self.jsonString(prefix),
|
|
206
|
+
try self.symbol(prefix, "init"),
|
|
207
|
+
try self.symbol(prefix, "set_panic_sink"),
|
|
208
|
+
try self.symbol(prefix, "collect"),
|
|
209
|
+
try self.symbol(prefix, "frame_reset"),
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// The export map, in the sidecar's attested (canonical) order:
|
|
213
|
+
// every suffix the object exports that is neither mode-provided
|
|
214
|
+
// nor identity-synthesized. The export name is the generated
|
|
215
|
+
// facade's own exported function; the symbol carries the
|
|
216
|
+
// contract's prefix.
|
|
217
|
+
var first = true;
|
|
218
|
+
for (self.sidecar.abi.exports) |suffix| {
|
|
219
|
+
if (nameListed(&unmapped_suffixes, suffix)) continue;
|
|
220
|
+
const signature = signatureFor(suffix) orelse continue;
|
|
221
|
+
if (!first) try self.raw(",\n");
|
|
222
|
+
first = false;
|
|
223
|
+
try self.print(" {{ \"export\": {s}, \"symbol\": {s}, \"params\": [", .{ try self.jsonString(signature.export_name orelse suffix), try self.symbol(prefix, suffix) });
|
|
224
|
+
for (signature.params, 0..) |class, index| {
|
|
225
|
+
try self.print("{s}\"{s}\"", .{ if (index == 0) "" else ", ", class });
|
|
226
|
+
}
|
|
227
|
+
try self.print("], \"returns\": \"{s}\" }}", .{signature.returns});
|
|
228
|
+
}
|
|
229
|
+
try self.raw("\n ],\n");
|
|
230
|
+
|
|
231
|
+
// The contract-sidecar section: the same invocation that builds
|
|
232
|
+
// the archive emits the sidecar at the declared path and
|
|
233
|
+
// synthesizes the two identity getters from these constants.
|
|
234
|
+
// The designations name the generated facade's own exported
|
|
235
|
+
// declarations: the contract's root type spellings (the facade
|
|
236
|
+
// re-exports the author's declarations verbatim) and the
|
|
237
|
+
// wrapper entries whose return shapes restate the contract's
|
|
238
|
+
// shape flags. A subscribing contract designates its
|
|
239
|
+
// subscriptions wrapper; a non-subscribing one names none (the
|
|
240
|
+
// compiler refuses dangling designations).
|
|
241
|
+
try self.print(
|
|
242
|
+
\\ "sidecar": {{
|
|
243
|
+
\\ "path": "core.contract.json",
|
|
244
|
+
\\ "wire_version": {d},
|
|
245
|
+
\\ "abi_version": {d},
|
|
246
|
+
\\ "snapshot_format": {d},
|
|
247
|
+
\\ "build_id_symbol": {s},
|
|
248
|
+
\\ "abi_version_symbol": {s},
|
|
249
|
+
\\ "model": {s},
|
|
250
|
+
\\ "msg": {s},
|
|
251
|
+
\\ "init_export": "init",
|
|
252
|
+
\\ "update_export": "coreUpdate",
|
|
253
|
+
\\
|
|
254
|
+
, .{
|
|
255
|
+
self.sidecar.wire_version,
|
|
256
|
+
self.sidecar.abi_version,
|
|
257
|
+
self.sidecar.abi.snapshot_format,
|
|
258
|
+
try self.symbol(prefix, "build_id"),
|
|
259
|
+
try self.symbol(prefix, "abi_version"),
|
|
260
|
+
try self.jsonString(self.sidecar.model),
|
|
261
|
+
try self.jsonString(self.sidecar.msg.name),
|
|
262
|
+
});
|
|
263
|
+
if (self.sidecar.has_subscriptions) {
|
|
264
|
+
try self.raw(" \"subscriptions_export\": \"coreSubscriptions\",\n");
|
|
265
|
+
}
|
|
266
|
+
// The integer-slot declarations carry through from the contract:
|
|
267
|
+
// the compiler proves every declared slot whole at construction
|
|
268
|
+
// (the generated facade and the author's own update logic carry
|
|
269
|
+
// the inline proofs), and the co-emitted contract attests the
|
|
270
|
+
// same classes the reference lane derived.
|
|
271
|
+
if (self.sidecar.integer_slots.len == 0) {
|
|
272
|
+
try self.raw(" \"integer_slots\": []\n },\n");
|
|
273
|
+
} else {
|
|
274
|
+
try self.raw(" \"integer_slots\": [\n");
|
|
275
|
+
for (self.sidecar.integer_slots, 0..) |slot, index| {
|
|
276
|
+
try self.print(" {{ \"slot\": {s}, \"class\": \"{t}\" }}{s}\n", .{ try self.jsonString(slot.slot), slot.class, if (index + 1 == self.sidecar.integer_slots.len) "" else "," });
|
|
277
|
+
}
|
|
278
|
+
try self.raw(" ]\n },\n");
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// The determinism policy: deny-fences over the ambient surfaces
|
|
282
|
+
// with the SDK's teachings, the shared async teaching, and the
|
|
283
|
+
// trap remediations.
|
|
284
|
+
try self.raw(" \"determinism\": {\n \"teachings\": {\n");
|
|
285
|
+
for (determinism_teachings, 0..) |rider, index| {
|
|
286
|
+
try self.print(" {s}: {s}{s}\n", .{ try self.jsonString(rider.code), try self.jsonString(rider.text), if (index + 1 == determinism_teachings.len) "" else "," });
|
|
287
|
+
}
|
|
288
|
+
try self.raw(" },\n \"remediations\": {\n");
|
|
289
|
+
for (determinism_remediations, 0..) |rider, index| {
|
|
290
|
+
try self.print(" {s}: {s}{s}\n", .{ try self.jsonString(rider.code), try self.jsonString(rider.text), if (index + 1 == determinism_remediations.len) "" else "," });
|
|
291
|
+
}
|
|
292
|
+
try self.raw(" },\n \"fences\": [\n");
|
|
293
|
+
for (determinism_fences, 0..) |fence, index| {
|
|
294
|
+
if (fence.id) |id| {
|
|
295
|
+
try self.print(" {{ \"id\": {s}, \"teaching\": {s} }}{s}\n", .{ try self.jsonString(id), try self.jsonString(fence.teaching), if (index + 1 == determinism_fences.len) "" else "," });
|
|
296
|
+
} else {
|
|
297
|
+
try self.print(" {{ \"prefix\": {s}, \"teaching\": {s} }}{s}\n", .{ try self.jsonString(fence.prefix.?), try self.jsonString(fence.teaching), if (index + 1 == determinism_fences.len) "" else "," });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
try self.raw(" ]\n }\n}\n");
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/// A prefixed symbol spelling as a JSON string literal.
|
|
304
|
+
fn symbol(self: *ProfileEmitter, prefix: []const u8, suffix: []const u8) Error![]const u8 {
|
|
305
|
+
return self.jsonString(try std.fmt.allocPrint(self.arena, "{s}{s}", .{ prefix, suffix }));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/// A JSON double-quoted string literal: quotes and backslashes
|
|
309
|
+
/// escape, control bytes take \u escapes, everything else rides as
|
|
310
|
+
/// UTF-8 — which the input must therefore be (JSON has no other
|
|
311
|
+
/// text). The CLI validates its one caller-supplied string (the
|
|
312
|
+
/// entry spelling) with a teaching before emission ever starts;
|
|
313
|
+
/// this check is the emitter's own backstop.
|
|
314
|
+
fn jsonString(self: *ProfileEmitter, text: []const u8) Error![]const u8 {
|
|
315
|
+
if (!std.unicode.utf8ValidateSlice(text)) return error.Refused;
|
|
316
|
+
var out: std.ArrayListUnmanaged(u8) = .empty;
|
|
317
|
+
try out.append(self.arena, '"');
|
|
318
|
+
for (text) |char| {
|
|
319
|
+
switch (char) {
|
|
320
|
+
'"' => try out.appendSlice(self.arena, "\\\""),
|
|
321
|
+
'\\' => try out.appendSlice(self.arena, "\\\\"),
|
|
322
|
+
'\n' => try out.appendSlice(self.arena, "\\n"),
|
|
323
|
+
'\r' => try out.appendSlice(self.arena, "\\r"),
|
|
324
|
+
'\t' => try out.appendSlice(self.arena, "\\t"),
|
|
325
|
+
else => {
|
|
326
|
+
if (char < 0x20) {
|
|
327
|
+
try out.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, "\\u{x:0>4}", .{char}));
|
|
328
|
+
} else {
|
|
329
|
+
try out.append(self.arena, char);
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
try out.append(self.arena, '"');
|
|
335
|
+
return out.items;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
fn signatureFor(suffix: []const u8) ?*const ExportSignature {
|
|
340
|
+
for (&export_signatures) |*signature| {
|
|
341
|
+
if (std.mem.eql(u8, signature.suffix, suffix)) return signature;
|
|
342
|
+
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
fn nameListed(names: []const []const u8, name: []const u8) bool {
|
|
347
|
+
for (names) |candidate| {
|
|
348
|
+
if (std.mem.eql(u8, candidate, name)) return true;
|
|
349
|
+
}
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// --------------------------------------------------------------- tests
|
|
354
|
+
|
|
355
|
+
const testing = std.testing;
|
|
356
|
+
|
|
357
|
+
fn profileFromJson(arena: std.mem.Allocator, json: []const u8, entry: []const u8) ![]const u8 {
|
|
358
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
359
|
+
const parsed = try sidecar_mod.read(arena, json, &diags);
|
|
360
|
+
return emitProfile(arena, parsed, entry, &diags);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
test "profile emission is deterministic and carries the library-mode surface" {
|
|
364
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
365
|
+
defer arena_state.deinit();
|
|
366
|
+
const arena = arena_state.allocator();
|
|
367
|
+
const first = try profileFromJson(arena, sidecar_mod.minimal_valid_json, default_entry);
|
|
368
|
+
const second = try profileFromJson(arena, sidecar_mod.minimal_valid_json, default_entry);
|
|
369
|
+
try testing.expectEqualStrings(first, second);
|
|
370
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"profile_format\": 1") != null);
|
|
371
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"entry\": \"core_facade.ts\"") != null);
|
|
372
|
+
// Mode symbols ride the abi block under the contract's prefix.
|
|
373
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"init_symbol\": \"nsc_core_init\"") != null);
|
|
374
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"sink_register_symbol\": \"nsc_core_set_panic_sink\"") != null);
|
|
375
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"collect_symbol\": \"nsc_core_collect\"") != null);
|
|
376
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"result_reset_symbol\": \"nsc_core_frame_reset\"") != null);
|
|
377
|
+
// The export map binds the facade's exported function name to the
|
|
378
|
+
// prefixed symbol with the marshalled signature; mode symbols and
|
|
379
|
+
// identity getters stay out of it.
|
|
380
|
+
try testing.expect(std.mem.indexOf(u8, first, "{ \"export\": \"dispatch_number\", \"symbol\": \"nsc_core_dispatch_number\", \"params\": [\"u8\", \"f64\"], \"returns\": \"bytes\" }") != null);
|
|
381
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"export\": \"init\"") == null);
|
|
382
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"export\": \"build_id\"") == null);
|
|
383
|
+
// The fixed ABI entry must not take the author-facing
|
|
384
|
+
// `subscriptions` spelling: the sidecar emitter treats an export under
|
|
385
|
+
// that name as a real subscription declaration even when the profile
|
|
386
|
+
// deliberately carries no subscriptions_export designation.
|
|
387
|
+
try testing.expect(std.mem.indexOf(u8, first, "{ \"export\": \"abi_subscriptions\", \"symbol\": \"nsc_core_subscriptions\", \"params\": [], \"returns\": \"bytes\" }") != null);
|
|
388
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"export\": \"subscriptions\"") == null);
|
|
389
|
+
// The sidecar section echoes the contract's generations and
|
|
390
|
+
// declares the SDK's emission path, identity-getter symbols, the
|
|
391
|
+
// facade's designated entries, and the integer-slot declarations.
|
|
392
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"path\": \"core.contract.json\"") != null);
|
|
393
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"wire_version\": 3") != null);
|
|
394
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"build_id_symbol\": \"nsc_core_build_id\"") != null);
|
|
395
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"abi_version_symbol\": \"nsc_core_abi_version\"") != null);
|
|
396
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"init_export\": \"init\"") != null);
|
|
397
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"update_export\": \"coreUpdate\"") != null);
|
|
398
|
+
// minimal_valid_json has no subscriptions: the designation must not
|
|
399
|
+
// dangle (the compiler refuses a name that resolves to nothing).
|
|
400
|
+
try testing.expect(std.mem.indexOf(u8, first, "subscriptions_export") == null);
|
|
401
|
+
// The integer-slot declarations carry through from the contract.
|
|
402
|
+
try testing.expect(std.mem.indexOf(u8, first, "{ \"slot\": \"Model.count\", \"class\": \"i64\" }") != null);
|
|
403
|
+
// No provenance stub rides the profile (the compiler computes its
|
|
404
|
+
// own source hash over the module graph).
|
|
405
|
+
try testing.expect(std.mem.indexOf(u8, first, "source_hash") == null);
|
|
406
|
+
// The determinism policy rides whole: fences with SDK teachings,
|
|
407
|
+
// the shared async teaching, the trap remediations.
|
|
408
|
+
try testing.expect(std.mem.indexOf(u8, first, "{ \"id\": \"stdlib.math.random\"") != null);
|
|
409
|
+
try testing.expect(std.mem.indexOf(u8, first, "{ \"prefix\": \"node-builtin.fs.\"") != null);
|
|
410
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"async\":") != null);
|
|
411
|
+
try testing.expect(std.mem.indexOf(u8, first, "\"SC4013\":") != null);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
test "wired channels join the export map with their wire shapes" {
|
|
415
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
416
|
+
defer arena_state.deinit();
|
|
417
|
+
const arena = arena_state.allocator();
|
|
418
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"key_msg\": false", "\"key_msg\": true");
|
|
419
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"pinch_msg\": false", "\"pinch_msg\": true");
|
|
420
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"helper_call\"]", "\"helper_call\", \"key_msg\", \"pinch_msg\"]");
|
|
421
|
+
const generated = try profileFromJson(arena, source, default_entry);
|
|
422
|
+
// The wire-shaped signatures: bytes as buffers, u8 modifier
|
|
423
|
+
// booleans, the pinch phase a u32 member index. The export names
|
|
424
|
+
// take the facade's abi_ spellings; the symbols the contract's
|
|
425
|
+
// prefix.
|
|
426
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ \"export\": \"abi_key_msg\", \"symbol\": \"nsc_core_key_msg\", \"params\": [\"bytes\", \"u8\", \"u8\", \"u8\", \"u8\"], \"returns\": \"bytes\" }") != null);
|
|
427
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ \"export\": \"abi_pinch_msg\", \"symbol\": \"nsc_core_pinch_msg\", \"params\": [\"f64\", \"bytes\", \"u32\", \"f64\", \"f64\", \"f64\"], \"returns\": \"bytes\" }") != null);
|
|
428
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi_command_msg") == null);
|
|
429
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi_frame_msg") == null);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
test "the profile tracks the contract's prefix and generations" {
|
|
433
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
434
|
+
defer arena_state.deinit();
|
|
435
|
+
const arena = arena_state.allocator();
|
|
436
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"prefix\": \"nsc_core_\"", "\"prefix\": \"app2_\"");
|
|
437
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"wire_version\": 3", "\"wire_version\": 3");
|
|
438
|
+
const generated = try profileFromJson(arena, source, "my_facade.ts");
|
|
439
|
+
try testing.expect(std.mem.indexOf(u8, generated, "\"entry\": \"my_facade.ts\"") != null);
|
|
440
|
+
try testing.expect(std.mem.indexOf(u8, generated, "\"prefix\": \"app2_\"") != null);
|
|
441
|
+
try testing.expect(std.mem.indexOf(u8, generated, "\"init_symbol\": \"app2_init\"") != null);
|
|
442
|
+
// Export names keep the facade's own function spellings; only the
|
|
443
|
+
// symbols take the contract's prefix.
|
|
444
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ \"export\": \"model_snapshot\", \"symbol\": \"app2_model_snapshot\"") != null);
|
|
445
|
+
try testing.expect(std.mem.indexOf(u8, generated, "\"export\": \"app2_") == null);
|
|
446
|
+
try testing.expect(std.mem.indexOf(u8, generated, "\"build_id_symbol\": \"app2_build_id\"") != null);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
test "a non-UTF-8 entry spelling refuses instead of corrupting the JSON" {
|
|
450
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
451
|
+
defer arena_state.deinit();
|
|
452
|
+
const arena = arena_state.allocator();
|
|
453
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
454
|
+
const parsed = try sidecar_mod.read(arena, sidecar_mod.minimal_valid_json, &diags);
|
|
455
|
+
try testing.expectError(error.Refused, emitProfile(arena, parsed, "core_\xfffacade.ts", &diags));
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
test "a contract attesting false for an enforced posture refuses" {
|
|
459
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
460
|
+
defer arena_state.deinit();
|
|
461
|
+
const arena = arena_state.allocator();
|
|
462
|
+
for ([_]struct { needle: []const u8, replacement: []const u8, fragment: []const u8 }{
|
|
463
|
+
.{ .needle = "\"deterministic\": true", .replacement = "\"deterministic\": false", .fragment = "attests deterministic: false" },
|
|
464
|
+
.{ .needle = "\"async_free\": true", .replacement = "\"async_free\": false", .fragment = "attests async_free: false" },
|
|
465
|
+
}) |case| {
|
|
466
|
+
const source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, case.needle, case.replacement);
|
|
467
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
468
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
469
|
+
try testing.expectError(error.Refused, emitProfile(arena, parsed, default_entry, &diags));
|
|
470
|
+
var found = false;
|
|
471
|
+
for (diags.list.items) |item| {
|
|
472
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, case.fragment) != null) found = true;
|
|
473
|
+
}
|
|
474
|
+
try testing.expect(found);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
test "the emitted profile parses as JSON with the expected top-level keys" {
|
|
479
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
480
|
+
defer arena_state.deinit();
|
|
481
|
+
const arena = arena_state.allocator();
|
|
482
|
+
const generated = try profileFromJson(arena, sidecar_mod.minimal_valid_json, default_entry);
|
|
483
|
+
const parsed = try std.json.parseFromSliceLeaky(std.json.Value, arena, generated, .{});
|
|
484
|
+
const top = parsed.object;
|
|
485
|
+
for ([_][]const u8{ "profile_format", "name", "entry", "emission", "abi", "exports", "sidecar", "determinism" }) |key| {
|
|
486
|
+
try testing.expect(top.contains(key));
|
|
487
|
+
}
|
|
488
|
+
const determinism = top.get("determinism").?.object;
|
|
489
|
+
try testing.expect(determinism.get("fences").?.array.items.len == determinism_fences.len);
|
|
490
|
+
// Teachings and remediations stay within the profile loader's
|
|
491
|
+
// string constraints: no control bytes below 0x20 except newline,
|
|
492
|
+
// 512 bytes at most.
|
|
493
|
+
for (determinism_fences) |fence| {
|
|
494
|
+
try testing.expect(fence.teaching.len <= 512);
|
|
495
|
+
for (fence.teaching) |char| try testing.expect(char >= 0x20 or char == '\n');
|
|
496
|
+
}
|
|
497
|
+
for (determinism_teachings ++ determinism_remediations) |rider| {
|
|
498
|
+
try testing.expect(rider.text.len <= 512);
|
|
499
|
+
for (rider.text) |char| try testing.expect(char >= 0x20 or char == '\n');
|
|
500
|
+
}
|
|
501
|
+
}
|