@native-sdk/cli 0.5.2 → 0.5.4
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 +41 -1
- package/build.zig +264 -3
- package/native-sdk.d.ts +17 -1
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/rt/rt.zig +138 -3
- package/packages/core/sdk/core.ts +225 -3
- package/packages/core/sdk/events.ts +39 -2
- package/packages/core/src/checker.ts +1 -1
- package/packages/core/src/emitter.ts +2625 -262
- package/packages/core/src/infer.ts +53 -7
- package/skill-data/automation/SKILL.md +7 -4
- package/skill-data/core/references/app-model-runtime.md +3 -0
- package/skill-data/native-ui/SKILL.md +4 -2
- package/skill-data/ts-core/SKILL.md +9 -1
- package/src/app_runner/root.zig +106 -1
- package/src/app_runner/ts_core_main.zig +4 -0
- package/src/automation/protocol.zig +20 -1
- package/src/embed/host.zig +27 -0
- package/src/embed/tests.zig +187 -0
- package/src/embed/ui_host.zig +4 -0
- package/src/platform/linux/gtk_host.c +112 -3
- package/src/platform/linux/root.zig +71 -0
- package/src/platform/macos/appkit_host.h +59 -2
- package/src/platform/macos/appkit_host.m +663 -34
- package/src/platform/macos/cef_host.mm +262 -10
- package/src/platform/macos/root.zig +126 -4
- package/src/platform/null_platform.zig +298 -5
- package/src/platform/null_platform_tests.zig +39 -0
- package/src/platform/root.zig +36 -0
- package/src/platform/types.zig +247 -8
- package/src/platform/windows/root.zig +134 -0
- package/src/platform/windows/webview2_host.cpp +152 -40
- package/src/primitives/app_manifest/comptime_scene.zig +5 -0
- package/src/primitives/app_manifest/root.zig +1 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- package/src/primitives/canvas/chart.zig +7 -7
- package/src/primitives/canvas/chart_tests.zig +66 -0
- package/src/primitives/canvas/commands.zig +48 -0
- package/src/primitives/canvas/events.zig +161 -0
- package/src/primitives/canvas/font_ttf.zig +155 -8
- package/src/primitives/canvas/font_ttf_tests.zig +686 -0
- package/src/primitives/canvas/reference.zig +67 -8
- package/src/primitives/canvas/render_fingerprints.zig +8 -0
- package/src/primitives/canvas/root.zig +25 -0
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/ui.zig +46 -9
- package/src/primitives/canvas/ui_markup.zig +82 -34
- package/src/primitives/canvas/ui_markup_binary.zig +4 -1
- package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
- package/src/primitives/canvas/ui_markup_contract.zig +10 -1
- package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
- package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
- package/src/primitives/canvas/ui_markup_tests.zig +55 -9
- package/src/primitives/canvas/ui_markup_view.zig +81 -14
- package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
- package/src/primitives/canvas/ui_schema.zig +29 -0
- package/src/primitives/canvas/ui_schema_tests.zig +11 -11
- package/src/primitives/canvas/ui_tests.zig +31 -0
- package/src/primitives/canvas/vector.zig +258 -141
- package/src/primitives/canvas/widget_access.zig +1 -1
- package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
- package/src/primitives/canvas/widget_layout.zig +9 -4
- package/src/primitives/canvas/widget_render.zig +78 -40
- package/src/primitives/canvas/widget_render_controls.zig +15 -4
- package/src/primitives/canvas/widget_semantics.zig +6 -1
- package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
- package/src/primitives/canvas/widget_text_input.zig +118 -10
- package/src/primitives/canvas/widgets.zig +16 -0
- package/src/root.zig +12 -0
- package/src/runtime/api.zig +15 -0
- package/src/runtime/automation_commands.zig +33 -0
- package/src/runtime/automation_widget_dispatch.zig +64 -0
- package/src/runtime/bridge_payload.zig +7 -0
- package/src/runtime/bridge_responses.zig +5 -0
- package/src/runtime/builtin_bridge.zig +9 -4
- package/src/runtime/canvas_font_tests.zig +711 -5
- package/src/runtime/canvas_fonts.zig +120 -25
- package/src/runtime/canvas_frame_helpers.zig +15 -0
- package/src/runtime/canvas_image_tests.zig +221 -0
- package/src/runtime/canvas_images.zig +78 -19
- package/src/runtime/canvas_limits.zig +44 -8
- package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
- package/src/runtime/canvas_widget_events.zig +24 -4
- package/src/runtime/canvas_widget_runtime.zig +24 -7
- package/src/runtime/canvas_widget_state.zig +6 -1
- package/src/runtime/canvas_widget_text_tests.zig +277 -0
- package/src/runtime/core.zig +148 -9
- package/src/runtime/effects.zig +1664 -43
- package/src/runtime/effects_fetch_tests.zig +89 -1
- package/src/runtime/effects_file_tests.zig +41 -1
- package/src/runtime/effects_image_tests.zig +1304 -0
- package/src/runtime/effects_tests.zig +153 -0
- package/src/runtime/flow.zig +44 -0
- package/src/runtime/gpu_surface_events.zig +7 -0
- package/src/runtime/media_surface.zig +726 -0
- package/src/runtime/media_surface_tests.zig +1476 -0
- package/src/runtime/root.zig +20 -0
- package/src/runtime/session_blobs.zig +689 -0
- package/src/runtime/session_journal.zig +111 -20
- package/src/runtime/session_record.zig +27 -2
- package/src/runtime/session_replay.zig +168 -16
- package/src/runtime/session_tests.zig +2118 -4
- package/src/runtime/shell_layout.zig +7 -0
- package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
- package/src/runtime/testdata/fonts/OFL.txt +93 -0
- package/src/runtime/tests.zig +3 -0
- package/src/runtime/ts_core_host.zig +439 -4
- package/src/runtime/ts_core_host_tests.zig +41 -0
- package/src/runtime/ts_ui_app.zig +63 -0
- package/src/runtime/ui_app.zig +169 -6
- package/src/runtime/ui_app_tests.zig +334 -0
- package/src/runtime/ui_app_window_tests.zig +468 -0
- package/src/runtime/window_command_bridge_tests.zig +117 -0
- package/src/runtime/window_storage.zig +32 -0
- package/src/runtime/window_views.zig +61 -1
- package/src/tooling/manifest.zig +75 -0
- package/src/tooling/raw_manifest.zig +2 -0
- package/src/tooling/templates.zig +219 -1
package/src/runtime/effects.zig
CHANGED
|
@@ -64,6 +64,8 @@
|
|
|
64
64
|
|
|
65
65
|
const std = @import("std");
|
|
66
66
|
const builtin = @import("builtin");
|
|
67
|
+
const canvas = @import("canvas");
|
|
68
|
+
const canvas_limits = @import("canvas_limits.zig");
|
|
67
69
|
const platform = @import("../platform/root.zig");
|
|
68
70
|
const runtime_clock = @import("clock.zig");
|
|
69
71
|
|
|
@@ -120,6 +122,13 @@ pub const max_effect_queue_entries: usize = 64;
|
|
|
120
122
|
/// that found the queue full). Sized above the slot count so a burst of
|
|
121
123
|
/// rejected spawns in one update still surfaces individually.
|
|
122
124
|
pub const max_effect_pending_exits: usize = 32;
|
|
125
|
+
/// Inline capacity of the loop-side image-terminal stage — the
|
|
126
|
+
/// allocation-free everyday case. Unlike the pending ring the stage
|
|
127
|
+
/// never evicts: a burst past this spills to a heap ring sized by the
|
|
128
|
+
/// burst itself (each staged entry is one `loadImage` call's only
|
|
129
|
+
/// terminal, so storage is bounded by the caller's own call count
|
|
130
|
+
/// between drains).
|
|
131
|
+
pub const max_effect_pending_images_inline: usize = max_effect_pending_exits;
|
|
123
132
|
|
|
124
133
|
/// Maximum bytes of one fetch's URL.
|
|
125
134
|
pub const max_effect_url_bytes: usize = 2048;
|
|
@@ -209,13 +218,18 @@ pub const RegisteredImage = struct {
|
|
|
209
218
|
/// Type-erased handle to the runtime's window verbs, bound onto the
|
|
210
219
|
/// effects channel (`bindWindowActions`) so `update` can drive REAL OS
|
|
211
220
|
/// window actions — the seam behind app-drawn close/minimize controls
|
|
212
|
-
/// on chromeless windows
|
|
213
|
-
///
|
|
214
|
-
///
|
|
221
|
+
/// on chromeless windows and the menu-bar-app lifecycle (tray "Open"
|
|
222
|
+
/// shows the hidden window, tray "Quit" quits for real). Label-addressed
|
|
223
|
+
/// because labels are what apps declare (`ShellWindow.label`,
|
|
224
|
+
/// `WindowDescriptor.label`); the runtime side resolves them to live
|
|
225
|
+
/// window ids. `quit_fn` alone is app-scoped, not window-scoped.
|
|
226
|
+
/// Constructed by `UiApp`.
|
|
215
227
|
pub const WindowActionBinding = struct {
|
|
216
228
|
context: *anyopaque,
|
|
217
229
|
close_fn: *const fn (context: *anyopaque, window_label: []const u8) bool,
|
|
218
230
|
minimize_fn: *const fn (context: *anyopaque, window_label: []const u8) bool,
|
|
231
|
+
show_fn: *const fn (context: *anyopaque, window_label: []const u8) bool,
|
|
232
|
+
quit_fn: *const fn (context: *anyopaque) bool,
|
|
219
233
|
};
|
|
220
234
|
|
|
221
235
|
/// Type-erased handle to the embedding host's named-command services,
|
|
@@ -241,17 +255,20 @@ pub const HostCallBinding = struct {
|
|
|
241
255
|
cancel_fn: ?*const fn (context: *anyopaque, key: u64) void = null,
|
|
242
256
|
};
|
|
243
257
|
|
|
244
|
-
/// Window-action label capacity (`Effects.closeWindow`/`minimizeWindow
|
|
245
|
-
/// the mirror copies the last requested label so tests
|
|
258
|
+
/// Window-action label capacity (`Effects.closeWindow`/`minimizeWindow`/
|
|
259
|
+
/// `showWindow`): the mirror copies the last requested label so tests
|
|
260
|
+
/// can pin it.
|
|
246
261
|
pub const max_window_action_label = 64;
|
|
247
262
|
|
|
248
|
-
/// The window-action mirror: observable state for every close/minimize
|
|
249
|
-
/// request made through the channel, recorded before the
|
|
250
|
-
/// (and INSTEAD of it under the fake executor — hermetic
|
|
251
|
-
/// counts, live runs also perform the verb).
|
|
263
|
+
/// The window-action mirror: observable state for every close/minimize/
|
|
264
|
+
/// show/quit request made through the channel, recorded before the
|
|
265
|
+
/// runtime call (and INSTEAD of it under the fake executor — hermetic
|
|
266
|
+
/// tests pin the counts, live runs also perform the verb).
|
|
252
267
|
pub const WindowActionState = struct {
|
|
253
268
|
close_count: u32 = 0,
|
|
254
269
|
minimize_count: u32 = 0,
|
|
270
|
+
show_count: u32 = 0,
|
|
271
|
+
quit_count: u32 = 0,
|
|
255
272
|
last_label_buffer: [max_window_action_label]u8 = @splat(0),
|
|
256
273
|
last_label_len: usize = 0,
|
|
257
274
|
|
|
@@ -626,6 +643,155 @@ pub fn audioCachePath(buffer: []u8, cache_dir: []const u8, url: []const u8) ![]c
|
|
|
626
643
|
return std.fmt.bufPrint(buffer, "{s}/audio/{s}{s}", .{ cache_dir, hex, extension });
|
|
627
644
|
}
|
|
628
645
|
|
|
646
|
+
/// Derive the conventional cache file path for a URL image source:
|
|
647
|
+
/// `<cache_dir>/images/<hash>.<ext>` — `audioCachePath`'s convention
|
|
648
|
+
/// (first 16 bytes of the URL's SHA-256 in lowercase hex, the extension
|
|
649
|
+
/// kept as a decoder hint) under its own `images/` segment so the two
|
|
650
|
+
/// caches never collide and each clears independently.
|
|
651
|
+
pub fn imageCachePath(buffer: []u8, cache_dir: []const u8, url: []const u8) ![]const u8 {
|
|
652
|
+
if (cache_dir.len == 0 or url.len == 0) return error.InvalidImageOptions;
|
|
653
|
+
var digest: [32]u8 = undefined;
|
|
654
|
+
std.crypto.hash.sha2.Sha256.hash(url, &digest, .{});
|
|
655
|
+
const hex = std.fmt.bytesToHex(digest[0..16].*, .lower);
|
|
656
|
+
const tail = if (std.mem.lastIndexOfScalar(u8, url, '/')) |slash| url[slash + 1 ..] else url;
|
|
657
|
+
var extension: []const u8 = "";
|
|
658
|
+
if (std.mem.lastIndexOfScalar(u8, tail, '.')) |dot| {
|
|
659
|
+
const candidate = tail[dot..];
|
|
660
|
+
// A plausible extension only: short, and free of query/fragment
|
|
661
|
+
// syntax that would smuggle URL machinery into a file name.
|
|
662
|
+
if (candidate.len <= 8 and std.mem.indexOfAny(u8, candidate, "?#&") == null) {
|
|
663
|
+
extension = candidate;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return std.fmt.bufPrint(buffer, "{s}/images/{s}{s}", .{ cache_dir, hex, extension });
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/// The temp path one cache install writes before its atomic rename
|
|
670
|
+
/// into `cache_path`. The name must be WRITER-unique, not merely
|
|
671
|
+
/// url-unique or channel-unique: two concurrent loads of the same URL
|
|
672
|
+
/// install toward the same cache path, and a shared temp would let one
|
|
673
|
+
/// writer truncate the other's bytes mid-write — or rename a
|
|
674
|
+
/// half-written file into place. The slot generation alone cannot
|
|
675
|
+
/// carry that uniqueness: it is channel-local, so two Effects channels
|
|
676
|
+
/// in one process — or two app processes sharing the platform cache
|
|
677
|
+
/// directory — can reach the same generation concurrently and collide.
|
|
678
|
+
/// `token` is the install's own random draw (`installImageCache` takes
|
|
679
|
+
/// it from the operation's executor io), unique across channels and
|
|
680
|
+
/// processes alike; the generation stays in the name purely as debris
|
|
681
|
+
/// provenance — a leftover `.partial` names which in-flight operation
|
|
682
|
+
/// a hard crash interrupted. This function stays a pure formatter on
|
|
683
|
+
/// every compile target: the entropy lives with the caller, and no
|
|
684
|
+
/// freestanding build can ever run an install (there is no executor io
|
|
685
|
+
/// and no cache directory there), so no target gate is needed here.
|
|
686
|
+
pub fn imageCachePartialPath(buffer: []u8, cache_path: []const u8, generation: u32, token: u64) ![]const u8 {
|
|
687
|
+
if (cache_path.len == 0) return error.InvalidImageOptions;
|
|
688
|
+
return std.fmt.bufPrint(buffer, "{s}.{d}-{x:0>16}.partial", .{ cache_path, generation, token });
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/// Longest local path (and cache path) one `loadImage` source may name,
|
|
692
|
+
/// mirroring the file effect's path bound; the URL keeps the fetch
|
|
693
|
+
/// bound (`max_effect_url_bytes`). Longer strings deliver exactly one
|
|
694
|
+
/// `.rejected` image result Msg.
|
|
695
|
+
pub const max_effect_image_path_bytes: usize = max_effect_file_path_bytes;
|
|
696
|
+
|
|
697
|
+
/// Maximum ENCODED source bytes one `loadImage` accepts — from a local
|
|
698
|
+
/// file, the URL cache, or the network alike. Sized past the decoded
|
|
699
|
+
/// pixel bound (`canvas_limits.max_registered_canvas_image_pixel_bytes`)
|
|
700
|
+
/// by the same 1/4 margin the decode scratch carries: an encoded stream
|
|
701
|
+
/// larger than that cannot decode inside the registered-image budget on
|
|
702
|
+
/// any host, so hauling more bytes would only defer the same
|
|
703
|
+
/// `.too_large` answer. Unlike fetch bodies there is no truncated
|
|
704
|
+
/// delivery — a cut image can never decode, so over-bound sources fail
|
|
705
|
+
/// whole with `.too_large`, never arrive clipped.
|
|
706
|
+
pub const max_effect_image_bytes: usize = canvas_limits.max_registered_canvas_image_pixel_bytes +
|
|
707
|
+
canvas_limits.max_registered_canvas_image_pixel_bytes / 4;
|
|
708
|
+
|
|
709
|
+
/// Bytes of an image effect result's content address in the session
|
|
710
|
+
/// journal: the first half of the source bytes' SHA-256, the
|
|
711
|
+
/// `audioCachePath` hashing convention. The journal record carries hash
|
|
712
|
+
/// and length; the bytes themselves live in the session blob store.
|
|
713
|
+
pub const effect_image_blob_hash_len: usize = 16;
|
|
714
|
+
|
|
715
|
+
/// The terminal outcome of one `loadImage` effect — exactly one Msg per
|
|
716
|
+
/// load, `.loaded` or one failure class, never silence. The classes are
|
|
717
|
+
/// the union of the source stages and the registered-image API's own
|
|
718
|
+
/// errors, so the effect fails with the same vocabulary the direct
|
|
719
|
+
/// `registerCanvasImageBytes` path uses.
|
|
720
|
+
pub const EffectImageOutcome = enum(u8) {
|
|
721
|
+
/// The pixels are registered under the requested id; `width` and
|
|
722
|
+
/// `height` report what the platform codec decoded.
|
|
723
|
+
loaded,
|
|
724
|
+
/// The request never ran: an invalid id (0, or the reserved
|
|
725
|
+
/// media-surface namespace), no source at all, an over-bound
|
|
726
|
+
/// path/url, a non-http(s) url, all slots busy, or a duplicate
|
|
727
|
+
/// active key.
|
|
728
|
+
rejected,
|
|
729
|
+
/// The local path does not exist and no url was given to fall
|
|
730
|
+
/// through to.
|
|
731
|
+
not_found,
|
|
732
|
+
/// The local read failed for any reason but absence (permissions, a
|
|
733
|
+
/// directory, disk errors). A present-but-unreadable file never
|
|
734
|
+
/// falls through to the url — retrying a different source would
|
|
735
|
+
/// mask the real problem, the audio cascade's rule.
|
|
736
|
+
io_failed,
|
|
737
|
+
/// DNS, TCP, or the connect phase failed (the fetch taxonomy).
|
|
738
|
+
connect_failed,
|
|
739
|
+
/// TLS setup failed.
|
|
740
|
+
tls_failed,
|
|
741
|
+
/// The HTTP exchange broke mid-protocol.
|
|
742
|
+
protocol_failed,
|
|
743
|
+
/// The whole cascade exceeded the effect timeout.
|
|
744
|
+
timed_out,
|
|
745
|
+
/// The server answered with a non-2xx status — carried in
|
|
746
|
+
/// `EffectImageResult.status`. The body is discarded, never decoded
|
|
747
|
+
/// (an error page is not an image).
|
|
748
|
+
http_status,
|
|
749
|
+
/// `cancel(id)` ended the load before its result was delivered.
|
|
750
|
+
cancelled,
|
|
751
|
+
/// The source bytes exceed `max_effect_image_bytes`, or the decoded
|
|
752
|
+
/// pixels exceed the registered-image slot bound
|
|
753
|
+
/// (`error.ImageTooLarge`) — one budget class either way.
|
|
754
|
+
too_large,
|
|
755
|
+
/// The host has no image codec (`error.UnsupportedService`), or no
|
|
756
|
+
/// image registry is bound to this channel.
|
|
757
|
+
unsupported,
|
|
758
|
+
/// The platform codec could not decode the bytes
|
|
759
|
+
/// (`error.ImageDecodeFailed`, impossible decoded dimensions).
|
|
760
|
+
decode_failed,
|
|
761
|
+
/// Every registered-image slot holds another id
|
|
762
|
+
/// (`error.ImageRegistryFull`).
|
|
763
|
+
registry_full,
|
|
764
|
+
/// The host refused the memory the registration needed
|
|
765
|
+
/// (`error.OutOfMemory` — the registry slot's lazily allocated
|
|
766
|
+
/// pixel buffer). The bytes may be perfectly valid: this is
|
|
767
|
+
/// resource exhaustion, not corruption, so it gets its own class —
|
|
768
|
+
/// calling it `decode_failed` would tell the app to distrust a
|
|
769
|
+
/// source that decodes fine on retry. Named for the failing stage
|
|
770
|
+
/// like its siblings (`io_failed`, `decode_failed`): the
|
|
771
|
+
/// allocation failed.
|
|
772
|
+
alloc_failed,
|
|
773
|
+
};
|
|
774
|
+
|
|
775
|
+
/// Payload for `on_result` Msg constructors of image loads. All plain
|
|
776
|
+
/// data — safe to store in the model (the decoded pixels live in the
|
|
777
|
+
/// runtime's registered-image storage, referenced by id from views).
|
|
778
|
+
pub const EffectImageResult = struct {
|
|
779
|
+
/// The requested ImageId, echoed verbatim (it doubles as the effect
|
|
780
|
+
/// key — see `LoadImageOptions.id`).
|
|
781
|
+
id: u64,
|
|
782
|
+
outcome: EffectImageOutcome = .loaded,
|
|
783
|
+
/// Decoded dimensions for `.loaded`; 0 otherwise.
|
|
784
|
+
width: usize = 0,
|
|
785
|
+
height: usize = 0,
|
|
786
|
+
/// The HTTP status for url loads that performed an exchange
|
|
787
|
+
/// (`.http_status`, and `.loaded` from the network); 0 when none
|
|
788
|
+
/// occurred — local paths and cache hits. 0 is signal, not a
|
|
789
|
+
/// missing value: a cache hit is a real `.loaded` with no exchange
|
|
790
|
+
/// behind it, so apps can distinguish a network load from a cached
|
|
791
|
+
/// one, and fabricating the origin's status for it would lie.
|
|
792
|
+
status: u16 = 0,
|
|
793
|
+
};
|
|
794
|
+
|
|
629
795
|
/// Base platform timer id for fx timers: slot N arms the platform timer
|
|
630
796
|
/// `effect_timer_platform_id_base + N`. Lives in the framework-reserved
|
|
631
797
|
/// id range (`platform.reserved_timer_id_base`) with an `0x00f7_0000`
|
|
@@ -668,6 +834,13 @@ pub const EffectResultKind = enum(u8) {
|
|
|
668
834
|
/// launches with no variables set) re-derive from the launch
|
|
669
835
|
/// configuration exactly as before.
|
|
670
836
|
env = 10,
|
|
837
|
+
/// One `loadImage` terminal (`EffectImageResult`). The record's
|
|
838
|
+
/// `payload` carries the ENCODED source bytes as they leave the
|
|
839
|
+
/// drain — the loaded bytes ARE the effect result — and the session
|
|
840
|
+
/// recorder moves them into the content-addressed blob store,
|
|
841
|
+
/// journaling `image_blob_hash`/`image_blob_len` in their place
|
|
842
|
+
/// (journal format v7).
|
|
843
|
+
image = 11,
|
|
671
844
|
};
|
|
672
845
|
|
|
673
846
|
/// Journaled wall-clock reads buffered for replay (`Effects.wallMs`).
|
|
@@ -727,6 +900,19 @@ pub const EffectResultRecord = struct {
|
|
|
727
900
|
/// the honest non-determinism (a real FFT of real audio) recorded at
|
|
728
901
|
/// the boundary so replay repaints identical bars.
|
|
729
902
|
audio_bands: [platform.audio_spectrum_band_count]u8 = @splat(0),
|
|
903
|
+
/// `.image` records: the delivered terminal outcome and the decoded
|
|
904
|
+
/// dimensions (0 unless `.loaded`); the HTTP status rides the
|
|
905
|
+
/// shared `status` field (0 when no exchange occurred — local
|
|
906
|
+
/// paths, cache hits — see `EffectImageResult.status`).
|
|
907
|
+
image_outcome: EffectImageOutcome = .loaded,
|
|
908
|
+
image_width: u64 = 0,
|
|
909
|
+
image_height: u64 = 0,
|
|
910
|
+
/// `.image` records: the content address of the journaled source
|
|
911
|
+
/// bytes in the session blob store, filled by the recorder when it
|
|
912
|
+
/// moves `payload` out of line. All-zero when the terminal carried
|
|
913
|
+
/// no bytes (source-stage failures).
|
|
914
|
+
image_blob_hash: [effect_image_blob_hash_len]u8 = @splat(0),
|
|
915
|
+
image_blob_len: u64 = 0,
|
|
730
916
|
};
|
|
731
917
|
|
|
732
918
|
/// Type-erased sink the drain reports every delivered result to while a
|
|
@@ -907,6 +1093,42 @@ fn classifyFetchError(err: anyerror) EffectFetchOutcome {
|
|
|
907
1093
|
};
|
|
908
1094
|
}
|
|
909
1095
|
|
|
1096
|
+
/// Map a registered-image API error onto the image effect's failure
|
|
1097
|
+
/// taxonomy — the SAME classes the direct `registerCanvasImageBytes`
|
|
1098
|
+
/// path raises, flattened for the one terminal Msg.
|
|
1099
|
+
fn classifyImageRegisterError(err: anyerror) EffectImageOutcome {
|
|
1100
|
+
return switch (err) {
|
|
1101
|
+
error.UnsupportedService => .unsupported,
|
|
1102
|
+
error.ImageTooLarge => .too_large,
|
|
1103
|
+
error.ImageRegistryFull => .registry_full,
|
|
1104
|
+
// Resource exhaustion is its own class, never `decode_failed`:
|
|
1105
|
+
// the registry allocates each slot's pixel buffer lazily at its
|
|
1106
|
+
// first registration, and an OOM there says nothing about the
|
|
1107
|
+
// bytes — reporting valid bytes as corrupt would send the app
|
|
1108
|
+
// chasing its source instead of its memory.
|
|
1109
|
+
error.OutOfMemory => .alloc_failed,
|
|
1110
|
+
// Invalid ids are refused at issue time, before any I/O; a
|
|
1111
|
+
// decode that produced impossible dimensions is a decode
|
|
1112
|
+
// failure like any other undecodable stream.
|
|
1113
|
+
error.InvalidImageId => .rejected,
|
|
1114
|
+
else => .decode_failed,
|
|
1115
|
+
};
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
/// Map a network-phase error of the image cascade onto the image
|
|
1119
|
+
/// taxonomy, riding the fetch classifier so the two effects never
|
|
1120
|
+
/// disagree about what a DNS failure is called.
|
|
1121
|
+
fn classifyImageFetchError(err: anyerror) EffectImageOutcome {
|
|
1122
|
+
return switch (classifyFetchError(err)) {
|
|
1123
|
+
.connect_failed => .connect_failed,
|
|
1124
|
+
.tls_failed => .tls_failed,
|
|
1125
|
+
.timed_out => .timed_out,
|
|
1126
|
+
.cancelled => .cancelled,
|
|
1127
|
+
.rejected => .rejected,
|
|
1128
|
+
.ok, .protocol_failed => .protocol_failed,
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
|
|
910
1132
|
pub fn Effects(comptime Msg: type) type {
|
|
911
1133
|
return struct {
|
|
912
1134
|
const Self = @This();
|
|
@@ -919,6 +1141,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
919
1141
|
pub const TimerMsgFn = *const fn (timer: EffectTimer) Msg;
|
|
920
1142
|
pub const AudioMsgFn = *const fn (event: EffectAudio) Msg;
|
|
921
1143
|
pub const HostMsgFn = *const fn (result: EffectHostResult) Msg;
|
|
1144
|
+
pub const ImageMsgFn = *const fn (result: EffectImageResult) Msg;
|
|
922
1145
|
|
|
923
1146
|
/// Comptime Msg constructor for `on_line`, following
|
|
924
1147
|
/// `canvas.Ui(Msg).inputMsg`: `lineMsg(.agent_line)` builds
|
|
@@ -1015,6 +1238,18 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1015
1238
|
}.make;
|
|
1016
1239
|
}
|
|
1017
1240
|
|
|
1241
|
+
/// Comptime Msg constructor for `on_result` of image loads:
|
|
1242
|
+
/// `imageMsg(.cover_loaded)` builds
|
|
1243
|
+
/// `Msg{ .cover_loaded = result }` — the variant's payload type
|
|
1244
|
+
/// must be `native_sdk.EffectImageResult`.
|
|
1245
|
+
pub fn imageMsg(comptime tag: std.meta.Tag(Msg)) ImageMsgFn {
|
|
1246
|
+
return struct {
|
|
1247
|
+
fn make(result: EffectImageResult) Msg {
|
|
1248
|
+
return @unionInit(Msg, @tagName(tag), result);
|
|
1249
|
+
}
|
|
1250
|
+
}.make;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1018
1253
|
pub const SpawnOptions = struct {
|
|
1019
1254
|
/// Caller-chosen identity, stored in the model. Must not
|
|
1020
1255
|
/// collide with another still-running effect.
|
|
@@ -1258,6 +1493,63 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1258
1493
|
on_event: ?AudioMsgFn = null,
|
|
1259
1494
|
};
|
|
1260
1495
|
|
|
1496
|
+
pub const LoadImageOptions = struct {
|
|
1497
|
+
/// The ImageId the decoded pixels register under — model-
|
|
1498
|
+
/// owned, chosen by the app, exactly the id `image`/`avatar`
|
|
1499
|
+
/// widgets reference. It doubles as the effect key: image
|
|
1500
|
+
/// loads share the `max_effects` slots and the key space
|
|
1501
|
+
/// with spawns, fetches, and file effects. Id 0 (the
|
|
1502
|
+
/// no-image sentinel) and the reserved media-surface
|
|
1503
|
+
/// namespace (`canvas.media_surface_image_id_bit`) are
|
|
1504
|
+
/// refused with one `.rejected` result, mirroring
|
|
1505
|
+
/// `registerCanvasImage`.
|
|
1506
|
+
id: u64,
|
|
1507
|
+
/// Local file path, tried FIRST — the audio cascade's rule:
|
|
1508
|
+
/// a present-but-missing file falls through to `url` when
|
|
1509
|
+
/// one is given; every other local failure is terminal
|
|
1510
|
+
/// (retrying a different source would mask the real
|
|
1511
|
+
/// problem). Bounded by `max_effect_image_path_bytes`.
|
|
1512
|
+
path: []const u8 = "",
|
|
1513
|
+
/// http(s) source, tried when the local path is absent or
|
|
1514
|
+
/// missing. A verified cache entry at `cache_path` loads
|
|
1515
|
+
/// locally (no network); otherwise the bytes are fetched
|
|
1516
|
+
/// whole and installed into the cache for next time. Empty
|
|
1517
|
+
/// means local-only.
|
|
1518
|
+
url: []const u8 = "",
|
|
1519
|
+
/// Where the URL's bytes are cached, and the cache policy
|
|
1520
|
+
/// in one field: empty disables caching. Derive it with
|
|
1521
|
+
/// `imageCachePath` for the content-addressed convention.
|
|
1522
|
+
/// The fetch writes beside this path and atomically renames
|
|
1523
|
+
/// into place only after the size verifies, so a partial
|
|
1524
|
+
/// download never occupies the cache name.
|
|
1525
|
+
cache_path: []const u8 = "",
|
|
1526
|
+
/// The source's known byte size (from a manifest), the
|
|
1527
|
+
/// cache integrity gate: a cache entry whose size disagrees
|
|
1528
|
+
/// is discarded and re-fetched, and a finished download
|
|
1529
|
+
/// that disagrees is never installed. Zero means "unknown"
|
|
1530
|
+
/// — existence alone then qualifies a cache entry.
|
|
1531
|
+
expected_bytes: u64 = 0,
|
|
1532
|
+
/// Whole-cascade timeout in milliseconds (local probe,
|
|
1533
|
+
/// cache read, and network fetch together); expiry delivers
|
|
1534
|
+
/// the result with outcome `.timed_out`.
|
|
1535
|
+
timeout_ms: u32 = default_effect_fetch_timeout_ms,
|
|
1536
|
+
/// Msg constructor the ONE terminal result flows through.
|
|
1537
|
+
/// Without one the load still runs (and registers on
|
|
1538
|
+
/// success); the app just hears nothing back.
|
|
1539
|
+
on_result: ?ImageMsgFn = null,
|
|
1540
|
+
};
|
|
1541
|
+
|
|
1542
|
+
/// A recorded image-load request, exposed by the fake executor
|
|
1543
|
+
/// for test assertions. Slices point into slot storage and stay
|
|
1544
|
+
/// valid until the result is fed and drained.
|
|
1545
|
+
pub const ImageLoadRequest = struct {
|
|
1546
|
+
id: u64,
|
|
1547
|
+
path: []const u8,
|
|
1548
|
+
url: []const u8,
|
|
1549
|
+
cache_path: []const u8,
|
|
1550
|
+
expected_bytes: u64,
|
|
1551
|
+
};
|
|
1552
|
+
|
|
1261
1553
|
/// A recorded fx timer, exposed by the fake executor for test
|
|
1262
1554
|
/// assertions.
|
|
1263
1555
|
pub const TimerRequest = struct {
|
|
@@ -1364,9 +1656,9 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1364
1656
|
/// thereby retires) it.
|
|
1365
1657
|
const SlotState = enum(u8) { idle, running, done, draining };
|
|
1366
1658
|
|
|
1367
|
-
const SlotKind = enum(u8) { spawn, fetch, file, clipboard, host };
|
|
1659
|
+
const SlotKind = enum(u8) { spawn, fetch, file, clipboard, host, image };
|
|
1368
1660
|
|
|
1369
|
-
const EntryKind = enum(u8) { line, exit, response, file, clipboard, host };
|
|
1661
|
+
const EntryKind = enum(u8) { line, exit, response, file, clipboard, host, image };
|
|
1370
1662
|
|
|
1371
1663
|
const Entry = struct {
|
|
1372
1664
|
kind: EntryKind = .line,
|
|
@@ -1409,12 +1701,27 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1409
1701
|
collect_len: u32 = 0,
|
|
1410
1702
|
collect_truncated: bool = false,
|
|
1411
1703
|
stderr_truncated: bool = false,
|
|
1704
|
+
/// `.image` entries: the source stage's outcome (`.loaded`
|
|
1705
|
+
/// means bytes are staged in the slot buffer and the drain
|
|
1706
|
+
/// decodes + registers them; anything else is the terminal
|
|
1707
|
+
/// failure class as-is).
|
|
1708
|
+
image_outcome: EffectImageOutcome = .loaded,
|
|
1709
|
+
/// `.image` entries fed with a RECORDED terminal (session
|
|
1710
|
+
/// replay): the drain delivers the journaled outcome and
|
|
1711
|
+
/// dimensions verbatim — re-registration is best-effort
|
|
1712
|
+
/// presentation, never the Msg source — so a replay host
|
|
1713
|
+
/// whose codec differs from the recording host's still
|
|
1714
|
+
/// replays the identical Msg stream.
|
|
1715
|
+
image_fed: bool = false,
|
|
1716
|
+
image_fed_width: u64 = 0,
|
|
1717
|
+
image_fed_height: u64 = 0,
|
|
1412
1718
|
line_fn: ?LineMsgFn = null,
|
|
1413
1719
|
exit_fn: ?ExitMsgFn = null,
|
|
1414
1720
|
response_fn: ?ResponseMsgFn = null,
|
|
1415
1721
|
file_fn: ?FileMsgFn = null,
|
|
1416
1722
|
clipboard_fn: ?ClipboardMsgFn = null,
|
|
1417
1723
|
host_fn: ?HostMsgFn = null,
|
|
1724
|
+
image_fn: ?ImageMsgFn = null,
|
|
1418
1725
|
/// `.line` entries whose payload exceeds the inline buffer
|
|
1419
1726
|
/// (a raised `max_line_bytes` bound): the bytes ride in this
|
|
1420
1727
|
/// heap allocation instead of `line_bytes`. Owned by the
|
|
@@ -1444,6 +1751,20 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1444
1751
|
/// `takeAudioMsg`. Non-resolving entries (rejections and
|
|
1445
1752
|
/// synchronous failures) are fully formed at enqueue.
|
|
1446
1753
|
audio: struct { event: EffectAudio, audio_fn: ?AudioMsgFn, resolve: bool },
|
|
1754
|
+
/// Loop-thread image terminals (rejections, fake cancels,
|
|
1755
|
+
/// feed fallbacks) — always payload-free, fully formed at
|
|
1756
|
+
/// enqueue. `regenerates` is true only for pre-executor
|
|
1757
|
+
/// validation refusals: the same deterministic checks in
|
|
1758
|
+
/// `loadImage` refuse again under session replay, so their
|
|
1759
|
+
/// journaled records are skipped rather than fed. Every
|
|
1760
|
+
/// other loop-side terminal — fake cancels, feed fallbacks,
|
|
1761
|
+
/// an executor that could not start the load — is executor
|
|
1762
|
+
/// truth the replayed request cannot reproduce and must be
|
|
1763
|
+
/// fed from the journal. Image terminals never occupy the
|
|
1764
|
+
/// lossy ring itself: they stage in `pending_images` (see
|
|
1765
|
+
/// there for why they must be non-lossy) and take this
|
|
1766
|
+
/// union shape only at drain time, in `takePendingMsg`.
|
|
1767
|
+
image: struct { result: EffectImageResult, image_fn: ?ImageMsgFn, regenerates: bool },
|
|
1447
1768
|
|
|
1448
1769
|
fn addDropped(pending: *PendingMsg, count: u32) void {
|
|
1449
1770
|
switch (pending.*) {
|
|
@@ -1460,6 +1781,13 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1460
1781
|
// EffectHostResult carries none: its terminals are
|
|
1461
1782
|
// one-per-request by construction.
|
|
1462
1783
|
.host => {},
|
|
1784
|
+
// Image terminals never enter the ring (they stage
|
|
1785
|
+
// in the non-lossy `pending_images`), so neither
|
|
1786
|
+
// overflow arm can ever see one. EffectImageResult
|
|
1787
|
+
// carries no drop counter to fold a loss into —
|
|
1788
|
+
// eviction here would silently break the
|
|
1789
|
+
// exactly-one-terminal-per-load contract.
|
|
1790
|
+
.image => unreachable,
|
|
1463
1791
|
}
|
|
1464
1792
|
}
|
|
1465
1793
|
|
|
@@ -1472,10 +1800,32 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1472
1800
|
.timer => 0,
|
|
1473
1801
|
.audio => 0,
|
|
1474
1802
|
.host => 0,
|
|
1803
|
+
// Never in the ring; see `addDropped`.
|
|
1804
|
+
.image => unreachable,
|
|
1475
1805
|
};
|
|
1476
1806
|
}
|
|
1477
1807
|
};
|
|
1478
1808
|
|
|
1809
|
+
/// One loop-side image terminal awaiting drain, staged outside
|
|
1810
|
+
/// the shared pending ring. The stage is NON-LOSSY, unlike the
|
|
1811
|
+
/// ring: an image load's contract is exactly one terminal per
|
|
1812
|
+
/// load, `EffectImageResult` carries no drop counter to make a
|
|
1813
|
+
/// loss visible, and loop-side validation rejections are
|
|
1814
|
+
/// unbounded per dispatch — every refused `loadImage` stages
|
|
1815
|
+
/// one entry here before the next drain runs, so ring eviction
|
|
1816
|
+
/// would leave the issuing model waiting forever on a terminal
|
|
1817
|
+
/// that silently vanished. Worker-origin image terminals are
|
|
1818
|
+
/// bounded by the effect slots and ride the completion queue,
|
|
1819
|
+
/// never this stage. `seq` orders staged entries against ring
|
|
1820
|
+
/// entries so the drain preserves enqueue order across both
|
|
1821
|
+
/// structures (`takePendingMsg` merges by it).
|
|
1822
|
+
const PendingImage = struct {
|
|
1823
|
+
seq: u64,
|
|
1824
|
+
result: EffectImageResult,
|
|
1825
|
+
image_fn: ?ImageMsgFn,
|
|
1826
|
+
regenerates: bool,
|
|
1827
|
+
};
|
|
1828
|
+
|
|
1479
1829
|
/// Everything a real spawn worker's BLOCKING phase may touch,
|
|
1480
1830
|
/// held out-of-line from the channel on its own heap block —
|
|
1481
1831
|
/// the spawn twin of `FileWorkerContext`, and the same
|
|
@@ -1660,6 +2010,17 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1660
2010
|
collect_buffer: ?[]u8 = null,
|
|
1661
2011
|
collect_len: usize = 0,
|
|
1662
2012
|
collect_truncated: bool = false,
|
|
2013
|
+
/// A `.lines` spawn's posted-but-undelivered exit marker:
|
|
2014
|
+
/// set alongside the exit entry's enqueue (worker commit or
|
|
2015
|
+
/// feed), cleared by the drain the moment it dequeues the
|
|
2016
|
+
/// generation-matched exit — the delivery instant the other
|
|
2017
|
+
/// families mark by their buffer handoff (`fetch_buffer`,
|
|
2018
|
+
/// `collect_buffer`). While set, the slot parks in
|
|
2019
|
+
/// `.draining` and its key stays occupied
|
|
2020
|
+
/// (`findUndeliveredTerminalSlot`). Written by the worker
|
|
2021
|
+
/// before its `.draining` release store (the worker's last
|
|
2022
|
+
/// slot access); loop-thread only after that.
|
|
2023
|
+
exit_undelivered: bool = false,
|
|
1663
2024
|
/// Ring of the child's most recent stderr bytes. Written by
|
|
1664
2025
|
/// the stderr reader (worker-side thread in real mode, loop
|
|
1665
2026
|
/// thread in fake mode); read only after the child is done.
|
|
@@ -1671,6 +2032,22 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1671
2032
|
file_op: EffectFileOp = .read,
|
|
1672
2033
|
// ---- clipboard-only fields (kind == .clipboard) ----
|
|
1673
2034
|
clipboard_op: EffectClipboardOp = .write,
|
|
2035
|
+
// ---- image-only fields (kind == .image) ----
|
|
2036
|
+
on_image: ?ImageMsgFn = null,
|
|
2037
|
+
/// The local source path (the URL rides `url_storage`, a
|
|
2038
|
+
/// slot being one occupancy at a time — but path and url
|
|
2039
|
+
/// COEXIST in an image cascade, so the path gets its own
|
|
2040
|
+
/// storage).
|
|
2041
|
+
image_path_storage: [max_effect_image_path_bytes]u8 = undefined,
|
|
2042
|
+
image_path_len: usize = 0,
|
|
2043
|
+
image_cache_storage: [max_effect_image_path_bytes]u8 = undefined,
|
|
2044
|
+
image_cache_len: usize = 0,
|
|
2045
|
+
image_expected_bytes: u64 = 0,
|
|
2046
|
+
/// Terminal source-stage state, written by the image task
|
|
2047
|
+
/// before `fetch_done` (the shared completion latch):
|
|
2048
|
+
/// `.loaded` means encoded bytes are staged in
|
|
2049
|
+
/// `fetch_buffer` for the drain to decode + register.
|
|
2050
|
+
image_outcome: EffectImageOutcome = .loaded,
|
|
1674
2051
|
// ---- fetch/file fields ----
|
|
1675
2052
|
/// `.stream` frames the response body into line entries;
|
|
1676
2053
|
/// `.buffered` delivers it whole on the terminal entry.
|
|
@@ -1734,10 +2111,29 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1734
2111
|
fn hostName(slot: *const Slot) []const u8 {
|
|
1735
2112
|
return slot.url_storage[0..slot.url_len];
|
|
1736
2113
|
}
|
|
2114
|
+
|
|
2115
|
+
fn imagePath(slot: *const Slot) []const u8 {
|
|
2116
|
+
return slot.image_path_storage[0..slot.image_path_len];
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
fn imageCache(slot: *const Slot) []const u8 {
|
|
2120
|
+
return slot.image_cache_storage[0..slot.image_cache_len];
|
|
2121
|
+
}
|
|
1737
2122
|
};
|
|
1738
2123
|
|
|
1739
2124
|
allocator: std.mem.Allocator,
|
|
1740
2125
|
executor: EffectExecutor = .real,
|
|
2126
|
+
/// Fake-executor convention: while set, every `loadImage` that
|
|
2127
|
+
/// parks a fake request completes IMMEDIATELY with these bytes
|
|
2128
|
+
/// (through `feedImageBytes`, the full decode→register path) —
|
|
2129
|
+
/// still at `loadImage` time, before the caller's dispatch
|
|
2130
|
+
/// returns. This is the deterministic stand-in for a real
|
|
2131
|
+
/// local-path or cache load finishing before the drain pass
|
|
2132
|
+
/// that spawned it ends: the chained-completion shape the
|
|
2133
|
+
/// drain's causal boundary (`DrainBoundary`) exists for. Test
|
|
2134
|
+
/// seam only; never set under session replay, where journaled
|
|
2135
|
+
/// terminals are the only delivery.
|
|
2136
|
+
fake_instant_image_bytes: ?[]const u8 = null,
|
|
1741
2137
|
/// Session-record sink: every drain-delivered result is reported
|
|
1742
2138
|
/// here (loop thread, right before its Msg runs through
|
|
1743
2139
|
/// `update`). Null outside recording.
|
|
@@ -1871,8 +2267,23 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1871
2267
|
/// Loop-thread-only ring: spawn/fetch rejections and fake-executor
|
|
1872
2268
|
/// terminals that found the queue full. Drained before the queue.
|
|
1873
2269
|
pending_exits: [max_effect_pending_exits]PendingMsg = undefined,
|
|
2270
|
+
/// Enqueue-order stamps for the ring entries (parallel to
|
|
2271
|
+
/// `pending_exits`), so the drain can merge ring entries with
|
|
2272
|
+
/// the separately staged image terminals in original order.
|
|
2273
|
+
pending_exit_seqs: [max_effect_pending_exits]u64 = undefined,
|
|
1874
2274
|
pending_exit_head: usize = 0,
|
|
1875
2275
|
pending_exit_len: usize = 0,
|
|
2276
|
+
/// Monotonic enqueue stamp shared by the pending ring and the
|
|
2277
|
+
/// image stage — the drain's merge key.
|
|
2278
|
+
pending_seq: u64 = 0,
|
|
2279
|
+
/// Loop-thread-only image-terminal stage (see `PendingImage`
|
|
2280
|
+
/// for the non-lossy contract). FIFO over `pending_images`
|
|
2281
|
+
/// until a burst outgrows it, then over `pending_image_spill`
|
|
2282
|
+
/// (freed when the stage drains empty, and at `deinit`).
|
|
2283
|
+
pending_images: [max_effect_pending_images_inline]PendingImage = undefined,
|
|
2284
|
+
pending_image_spill: []PendingImage = &.{},
|
|
2285
|
+
pending_image_head: usize = 0,
|
|
2286
|
+
pending_image_len: usize = 0,
|
|
1876
2287
|
/// Scratch the drained entry is copied into so its line slice
|
|
1877
2288
|
/// stays valid while `update` runs (recycled per drained Msg).
|
|
1878
2289
|
drain_scratch: Entry = .{},
|
|
@@ -2180,6 +2591,14 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2180
2591
|
self.allocator.free(buffer);
|
|
2181
2592
|
self.drain_heap_line = null;
|
|
2182
2593
|
}
|
|
2594
|
+
// Undrained staged image terminals are plain data — only a
|
|
2595
|
+
// burst's spill ring owns heap.
|
|
2596
|
+
if (self.pending_image_spill.len > 0) {
|
|
2597
|
+
self.allocator.free(self.pending_image_spill);
|
|
2598
|
+
self.pending_image_spill = &.{};
|
|
2599
|
+
}
|
|
2600
|
+
self.pending_image_head = 0;
|
|
2601
|
+
self.pending_image_len = 0;
|
|
2183
2602
|
// Sever the services binding last: the platform this pointer
|
|
2184
2603
|
// reaches into may be destroyed before the next call arrives
|
|
2185
2604
|
// (main's deferred app deinit runs after the runner's platform
|
|
@@ -2350,8 +2769,10 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2350
2769
|
/// copied before this returns), not an effect: no Msg follows.
|
|
2351
2770
|
/// Errors are the registry's (`error.InvalidImageId`,
|
|
2352
2771
|
/// `error.InvalidImageDimensions`, `error.ImageTooLarge`,
|
|
2353
|
-
/// `error.ImageRegistryFull
|
|
2354
|
-
///
|
|
2772
|
+
/// `error.ImageRegistryFull`, `error.OutOfMemory` — the slot's
|
|
2773
|
+
/// lazy pixel buffer could not be allocated; the registry is
|
|
2774
|
+
/// unchanged and the registration can be retried) plus
|
|
2775
|
+
/// `error.UnsupportedService` when no registry is bound.
|
|
2355
2776
|
pub fn registerImage(self: *Self, id: u64, width: usize, height: usize, rgba8: []const u8) anyerror!void {
|
|
2356
2777
|
const binding = self.images orelse return error.UnsupportedService;
|
|
2357
2778
|
return binding.register_fn(binding.context, id, width, height, rgba8);
|
|
@@ -2396,7 +2817,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2396
2817
|
if (options.max_line_bytes == 0 or options.max_line_bytes > max_effect_line_bytes_ceiling) {
|
|
2397
2818
|
return self.reject(options);
|
|
2398
2819
|
}
|
|
2399
|
-
if (self.
|
|
2820
|
+
if (self.keyOccupiedUntilDelivery(options.key)) return self.reject(options);
|
|
2400
2821
|
const slot_index = self.findIdleSlot() orelse return self.reject(options);
|
|
2401
2822
|
|
|
2402
2823
|
const slot = &self.slots[slot_index];
|
|
@@ -2408,6 +2829,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2408
2829
|
slot.on_line = options.on_line;
|
|
2409
2830
|
slot.on_exit = options.on_exit;
|
|
2410
2831
|
slot.on_response = null;
|
|
2832
|
+
slot.exit_undelivered = false;
|
|
2411
2833
|
slot.cancel_requested.store(false, .release);
|
|
2412
2834
|
// `cancelled_generation` is deliberately NOT reset: entries
|
|
2413
2835
|
// from a cancelled previous occupant may still sit in the
|
|
@@ -2549,7 +2971,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2549
2971
|
if (options.max_line_bytes == 0 or options.max_line_bytes > max_effect_line_bytes_ceiling) {
|
|
2550
2972
|
return self.rejectFetch(options);
|
|
2551
2973
|
}
|
|
2552
|
-
if (self.
|
|
2974
|
+
if (self.keyOccupiedUntilDelivery(options.key)) return self.rejectFetch(options);
|
|
2553
2975
|
const slot_index = self.findIdleSlot() orelse return self.rejectFetch(options);
|
|
2554
2976
|
|
|
2555
2977
|
const slot = &self.slots[slot_index];
|
|
@@ -2655,7 +3077,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2655
3077
|
if (file_path.len == 0 or file_path.len > max_effect_file_path_bytes) {
|
|
2656
3078
|
return self.rejectFile(key, op, on_result);
|
|
2657
3079
|
}
|
|
2658
|
-
if (self.
|
|
3080
|
+
if (self.keyOccupiedUntilDelivery(key)) return self.rejectFile(key, op, on_result);
|
|
2659
3081
|
const slot_index = self.findIdleSlot() orelse return self.rejectFile(key, op, on_result);
|
|
2660
3082
|
|
|
2661
3083
|
const slot = &self.slots[slot_index];
|
|
@@ -2746,6 +3168,141 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2746
3168
|
slot.worker_thread = thread;
|
|
2747
3169
|
}
|
|
2748
3170
|
|
|
3171
|
+
/// Load an image at runtime — the `Cmd.imageLoad` executor: the
|
|
3172
|
+
/// source cascade (local file first, then a verified cache
|
|
3173
|
+
/// entry, then the network) runs on a worker thread; the bytes
|
|
3174
|
+
/// it produces decode through the platform codec and register
|
|
3175
|
+
/// under `options.id` in the runtime's registered-image storage
|
|
3176
|
+
/// (the `registerCanvasImageBytes` seam — same slot budget,
|
|
3177
|
+
/// same pixel bound, same error classes); and exactly ONE
|
|
3178
|
+
/// terminal Msg follows through `on_result`: `.loaded` with the
|
|
3179
|
+
/// decoded width/height, or one failure class — never a crash,
|
|
3180
|
+
/// never silence. Views referencing the id repaint with the
|
|
3181
|
+
/// pixels on their next frame; `update` stays pure. Decode and
|
|
3182
|
+
/// registration run at drain time on the loop thread (the
|
|
3183
|
+
/// registry and its decode scratch are loop-thread state), so
|
|
3184
|
+
/// the journal records the encoded bytes exactly as the effect
|
|
3185
|
+
/// boundary delivered them and replay re-runs the same
|
|
3186
|
+
/// decode-and-register offline. Never fails from the caller's
|
|
3187
|
+
/// view: requests that cannot run deliver one `.rejected`
|
|
3188
|
+
/// result on the next drain. Image loads share the
|
|
3189
|
+
/// `max_effects` slots and the key space (keyed by `id`) with
|
|
3190
|
+
/// spawns, fetches, and file effects.
|
|
3191
|
+
pub fn loadImage(self: *Self, options: LoadImageOptions) void {
|
|
3192
|
+
self.reclaimSlots();
|
|
3193
|
+
// The same ids `registerCanvasImage` refuses, refused before
|
|
3194
|
+
// any I/O: 0 is the no-image sentinel, and the high bit is
|
|
3195
|
+
// the media-surface texture namespace.
|
|
3196
|
+
const id_invalid = options.id == 0 or (options.id & canvas.media_surface_image_id_bit) != 0;
|
|
3197
|
+
if (id_invalid or
|
|
3198
|
+
(options.path.len == 0 and options.url.len == 0) or
|
|
3199
|
+
options.path.len > max_effect_image_path_bytes or
|
|
3200
|
+
options.cache_path.len > max_effect_image_path_bytes or
|
|
3201
|
+
options.url.len > max_effect_url_bytes)
|
|
3202
|
+
{
|
|
3203
|
+
return self.rejectImage(options.id, options.on_result, true);
|
|
3204
|
+
}
|
|
3205
|
+
if (options.url.len > 0) {
|
|
3206
|
+
const uri = std.Uri.parse(options.url) catch return self.rejectImage(options.id, options.on_result, true);
|
|
3207
|
+
const scheme_ok = std.ascii.eqlIgnoreCase(uri.scheme, "http") or
|
|
3208
|
+
std.ascii.eqlIgnoreCase(uri.scheme, "https");
|
|
3209
|
+
if (!scheme_ok) return self.rejectImage(options.id, options.on_result, true);
|
|
3210
|
+
}
|
|
3211
|
+
// Occupied = running (any kind — the key space is shared),
|
|
3212
|
+
// OR any slot in the posted-but-undelivered terminal
|
|
3213
|
+
// window (`keyOccupiedUntilDelivery`). Delivery ends the
|
|
3214
|
+
// window — the drain takes the slot's delivery marker
|
|
3215
|
+
// BEFORE the terminal Msg reaches update — so a handler
|
|
3216
|
+
// that answers its own terminal by reloading the same id
|
|
3217
|
+
// (the gallery-refresh idiom) still parks as a fresh load.
|
|
3218
|
+
if (self.keyOccupiedUntilDelivery(options.id)) {
|
|
3219
|
+
return self.rejectImage(options.id, options.on_result, true);
|
|
3220
|
+
}
|
|
3221
|
+
const slot_index = self.findIdleSlot() orelse return self.rejectImage(options.id, options.on_result, true);
|
|
3222
|
+
|
|
3223
|
+
const slot = &self.slots[slot_index];
|
|
3224
|
+
// One spare byte past the source bound detects over-bound
|
|
3225
|
+
// files and bodies without a stat round trip (the file
|
|
3226
|
+
// effect's trick).
|
|
3227
|
+
// An allocation failure is NOT regenerable validation: the
|
|
3228
|
+
// replayed request allocates its own buffer and parks, so
|
|
3229
|
+
// this terminal must journal as executor truth and feed —
|
|
3230
|
+
// and the staged rejection holds the id until it drains
|
|
3231
|
+
// (`stagedImageOccupiesKey`), because replay's parked
|
|
3232
|
+
// request holds it through the same window.
|
|
3233
|
+
const buffer = self.allocator.alloc(u8, max_effect_image_bytes + 1) catch {
|
|
3234
|
+
return self.rejectImage(options.id, options.on_result, false);
|
|
3235
|
+
};
|
|
3236
|
+
slot.generation = self.next_generation;
|
|
3237
|
+
self.next_generation +%= 1;
|
|
3238
|
+
if (self.next_generation == 0) self.next_generation = 1;
|
|
3239
|
+
slot.key = options.id;
|
|
3240
|
+
slot.kind = .image;
|
|
3241
|
+
slot.on_line = null;
|
|
3242
|
+
slot.on_exit = null;
|
|
3243
|
+
slot.on_response = null;
|
|
3244
|
+
slot.on_file = null;
|
|
3245
|
+
slot.on_image = options.on_result;
|
|
3246
|
+
slot.timeout_ms = options.timeout_ms;
|
|
3247
|
+
slot.cancel_requested.store(false, .release);
|
|
3248
|
+
slot.fetch_done.store(false, .release);
|
|
3249
|
+
// `cancelled_generation` stays sticky, exactly as in `spawn`.
|
|
3250
|
+
slot.dropped_pending = 0;
|
|
3251
|
+
slot.dropped_total = 0;
|
|
3252
|
+
@memcpy(slot.url_storage[0..options.url.len], options.url);
|
|
3253
|
+
slot.url_len = options.url.len;
|
|
3254
|
+
@memcpy(slot.image_path_storage[0..options.path.len], options.path);
|
|
3255
|
+
slot.image_path_len = options.path.len;
|
|
3256
|
+
@memcpy(slot.image_cache_storage[0..options.cache_path.len], options.cache_path);
|
|
3257
|
+
slot.image_cache_len = options.cache_path.len;
|
|
3258
|
+
slot.image_expected_bytes = options.expected_bytes;
|
|
3259
|
+
// Pessimistic default: a task interrupted before it records
|
|
3260
|
+
// a terminal state must never read as a staged success.
|
|
3261
|
+
slot.image_outcome = .io_failed;
|
|
3262
|
+
if (slot.line_buffer) |old| {
|
|
3263
|
+
self.allocator.free(old);
|
|
3264
|
+
slot.line_buffer = null;
|
|
3265
|
+
}
|
|
3266
|
+
if (slot.fetch_buffer) |old| self.allocator.free(old);
|
|
3267
|
+
slot.fetch_buffer = buffer;
|
|
3268
|
+
slot.payload_len = 0;
|
|
3269
|
+
slot.body_len = 0;
|
|
3270
|
+
slot.fetch_status = 0;
|
|
3271
|
+
slot.fake = self.executor == .fake;
|
|
3272
|
+
slot.state.store(.running, .release);
|
|
3273
|
+
|
|
3274
|
+
if (slot.fake) {
|
|
3275
|
+
// Instant-load convention (see `fake_instant_image_bytes`):
|
|
3276
|
+
// complete the just-parked request before returning, the
|
|
3277
|
+
// deterministic mirror of a real local load racing the
|
|
3278
|
+
// drain pass that issued it. `EffectNotFound` cannot
|
|
3279
|
+
// happen (the slot parked two lines up), and a full
|
|
3280
|
+
// queue outside replay already delivered the loop-side
|
|
3281
|
+
// fallback terminal inside the feed — nothing is lost.
|
|
3282
|
+
if (self.fake_instant_image_bytes) |bytes| {
|
|
3283
|
+
self.feedImageBytes(options.id, bytes) catch {};
|
|
3284
|
+
}
|
|
3285
|
+
return;
|
|
3286
|
+
}
|
|
3287
|
+
|
|
3288
|
+
// Executor-start failures are NOT regenerable validation
|
|
3289
|
+
// either: under replay the fake executor parks the request
|
|
3290
|
+
// before ever touching io or threads, so these terminals
|
|
3291
|
+
// journal as executor truth and feed. Releasing the slot
|
|
3292
|
+
// here does not free the id — the staged rejection holds
|
|
3293
|
+
// it until the drain delivers (`stagedImageOccupiesKey`),
|
|
3294
|
+
// matching the parked replay request's window.
|
|
3295
|
+
const io = self.ensureIo() catch {
|
|
3296
|
+
self.releaseFetchSlot(slot);
|
|
3297
|
+
return self.rejectImage(options.id, options.on_result, false);
|
|
3298
|
+
};
|
|
3299
|
+
const thread = std.Thread.spawn(.{}, imageWorkerMain, .{ self, slot_index, slot.generation, io }) catch {
|
|
3300
|
+
self.releaseFetchSlot(slot);
|
|
3301
|
+
return self.rejectImage(options.id, options.on_result, false);
|
|
3302
|
+
};
|
|
3303
|
+
slot.worker_thread = thread;
|
|
3304
|
+
}
|
|
3305
|
+
|
|
2749
3306
|
/// Put text on the system clipboard through the platform
|
|
2750
3307
|
/// pasteboard — the same seam the runtime's cmd+C copy uses —
|
|
2751
3308
|
/// and deliver exactly one terminal Msg with an explicit
|
|
@@ -2784,7 +3341,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2784
3341
|
// Services are bound before init_fx/update ever run; a null
|
|
2785
3342
|
// here means a host without the platform clipboard arm.
|
|
2786
3343
|
if (!fake and self.services == null) return self.rejectClipboard(key, op, on_result);
|
|
2787
|
-
if (self.
|
|
3344
|
+
if (self.keyOccupiedUntilDelivery(key)) return self.rejectClipboard(key, op, on_result);
|
|
2788
3345
|
const slot_index = self.findIdleSlot() orelse return self.rejectClipboard(key, op, on_result);
|
|
2789
3346
|
|
|
2790
3347
|
const slot = &self.slots[slot_index];
|
|
@@ -2914,20 +3471,34 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2914
3471
|
return self.rejectHost(options.key, options.on_result);
|
|
2915
3472
|
}
|
|
2916
3473
|
if (!fake and self.host_calls == null) return self.rejectHost(options.key, options.on_result);
|
|
3474
|
+
// A staged non-regenerating image terminal holds the key
|
|
3475
|
+
// exactly like the slot windows below (see
|
|
3476
|
+
// `stagedImageOccupiesKey`): under replay that image
|
|
3477
|
+
// request is still parked until its journaled terminal
|
|
3478
|
+
// feeds, and the parked fake is what rejects this request
|
|
3479
|
+
// there.
|
|
3480
|
+
if (self.stagedImageOccupiesKey(options.key)) return self.rejectHost(options.key, options.on_result);
|
|
2917
3481
|
const slot_index = blk: {
|
|
2918
3482
|
// In flight = running (no answer yet) OR draining with
|
|
2919
3483
|
// an undelivered answer: both are replaced, dropping
|
|
2920
3484
|
// the old result silently (its queued entry dies by
|
|
2921
|
-
// generation mismatch at drain).
|
|
2922
|
-
// another kind is a key collision and rejects
|
|
2923
|
-
//
|
|
3485
|
+
// generation mismatch at drain). An occupancy of
|
|
3486
|
+
// another kind is a key collision and rejects — while
|
|
3487
|
+
// running AND through its posted-but-undelivered
|
|
3488
|
+
// terminal window (under session replay that request
|
|
3489
|
+
// is still a parked `.running` fake until its
|
|
3490
|
+
// journaled terminal feeds, so accepting the key here
|
|
3491
|
+
// live would diverge); a drained one is fully
|
|
3492
|
+
// delivered — its key is free.
|
|
2924
3493
|
var replaced: ?usize = null;
|
|
2925
3494
|
for (&self.slots, 0..) |*slot, index| {
|
|
2926
3495
|
const state = slot.state.load(.acquire);
|
|
2927
3496
|
if (state != .running and state != .draining) continue;
|
|
2928
3497
|
if (slot.key != options.key) continue;
|
|
2929
3498
|
if (slot.kind != .host) {
|
|
2930
|
-
if (state == .running
|
|
3499
|
+
if (state == .running or slotTerminalUndelivered(slot)) {
|
|
3500
|
+
return self.rejectHost(options.key, options.on_result);
|
|
3501
|
+
}
|
|
2931
3502
|
continue;
|
|
2932
3503
|
}
|
|
2933
3504
|
// Tell the host first: a late answer for the old
|
|
@@ -3082,6 +3653,15 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3082
3653
|
self.deliverLoopClipboard(.{ .key = key_copy, .op = op, .outcome = .cancelled }, clipboard_fn);
|
|
3083
3654
|
return;
|
|
3084
3655
|
}
|
|
3656
|
+
if (slot.kind == .image) {
|
|
3657
|
+
// No cascade ran: retire the slot and surface the
|
|
3658
|
+
// terminal result now.
|
|
3659
|
+
const image_fn = slot.on_image;
|
|
3660
|
+
const key_copy = slot.key;
|
|
3661
|
+
self.releaseFetchSlot(slot);
|
|
3662
|
+
self.deliverLoopImage(.{ .id = key_copy, .outcome = .cancelled }, image_fn, false);
|
|
3663
|
+
return;
|
|
3664
|
+
}
|
|
3085
3665
|
// No process: retire the slot and surface the exit now.
|
|
3086
3666
|
const exit_fn = slot.on_exit;
|
|
3087
3667
|
const exit: EffectExit = .{
|
|
@@ -3332,8 +3912,42 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3332
3912
|
_ = binding.minimize_fn(binding.context, window_label);
|
|
3333
3913
|
}
|
|
3334
3914
|
|
|
3335
|
-
///
|
|
3336
|
-
///
|
|
3915
|
+
/// Show a window by its declared label: unhide + activate — the
|
|
3916
|
+
/// counterpart to a `close_policy = .hide` hide, and the tray
|
|
3917
|
+
/// "Open" consequence of the menu-bar-app loop (macOS
|
|
3918
|
+
/// deminiaturizes and orders front, Windows SW_RESTORE/SW_SHOW +
|
|
3919
|
+
/// foreground, GTK presents). Also brings back a minimized or
|
|
3920
|
+
/// merely backgrounded window. Fire-and-forget, same contract
|
|
3921
|
+
/// as `closeWindow`: no event echoes beyond the window's own
|
|
3922
|
+
/// frame event, an unknown label is a no-op, and the fake
|
|
3923
|
+
/// executor only records the request in the mirror.
|
|
3924
|
+
pub fn showWindow(self: *Self, window_label: []const u8) void {
|
|
3925
|
+
self.window_action_state.show_count += 1;
|
|
3926
|
+
self.window_action_state.record(window_label);
|
|
3927
|
+
if (self.executor == .fake) return;
|
|
3928
|
+
const binding = self.window_actions orelse return;
|
|
3929
|
+
_ = binding.show_fn(binding.context, window_label);
|
|
3930
|
+
}
|
|
3931
|
+
|
|
3932
|
+
/// Quit the app for real — the graceful terminate, and the tray
|
|
3933
|
+
/// "Quit" consequence of the menu-bar-app loop. Rides the same
|
|
3934
|
+
/// shutdown event path as today's last-window close (the host
|
|
3935
|
+
/// emits `app_shutdown`, `app.stop` runs exactly once, a
|
|
3936
|
+
/// recording session seals its journal), so replay sees the
|
|
3937
|
+
/// identical journaled event. Like the window verbs it does not
|
|
3938
|
+
/// journal itself — the journaled `app_shutdown` it causes is
|
|
3939
|
+
/// the record — and under the fake executor only the mirror
|
|
3940
|
+
/// count moves.
|
|
3941
|
+
pub fn quitApp(self: *Self) void {
|
|
3942
|
+
self.window_action_state.quit_count += 1;
|
|
3943
|
+
if (self.executor == .fake) return;
|
|
3944
|
+
const binding = self.window_actions orelse return;
|
|
3945
|
+
_ = binding.quit_fn(binding.context);
|
|
3946
|
+
}
|
|
3947
|
+
|
|
3948
|
+
/// The window-action mirror, for tests: how many close/minimize/
|
|
3949
|
+
/// show/quit requests rode the channel and the last label
|
|
3950
|
+
/// requested.
|
|
3337
3951
|
pub fn windowActionState(self: *const Self) WindowActionState {
|
|
3338
3952
|
return self.window_action_state;
|
|
3339
3953
|
}
|
|
@@ -3481,15 +4095,70 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3481
4095
|
|
|
3482
4096
|
/// True when a drain would dispatch at least one Msg.
|
|
3483
4097
|
pub fn hasPending(self: *const Self) bool {
|
|
3484
|
-
return self.pending_exit_len > 0 or
|
|
4098
|
+
return self.pending_exit_len > 0 or
|
|
4099
|
+
self.pending_image_len > 0 or
|
|
4100
|
+
self.queue_count.load(.acquire) > 0;
|
|
4101
|
+
}
|
|
4102
|
+
|
|
4103
|
+
/// One drain pass's causal boundary: a snapshot of the
|
|
4104
|
+
/// completions that existed when the pass began. The session
|
|
4105
|
+
/// journal's ordering invariant — effect-result records precede
|
|
4106
|
+
/// the event record during whose dispatch they were drained, so
|
|
4107
|
+
/// replaying records in file order feeds each result before the
|
|
4108
|
+
/// event that consumes it — only holds if every result delivered
|
|
4109
|
+
/// during an event's dispatch answers a request that existed
|
|
4110
|
+
/// BEFORE that dispatch. A completion produced during the pass
|
|
4111
|
+
/// itself (an update handler starts a load fast enough to finish
|
|
4112
|
+
/// while the drain is still running) must therefore wait for the
|
|
4113
|
+
/// next wake: delivering it in the same pass would journal a
|
|
4114
|
+
/// result ahead of the event whose dispatch created its request,
|
|
4115
|
+
/// and replay's file-order feed would find no parked request to
|
|
4116
|
+
/// answer. Both producer paths (`enqueue` workers, the loop-side
|
|
4117
|
+
/// pending stages) nudge `wakeHost`, so a deferred completion
|
|
4118
|
+
/// always has its follow-up wake already scheduled.
|
|
4119
|
+
pub const DrainBoundary = struct {
|
|
4120
|
+
/// Loop-side pending entries staged before the pass: stamps
|
|
4121
|
+
/// below this deliver (both pending structures share the
|
|
4122
|
+
/// monotonic `pending_seq` stamp).
|
|
4123
|
+
pending_before: u64,
|
|
4124
|
+
/// Worker-queue entries enqueued before the pass. The queue
|
|
4125
|
+
/// is FIFO, so consuming exactly this many dequeues consumes
|
|
4126
|
+
/// exactly the pre-existing entries.
|
|
4127
|
+
queue_budget: usize,
|
|
4128
|
+
};
|
|
4129
|
+
|
|
4130
|
+
/// Snapshot the completion backlog at the start of one drain
|
|
4131
|
+
/// pass. Loop-thread only, like the drain itself.
|
|
4132
|
+
pub fn drainBoundary(self: *Self) DrainBoundary {
|
|
4133
|
+
return .{
|
|
4134
|
+
.pending_before = self.pending_seq,
|
|
4135
|
+
.queue_budget = self.queue_count.load(.acquire),
|
|
4136
|
+
};
|
|
3485
4137
|
}
|
|
3486
4138
|
|
|
3487
4139
|
/// Pop the next completion as a Msg. Loop-thread only. The
|
|
3488
4140
|
/// returned Msg's line payload stays valid until the next call.
|
|
4141
|
+
/// Unbounded: delivers completions produced while the caller
|
|
4142
|
+
/// drains, too. Runtime drain passes (`UiApp.drainEffects`, the
|
|
4143
|
+
/// ts-core host's `drain`) use `takeMsgWithin` instead so the
|
|
4144
|
+
/// journal's event boundaries stay causal (see `DrainBoundary`);
|
|
4145
|
+
/// this form serves callers that own their delivery timing —
|
|
4146
|
+
/// tests driving the channel directly.
|
|
3489
4147
|
pub fn takeMsg(self: *Self) ?Msg {
|
|
4148
|
+
var unbounded: DrainBoundary = .{
|
|
4149
|
+
.pending_before = std.math.maxInt(u64),
|
|
4150
|
+
.queue_budget = std.math.maxInt(usize),
|
|
4151
|
+
};
|
|
4152
|
+
return self.takeMsgWithin(&unbounded);
|
|
4153
|
+
}
|
|
4154
|
+
|
|
4155
|
+
/// `takeMsg` bounded to one drain pass: only completions inside
|
|
4156
|
+
/// `boundary` deliver; anything produced after the snapshot
|
|
4157
|
+
/// waits for the wake its producer already nudged.
|
|
4158
|
+
pub fn takeMsgWithin(self: *Self, boundary: *DrainBoundary) ?Msg {
|
|
3490
4159
|
self.reclaimSlots();
|
|
3491
4160
|
while (true) {
|
|
3492
|
-
if (self.takePendingMsg()) |pending| {
|
|
4161
|
+
if (self.takePendingMsg(boundary.pending_before)) |pending| {
|
|
3493
4162
|
switch (pending) {
|
|
3494
4163
|
.exit => |entry| {
|
|
3495
4164
|
const exit_fn = entry.exit_fn orelse continue;
|
|
@@ -3593,9 +4262,40 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3593
4262
|
});
|
|
3594
4263
|
return event_fn(event);
|
|
3595
4264
|
},
|
|
4265
|
+
.image => |entry| {
|
|
4266
|
+
// Journal BEFORE the handler gate: a staged
|
|
4267
|
+
// executor-truth terminal with no handler
|
|
4268
|
+
// (a fire-and-forget load's start failure)
|
|
4269
|
+
// still journals, because its record is
|
|
4270
|
+
// what retires the parked replay-side fake
|
|
4271
|
+
// (see `deliverLoopImage`). Only the Msg
|
|
4272
|
+
// depends on the handler.
|
|
4273
|
+
self.journalNote(.{
|
|
4274
|
+
.kind = .image,
|
|
4275
|
+
.key = entry.result.id,
|
|
4276
|
+
.status = entry.result.status,
|
|
4277
|
+
.image_outcome = entry.result.outcome,
|
|
4278
|
+
.image_width = entry.result.width,
|
|
4279
|
+
.image_height = entry.result.height,
|
|
4280
|
+
// `.image` journal encoding, the `.host`
|
|
4281
|
+
// records' convention: ONLY loop-side
|
|
4282
|
+
// validation refusals — regenerated by
|
|
4283
|
+
// the same checks under replay — mark
|
|
4284
|
+
// themselves with the exit reason. Every
|
|
4285
|
+
// other terminal (including worker-side
|
|
4286
|
+
// `.rejected`, which rides the slot
|
|
4287
|
+
// queue, not this ring) keeps `.exited`
|
|
4288
|
+
// and is fed from the journal.
|
|
4289
|
+
.exit_reason = if (entry.regenerates) .rejected else .exited,
|
|
4290
|
+
});
|
|
4291
|
+
const image_fn = entry.image_fn orelse continue;
|
|
4292
|
+
return image_fn(entry.result);
|
|
4293
|
+
},
|
|
3596
4294
|
}
|
|
3597
4295
|
}
|
|
4296
|
+
if (boundary.queue_budget == 0) return null;
|
|
3598
4297
|
if (!self.dequeueInto(&self.drain_scratch)) return null;
|
|
4298
|
+
boundary.queue_budget -= 1;
|
|
3599
4299
|
const entry = &self.drain_scratch;
|
|
3600
4300
|
const slot = &self.slots[entry.slot_index];
|
|
3601
4301
|
const cancelled = slot.cancelled_generation == entry.generation and entry.generation != 0;
|
|
@@ -3635,6 +4335,36 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3635
4335
|
return line_fn(line);
|
|
3636
4336
|
},
|
|
3637
4337
|
.exit => {
|
|
4338
|
+
// Dequeueing the occupant's exit IS its delivery
|
|
4339
|
+
// for key occupancy: clear the `.lines` marker
|
|
4340
|
+
// before any early-out (absent handler, cancel
|
|
4341
|
+
// rewrite) so the key frees exactly when the
|
|
4342
|
+
// terminal reaches — or would have reached —
|
|
4343
|
+
// update, and a handler that respawns its own
|
|
4344
|
+
// key parks as a fresh effect. Retire the slot
|
|
4345
|
+
// in the same stroke (see the `.image` arm's
|
|
4346
|
+
// race note): the real worker stores `.draining`
|
|
4347
|
+
// only AFTER posting this exit, so a drain can
|
|
4348
|
+
// consume it while the slot still reads
|
|
4349
|
+
// `.running` — and the handler's respawn would
|
|
4350
|
+
// reject as a duplicate active key. The
|
|
4351
|
+
// happens-before chain is complete here: the
|
|
4352
|
+
// worker's mark-then-post publishes the marker
|
|
4353
|
+
// (and every other slot write) before the entry
|
|
4354
|
+
// is consumable — the enqueue releases the queue
|
|
4355
|
+
// mutex this dequeue acquired — so with the
|
|
4356
|
+
// generations matching, clearing the marker and
|
|
4357
|
+
// storing `.draining` before the handler leaves
|
|
4358
|
+
// the key free by the time any update code runs.
|
|
4359
|
+
// The worker's own store keeps its role (it must
|
|
4360
|
+
// stay after the post — a `.draining` slot is
|
|
4361
|
+
// joinable, and the post can park in a
|
|
4362
|
+
// full-queue retry only the loop can relieve);
|
|
4363
|
+
// re-storing `.draining` is idempotent.
|
|
4364
|
+
if (entry.generation == slot.generation) {
|
|
4365
|
+
slot.exit_undelivered = false;
|
|
4366
|
+
slot.state.store(.draining, .release);
|
|
4367
|
+
}
|
|
3638
4368
|
// Collect exits own a heap stdout buffer: take it
|
|
3639
4369
|
// before any early-out so the slot retires even
|
|
3640
4370
|
// when the handler is absent. A mismatched
|
|
@@ -3688,6 +4418,13 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3688
4418
|
// generation means the occupant was already
|
|
3689
4419
|
// retired with its own terminal Msg.
|
|
3690
4420
|
if (entry.generation != slot.generation) continue;
|
|
4421
|
+
// Retire the slot BEFORE the terminal reaches
|
|
4422
|
+
// any handler (the `.image` arm's race note):
|
|
4423
|
+
// the real worker stores `.draining` only after
|
|
4424
|
+
// posting this entry, so a same-key fetch from
|
|
4425
|
+
// the response handler must not find the slot
|
|
4426
|
+
// still `.running`. Idempotent re-store.
|
|
4427
|
+
slot.state.store(.draining, .release);
|
|
3691
4428
|
// Take body ownership so the slot can be reused
|
|
3692
4429
|
// while `update` still reads the slice; the
|
|
3693
4430
|
// buffer is freed when the next response drains.
|
|
@@ -3727,6 +4464,13 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3727
4464
|
// `.response`: a mismatched generation means the
|
|
3728
4465
|
// occupant was already retired.
|
|
3729
4466
|
if (entry.generation != slot.generation) continue;
|
|
4467
|
+
// Retire the slot BEFORE the terminal reaches
|
|
4468
|
+
// any handler (the `.image` arm's race note):
|
|
4469
|
+
// the real worker stores `.draining` only after
|
|
4470
|
+
// posting this entry, so a same-key file effect
|
|
4471
|
+
// from the result handler must not find the
|
|
4472
|
+
// slot still `.running`. Idempotent re-store.
|
|
4473
|
+
slot.state.store(.draining, .release);
|
|
3730
4474
|
// Take buffer ownership so the slot can be
|
|
3731
4475
|
// reused while `update` still reads the bytes.
|
|
3732
4476
|
if (self.drain_fetch_body) |old| self.allocator.free(old);
|
|
@@ -3761,7 +4505,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3761
4505
|
.clipboard => {
|
|
3762
4506
|
// One terminal per clipboard occupancy, mirroring
|
|
3763
4507
|
// `.file`: a mismatched generation means the
|
|
3764
|
-
// occupant was already retired.
|
|
4508
|
+
// occupant was already retired. No consumer-side
|
|
4509
|
+
// `.draining` store is needed here (unlike the
|
|
4510
|
+
// worker-fed arms): clipboard terminals are
|
|
4511
|
+
// staged on the loop thread with the store
|
|
4512
|
+
// sequenced before the enqueue, so this drain
|
|
4513
|
+
// can never observe the entry ahead of it.
|
|
3765
4514
|
if (entry.generation != slot.generation) continue;
|
|
3766
4515
|
// Take buffer ownership so the slot can be
|
|
3767
4516
|
// reused while `update` still reads the text.
|
|
@@ -3769,7 +4518,6 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3769
4518
|
self.drain_fetch_body = slot.fetch_buffer;
|
|
3770
4519
|
slot.fetch_buffer = null;
|
|
3771
4520
|
const payload_len = slot.payload_len;
|
|
3772
|
-
const clipboard_fn = entry.clipboard_fn orelse continue;
|
|
3773
4521
|
const text: []const u8 = if (self.drain_fetch_body) |buffer|
|
|
3774
4522
|
buffer[payload_len .. payload_len + entry.line_len]
|
|
3775
4523
|
else
|
|
@@ -3784,6 +4532,13 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3784
4532
|
.text = text,
|
|
3785
4533
|
.dropped_before = entry.dropped_before,
|
|
3786
4534
|
};
|
|
4535
|
+
// Journal BEFORE the handler gate: a clipboard
|
|
4536
|
+
// terminal is executor truth (the pasteboard
|
|
4537
|
+
// ran), so a fire-and-forget write's `.ok` or
|
|
4538
|
+
// `.failed` must still journal — under session
|
|
4539
|
+
// replay the request is a parked fake that only
|
|
4540
|
+
// this record's feed retires. Only the Msg
|
|
4541
|
+
// depends on the handler.
|
|
3787
4542
|
self.journalNote(.{
|
|
3788
4543
|
.kind = .clipboard,
|
|
3789
4544
|
.key = result.key,
|
|
@@ -3792,6 +4547,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3792
4547
|
.clipboard_op = result.op,
|
|
3793
4548
|
.clipboard_outcome = result.outcome,
|
|
3794
4549
|
});
|
|
4550
|
+
const clipboard_fn = entry.clipboard_fn orelse continue;
|
|
3795
4551
|
return clipboard_fn(result);
|
|
3796
4552
|
},
|
|
3797
4553
|
.host => {
|
|
@@ -3799,7 +4555,14 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3799
4555
|
// `.response`: a mismatched generation means the
|
|
3800
4556
|
// occupant was already retired (replaced or
|
|
3801
4557
|
// cancelled — its result drops silently, per the
|
|
3802
|
-
// request contract).
|
|
4558
|
+
// request contract). No consumer-side
|
|
4559
|
+
// `.draining` store is needed here (unlike the
|
|
4560
|
+
// worker-fed arms): host answers are fed on the
|
|
4561
|
+
// loop thread with the store sequenced before
|
|
4562
|
+
// the enqueue — and a same-key host request
|
|
4563
|
+
// REPLACES an in-flight one rather than
|
|
4564
|
+
// rejecting, so no handler retry hinges on the
|
|
4565
|
+
// state either way.
|
|
3803
4566
|
if (entry.generation != slot.generation) continue;
|
|
3804
4567
|
// Take buffer ownership so the slot can be
|
|
3805
4568
|
// reused while `update` still reads the bytes.
|
|
@@ -3808,9 +4571,11 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3808
4571
|
slot.fetch_buffer = null;
|
|
3809
4572
|
const payload_len = slot.payload_len;
|
|
3810
4573
|
// A cancel that raced the feed (the entry was
|
|
3811
|
-
// already queued) still drops silently
|
|
4574
|
+
// already queued) still drops silently — on
|
|
4575
|
+
// both sides: live never journals it, and the
|
|
4576
|
+
// replayed cancel drops the parked fake, so
|
|
4577
|
+
// neither side has a record to feed.
|
|
3812
4578
|
if (cancelled) continue;
|
|
3813
|
-
const host_fn = entry.host_fn orelse continue;
|
|
3814
4579
|
const bytes: []const u8 = if (self.drain_fetch_body) |buffer|
|
|
3815
4580
|
buffer[payload_len .. payload_len + entry.line_len]
|
|
3816
4581
|
else
|
|
@@ -3820,6 +4585,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3820
4585
|
.ok = entry.host_ok,
|
|
3821
4586
|
.bytes = bytes,
|
|
3822
4587
|
};
|
|
4588
|
+
// Journal BEFORE the handler gate: a host
|
|
4589
|
+
// answer is executor truth, so it must journal
|
|
4590
|
+
// even when no `on_result` route exists — under
|
|
4591
|
+
// session replay the request is a parked fake
|
|
4592
|
+
// that only this record's feed retires. Only
|
|
4593
|
+
// the Msg depends on the handler.
|
|
3823
4594
|
self.journalNote(.{
|
|
3824
4595
|
.kind = .host,
|
|
3825
4596
|
.key = result.key,
|
|
@@ -3827,12 +4598,135 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3827
4598
|
// `.host` journal encoding: route in `code`.
|
|
3828
4599
|
.code = @intFromBool(!result.ok),
|
|
3829
4600
|
});
|
|
4601
|
+
const host_fn = entry.host_fn orelse continue;
|
|
3830
4602
|
return host_fn(result);
|
|
3831
4603
|
},
|
|
4604
|
+
.image => {
|
|
4605
|
+
// One terminal per image occupancy, mirroring
|
|
4606
|
+
// `.response`: a mismatched generation means the
|
|
4607
|
+
// occupant was already retired.
|
|
4608
|
+
if (entry.generation != slot.generation) continue;
|
|
4609
|
+
// Take buffer ownership so the slot can be
|
|
4610
|
+
// reused while `update` (and the journal sink)
|
|
4611
|
+
// still read the bytes.
|
|
4612
|
+
if (self.drain_fetch_body) |old| self.allocator.free(old);
|
|
4613
|
+
self.drain_fetch_body = slot.fetch_buffer;
|
|
4614
|
+
slot.fetch_buffer = null;
|
|
4615
|
+
// Retire the slot BEFORE the terminal reaches any
|
|
4616
|
+
// handler. The real worker stores `.draining`
|
|
4617
|
+
// only AFTER posting this entry, so a drain can
|
|
4618
|
+
// race ahead of that store and dispatch the
|
|
4619
|
+
// result while the slot still reads `.running` —
|
|
4620
|
+
// and an update that reacts to the terminal by
|
|
4621
|
+
// loading the same id again (reload-after-
|
|
4622
|
+
// terminal is allowed) would reject as a
|
|
4623
|
+
// duplicate active key. Storing here makes the
|
|
4624
|
+
// slot reclaimable by the time any update code
|
|
4625
|
+
// runs; the worker's own store stays as the
|
|
4626
|
+
// pre-drain transition (cancel targeting and
|
|
4627
|
+
// early thread reclaim still key off it), and
|
|
4628
|
+
// re-storing `.draining` is idempotent.
|
|
4629
|
+
slot.state.store(.draining, .release);
|
|
4630
|
+
const image_fn = entry.image_fn;
|
|
4631
|
+
const bytes: []const u8 = if (self.drain_fetch_body) |buffer|
|
|
4632
|
+
buffer[0..entry.line_len]
|
|
4633
|
+
else
|
|
4634
|
+
"";
|
|
4635
|
+
var result: EffectImageResult = .{
|
|
4636
|
+
.id = entry.key,
|
|
4637
|
+
.outcome = entry.image_outcome,
|
|
4638
|
+
.status = entry.status,
|
|
4639
|
+
};
|
|
4640
|
+
// The journal carries the source bytes whenever
|
|
4641
|
+
// the cascade delivered them — even when the
|
|
4642
|
+
// decode below fails, the bytes ARE the effect
|
|
4643
|
+
// result the boundary produced. RECORD-TIME
|
|
4644
|
+
// INVARIANT: a journaled `.loaded` always
|
|
4645
|
+
// carries non-empty bytes, because `.loaded`
|
|
4646
|
+
// reaches the journal only after
|
|
4647
|
+
// `register_bytes_fn` decoded and registered
|
|
4648
|
+
// these exact bytes (a failure rewrites the
|
|
4649
|
+
// outcome below, and empty bytes cannot decode).
|
|
4650
|
+
// Session replay refuses `.loaded` records with
|
|
4651
|
+
// a zero-length blob on this invariant
|
|
4652
|
+
// (`error.ReplayDamagedRecord`).
|
|
4653
|
+
var journal_bytes: []const u8 = "";
|
|
4654
|
+
if (entry.image_fed) {
|
|
4655
|
+
// Session replay: the recorded terminal is
|
|
4656
|
+
// the Msg, verbatim — checked BEFORE the
|
|
4657
|
+
// cancelled rewrite. A live cancel that won
|
|
4658
|
+
// journaled `.cancelled` and feeds back as
|
|
4659
|
+
// such; a live cancel that LOST (a staged
|
|
4660
|
+
// start-failure rejection has no slot to
|
|
4661
|
+
// mark, so live delivered `.rejected`) must
|
|
4662
|
+
// not be resurrected by the replayed
|
|
4663
|
+
// cancel's mark on the parked fake slot,
|
|
4664
|
+
// whose timing differs from the live seam.
|
|
4665
|
+
// Re-registration is
|
|
4666
|
+
// best-effort presentation — a replay host
|
|
4667
|
+
// whose codec cannot decode the recorded
|
|
4668
|
+
// bytes (the null platform decodes only the
|
|
4669
|
+
// strict PNG subset) still replays the
|
|
4670
|
+
// identical Msg stream; views just render
|
|
4671
|
+
// without the pixels, and pixel checkpoints
|
|
4672
|
+
// report the honest difference.
|
|
4673
|
+
journal_bytes = bytes;
|
|
4674
|
+
result.width = std.math.cast(usize, entry.image_fed_width) orelse 0;
|
|
4675
|
+
result.height = std.math.cast(usize, entry.image_fed_height) orelse 0;
|
|
4676
|
+
if (result.outcome == .loaded and bytes.len > 0) {
|
|
4677
|
+
self.registerDrainedImage(entry.key, bytes);
|
|
4678
|
+
}
|
|
4679
|
+
} else if (cancelled) {
|
|
4680
|
+
result = .{ .id = entry.key, .outcome = .cancelled };
|
|
4681
|
+
} else if (result.outcome == .loaded) {
|
|
4682
|
+
journal_bytes = bytes;
|
|
4683
|
+
if (self.images) |binding| {
|
|
4684
|
+
if (binding.register_bytes_fn(binding.context, entry.key, bytes)) |info| {
|
|
4685
|
+
result.width = info.width;
|
|
4686
|
+
result.height = info.height;
|
|
4687
|
+
} else |err| {
|
|
4688
|
+
result.outcome = classifyImageRegisterError(err);
|
|
4689
|
+
}
|
|
4690
|
+
} else {
|
|
4691
|
+
// No registry bound to this channel:
|
|
4692
|
+
// nothing can hold the pixels — the
|
|
4693
|
+
// same class as a host without a codec.
|
|
4694
|
+
result.outcome = .unsupported;
|
|
4695
|
+
}
|
|
4696
|
+
}
|
|
4697
|
+
self.journalNote(.{
|
|
4698
|
+
.kind = .image,
|
|
4699
|
+
.key = result.id,
|
|
4700
|
+
.payload = journal_bytes,
|
|
4701
|
+
.status = result.status,
|
|
4702
|
+
.image_outcome = result.outcome,
|
|
4703
|
+
.image_width = result.width,
|
|
4704
|
+
.image_height = result.height,
|
|
4705
|
+
});
|
|
4706
|
+
const deliver_fn = image_fn orelse continue;
|
|
4707
|
+
return deliver_fn(result);
|
|
4708
|
+
},
|
|
3832
4709
|
}
|
|
3833
4710
|
}
|
|
3834
4711
|
}
|
|
3835
4712
|
|
|
4713
|
+
/// Best-effort decode + register of a replayed image record's
|
|
4714
|
+
/// bytes. Failure is loud (once per process would hide repeats;
|
|
4715
|
+
/// once per failure is a bounded replay-time diagnostic) but
|
|
4716
|
+
/// never steers the Msg stream — the journaled terminal does.
|
|
4717
|
+
fn registerDrainedImage(self: *Self, id: u64, bytes: []const u8) void {
|
|
4718
|
+
const binding = self.images orelse return;
|
|
4719
|
+
_ = binding.register_bytes_fn(binding.context, id, bytes) catch |err| {
|
|
4720
|
+
if (comptime builtin.os.tag != .freestanding) {
|
|
4721
|
+
std.debug.print(
|
|
4722
|
+
"session replay: re-registering image id {d} from the journaled bytes failed ({s}); the Msg stream replays the recorded result, views render without the pixels\n",
|
|
4723
|
+
.{ id, @errorName(err) },
|
|
4724
|
+
);
|
|
4725
|
+
}
|
|
4726
|
+
return;
|
|
4727
|
+
};
|
|
4728
|
+
}
|
|
4729
|
+
|
|
3836
4730
|
// --------------------------------------------------- fake executor
|
|
3837
4731
|
|
|
3838
4732
|
/// Number of recorded (still-active) fake spawn requests.
|
|
@@ -3972,8 +4866,14 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3972
4866
|
return;
|
|
3973
4867
|
}
|
|
3974
4868
|
const delivered = self.enqueue(&entry);
|
|
3975
|
-
|
|
3976
|
-
|
|
4869
|
+
if (delivered) {
|
|
4870
|
+
// Park until the drain takes the exit: the key stays
|
|
4871
|
+
// occupied through the posted-but-undelivered window,
|
|
4872
|
+
// exactly like the real worker's commit.
|
|
4873
|
+
slot.exit_undelivered = true;
|
|
4874
|
+
slot.state.store(.draining, .release);
|
|
4875
|
+
} else {
|
|
4876
|
+
slot.state.store(.idle, .release);
|
|
3977
4877
|
self.deliverLoopExit(.{
|
|
3978
4878
|
.key = entry.key,
|
|
3979
4879
|
.code = code,
|
|
@@ -4205,6 +5105,137 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4205
5105
|
self.wakeHost();
|
|
4206
5106
|
}
|
|
4207
5107
|
|
|
5108
|
+
/// Number of recorded (still-active) fake image-load requests.
|
|
5109
|
+
pub fn pendingImageLoadCount(self: *Self) usize {
|
|
5110
|
+
var count: usize = 0;
|
|
5111
|
+
for (&self.slots) |*slot| {
|
|
5112
|
+
if (slot.fake and slot.kind == .image and slot.state.load(.acquire) == .running) count += 1;
|
|
5113
|
+
}
|
|
5114
|
+
return count;
|
|
5115
|
+
}
|
|
5116
|
+
|
|
5117
|
+
/// The `index`-th recorded fake image-load request (slot order).
|
|
5118
|
+
pub fn pendingImageLoadAt(self: *Self, index: usize) ?ImageLoadRequest {
|
|
5119
|
+
var seen: usize = 0;
|
|
5120
|
+
for (&self.slots) |*slot| {
|
|
5121
|
+
if (!(slot.fake and slot.kind == .image and slot.state.load(.acquire) == .running)) continue;
|
|
5122
|
+
if (seen == index) {
|
|
5123
|
+
return .{
|
|
5124
|
+
.id = slot.key,
|
|
5125
|
+
.path = slot.imagePath(),
|
|
5126
|
+
.url = slot.fetchUrl(),
|
|
5127
|
+
.cache_path = slot.imageCache(),
|
|
5128
|
+
.expected_bytes = slot.image_expected_bytes,
|
|
5129
|
+
};
|
|
5130
|
+
}
|
|
5131
|
+
seen += 1;
|
|
5132
|
+
}
|
|
5133
|
+
return null;
|
|
5134
|
+
}
|
|
5135
|
+
|
|
5136
|
+
/// Feed synthetic ENCODED source bytes to the fake image load
|
|
5137
|
+
/// with `id`, retiring its slot — the test mirror of a real
|
|
5138
|
+
/// cascade that produced bytes: the drain decodes and registers
|
|
5139
|
+
/// them through the bound registry exactly like the real
|
|
5140
|
+
/// executor, so tests exercise the full decode→register→Msg
|
|
5141
|
+
/// path. Bytes over `max_effect_image_bytes` deliver `.too_large`
|
|
5142
|
+
/// without decoding, mirroring the real bound.
|
|
5143
|
+
pub fn feedImageBytes(self: *Self, id: u64, bytes: []const u8) error{ EffectNotFound, EffectQueueFull }!void {
|
|
5144
|
+
const slot_index = self.findActiveFakeSlot(id, .image) orelse return error.EffectNotFound;
|
|
5145
|
+
const slot = &self.slots[slot_index];
|
|
5146
|
+
const buffer = slot.fetch_buffer orelse return error.EffectNotFound;
|
|
5147
|
+
var staged_len: usize = 0;
|
|
5148
|
+
var outcome: EffectImageOutcome = .loaded;
|
|
5149
|
+
if (bytes.len > max_effect_image_bytes) {
|
|
5150
|
+
outcome = .too_large;
|
|
5151
|
+
} else {
|
|
5152
|
+
@memcpy(buffer[0..bytes.len], bytes);
|
|
5153
|
+
staged_len = bytes.len;
|
|
5154
|
+
}
|
|
5155
|
+
slot.body_len = staged_len;
|
|
5156
|
+
try self.finishFedImage(slot, .{
|
|
5157
|
+
.kind = .image,
|
|
5158
|
+
.slot_index = @intCast(slot_index),
|
|
5159
|
+
.generation = slot.generation,
|
|
5160
|
+
.key = slot.key,
|
|
5161
|
+
.line_len = @intCast(staged_len),
|
|
5162
|
+
.status = slot.fetch_status,
|
|
5163
|
+
.image_outcome = outcome,
|
|
5164
|
+
.image_fn = slot.on_image,
|
|
5165
|
+
});
|
|
5166
|
+
}
|
|
5167
|
+
|
|
5168
|
+
/// Feed a RECORDED image terminal — the session-replay feed:
|
|
5169
|
+
/// the journaled outcome, dimensions, and status deliver
|
|
5170
|
+
/// verbatim (the Msg stream must replay byte-identical even on
|
|
5171
|
+
/// a host whose codec differs), and a `.loaded` record's bytes
|
|
5172
|
+
/// re-register best-effort so views repaint the recorded
|
|
5173
|
+
/// pixels. Also the failure-class feed for tests. Under replay
|
|
5174
|
+
/// a full completion queue reports `error.EffectQueueFull`
|
|
5175
|
+
/// with the request still parked — feed again after a drain
|
|
5176
|
+
/// (see `finishFedImage`).
|
|
5177
|
+
pub fn feedImageResult(self: *Self, id: u64, outcome: EffectImageOutcome, width: u64, height: u64, status: u16, bytes: []const u8) error{ EffectNotFound, EffectQueueFull }!void {
|
|
5178
|
+
const slot_index = self.findActiveFakeSlot(id, .image) orelse return error.EffectNotFound;
|
|
5179
|
+
const slot = &self.slots[slot_index];
|
|
5180
|
+
const buffer = slot.fetch_buffer orelse return error.EffectNotFound;
|
|
5181
|
+
const staged_len = @min(bytes.len, max_effect_image_bytes);
|
|
5182
|
+
@memcpy(buffer[0..staged_len], bytes[0..staged_len]);
|
|
5183
|
+
slot.body_len = staged_len;
|
|
5184
|
+
try self.finishFedImage(slot, .{
|
|
5185
|
+
.kind = .image,
|
|
5186
|
+
.slot_index = @intCast(slot_index),
|
|
5187
|
+
.generation = slot.generation,
|
|
5188
|
+
.key = slot.key,
|
|
5189
|
+
.line_len = @intCast(staged_len),
|
|
5190
|
+
.status = status,
|
|
5191
|
+
.image_outcome = outcome,
|
|
5192
|
+
.image_fed = true,
|
|
5193
|
+
.image_fed_width = width,
|
|
5194
|
+
.image_fed_height = height,
|
|
5195
|
+
.image_fn = slot.on_image,
|
|
5196
|
+
});
|
|
5197
|
+
}
|
|
5198
|
+
|
|
5199
|
+
/// Queue a fed image terminal. Under session replay the queue
|
|
5200
|
+
/// is the ONLY path: a full queue back-pressures with
|
|
5201
|
+
/// `error.EffectQueueFull` — the slot returns to `.running`
|
|
5202
|
+
/// exactly as before the feed, bytes still in its buffer — so
|
|
5203
|
+
/// the replay pump can drain the loop and feed again. That is
|
|
5204
|
+
/// the loop-thread mirror of the real worker's `postImage`
|
|
5205
|
+
/// retry: everything goes THROUGH the queue, so the recorded
|
|
5206
|
+
/// bytes and the recorded delivery order both survive a
|
|
5207
|
+
/// recording whose drain pass carried more results than the
|
|
5208
|
+
/// queue holds.
|
|
5209
|
+
///
|
|
5210
|
+
/// Outside replay (test feeds) a full queue keeps the
|
|
5211
|
+
/// pending-ring fallback: the terminal lands byte-free, and a
|
|
5212
|
+
/// DERIVED `.loaded` (a bytes feed) is rewritten `.rejected`
|
|
5213
|
+
/// there — nothing decoded, nothing registered; a success it
|
|
5214
|
+
/// cannot back would lie. A RECORDED terminal stands verbatim
|
|
5215
|
+
/// (only the best-effort re-registration is lost).
|
|
5216
|
+
fn finishFedImage(self: *Self, slot: *Slot, entry_value: Entry) error{EffectQueueFull}!void {
|
|
5217
|
+
var entry = entry_value;
|
|
5218
|
+
slot.state.store(.draining, .release);
|
|
5219
|
+
if (!self.enqueue(&entry)) {
|
|
5220
|
+
if (self.replay) {
|
|
5221
|
+
slot.state.store(.running, .release);
|
|
5222
|
+
return error.EffectQueueFull;
|
|
5223
|
+
}
|
|
5224
|
+
const image_fn = slot.on_image;
|
|
5225
|
+
var outcome = entry.image_outcome;
|
|
5226
|
+
if (outcome == .loaded and !entry.image_fed) outcome = .rejected;
|
|
5227
|
+
self.releaseFetchSlot(slot);
|
|
5228
|
+
self.deliverLoopImage(.{
|
|
5229
|
+
.id = entry.key,
|
|
5230
|
+
.outcome = outcome,
|
|
5231
|
+
.width = std.math.cast(usize, entry.image_fed_width) orelse 0,
|
|
5232
|
+
.height = std.math.cast(usize, entry.image_fed_height) orelse 0,
|
|
5233
|
+
.status = entry.status,
|
|
5234
|
+
}, image_fn, false);
|
|
5235
|
+
}
|
|
5236
|
+
self.wakeHost();
|
|
5237
|
+
}
|
|
5238
|
+
|
|
4208
5239
|
/// Number of parked (still-active) fake host requests.
|
|
4209
5240
|
pub fn pendingHostCount(self: *Self) usize {
|
|
4210
5241
|
var count: usize = 0;
|
|
@@ -4390,6 +5421,49 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4390
5421
|
self.deliverPending(.{ .clipboard = .{ .result = result, .clipboard_fn = clipboard_fn } });
|
|
4391
5422
|
}
|
|
4392
5423
|
|
|
5424
|
+
/// `regenerates` distinguishes the rejection's provenance for
|
|
5425
|
+
/// the session journal: true for pre-executor validation
|
|
5426
|
+
/// refusals (deterministic — the replayed `loadImage` refuses
|
|
5427
|
+
/// again), false when the executor could not run an otherwise
|
|
5428
|
+
/// valid request (io/thread/buffer failures the replay's fake
|
|
5429
|
+
/// executor never reproduces — those journal as executor truth
|
|
5430
|
+
/// and feed at replay).
|
|
5431
|
+
fn rejectImage(self: *Self, id: u64, image_fn: ?ImageMsgFn, regenerates: bool) void {
|
|
5432
|
+
self.deliverLoopImage(.{
|
|
5433
|
+
.id = id,
|
|
5434
|
+
.outcome = .rejected,
|
|
5435
|
+
}, image_fn, regenerates);
|
|
5436
|
+
}
|
|
5437
|
+
|
|
5438
|
+
/// Queue a terminal image result produced on the loop thread
|
|
5439
|
+
/// (rejections, fake cancels, feed fallbacks) for the next
|
|
5440
|
+
/// drain. No bytes ride here — nothing decodes, nothing
|
|
5441
|
+
/// registers. Staged outside the lossy pending ring: image
|
|
5442
|
+
/// terminals must never evict or be evicted (see
|
|
5443
|
+
/// `PendingImage`).
|
|
5444
|
+
///
|
|
5445
|
+
/// Only the Msg is gated on the handler. An executor-truth
|
|
5446
|
+
/// terminal (`regenerates = false`) stages even without one:
|
|
5447
|
+
/// it occupies its id through the staged window
|
|
5448
|
+
/// (`stagedImageOccupiesKey`) and journals at drain, because
|
|
5449
|
+
/// under session replay the request it answers is a parked
|
|
5450
|
+
/// fake that ONLY the journaled record's feed retires —
|
|
5451
|
+
/// skipping the stage for a fire-and-forget load would leave
|
|
5452
|
+
/// the id and a slot occupied replay-side forever. A
|
|
5453
|
+
/// handlerless REGENERATING refusal stages nothing: replay
|
|
5454
|
+
/// re-runs the same loop-side validation at the same dispatch
|
|
5455
|
+
/// and refuses identically, so both sides stay symmetric with
|
|
5456
|
+
/// no record, no occupancy, and no Msg.
|
|
5457
|
+
fn deliverLoopImage(self: *Self, result: EffectImageResult, image_fn: ?ImageMsgFn, regenerates: bool) void {
|
|
5458
|
+
if (image_fn == null and regenerates) return;
|
|
5459
|
+
self.stagePendingImage(.{
|
|
5460
|
+
.seq = self.nextPendingSeq(),
|
|
5461
|
+
.result = result,
|
|
5462
|
+
.image_fn = image_fn,
|
|
5463
|
+
.regenerates = regenerates,
|
|
5464
|
+
});
|
|
5465
|
+
}
|
|
5466
|
+
|
|
4393
5467
|
fn rejectTimer(self: *Self, options: StartTimerOptions) void {
|
|
4394
5468
|
self.deliverLoopTimer(.{
|
|
4395
5469
|
.key = options.key,
|
|
@@ -4522,6 +5596,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4522
5596
|
self.allocator.free(buffer);
|
|
4523
5597
|
slot.line_buffer = null;
|
|
4524
5598
|
}
|
|
5599
|
+
slot.exit_undelivered = false;
|
|
4525
5600
|
slot.state.store(.idle, .release);
|
|
4526
5601
|
}
|
|
4527
5602
|
|
|
@@ -4550,8 +5625,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4550
5625
|
|
|
4551
5626
|
/// Push onto the loop-side pending ring. When the ring is full
|
|
4552
5627
|
/// the oldest entry is replaced and the replacement carries the
|
|
4553
|
-
/// loss in its drop counter — overflow stays visible.
|
|
5628
|
+
/// loss in its drop counter — overflow stays visible. Image
|
|
5629
|
+
/// terminals never come through here: they carry no drop
|
|
5630
|
+
/// counter to keep an eviction visible, so they stage in the
|
|
5631
|
+
/// non-lossy `pending_images` instead (`stagePendingImage`).
|
|
4554
5632
|
fn deliverPending(self: *Self, pending: PendingMsg) void {
|
|
5633
|
+
const seq = self.nextPendingSeq();
|
|
4555
5634
|
if (self.pending_exit_len == max_effect_pending_exits) {
|
|
4556
5635
|
const oldest = &self.pending_exits[self.pending_exit_head];
|
|
4557
5636
|
self.pending_exit_head = (self.pending_exit_head + 1) % max_effect_pending_exits;
|
|
@@ -4560,17 +5639,102 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4560
5639
|
replacement.addDropped(oldest.droppedCount() +| 1);
|
|
4561
5640
|
const tail = (self.pending_exit_head + self.pending_exit_len) % max_effect_pending_exits;
|
|
4562
5641
|
self.pending_exits[tail] = replacement;
|
|
5642
|
+
self.pending_exit_seqs[tail] = seq;
|
|
4563
5643
|
self.pending_exit_len += 1;
|
|
4564
5644
|
} else {
|
|
4565
5645
|
const tail = (self.pending_exit_head + self.pending_exit_len) % max_effect_pending_exits;
|
|
4566
5646
|
self.pending_exits[tail] = pending;
|
|
5647
|
+
self.pending_exit_seqs[tail] = seq;
|
|
4567
5648
|
self.pending_exit_len += 1;
|
|
4568
5649
|
}
|
|
4569
5650
|
self.wakeHost();
|
|
4570
5651
|
}
|
|
4571
5652
|
|
|
4572
|
-
fn
|
|
4573
|
-
|
|
5653
|
+
fn nextPendingSeq(self: *Self) u64 {
|
|
5654
|
+
const seq = self.pending_seq;
|
|
5655
|
+
self.pending_seq += 1;
|
|
5656
|
+
return seq;
|
|
5657
|
+
}
|
|
5658
|
+
|
|
5659
|
+
/// The image stage's current backing storage: the inline buffer
|
|
5660
|
+
/// until a burst outgrows it, the heap ring after.
|
|
5661
|
+
fn pendingImageStorage(self: *Self) []PendingImage {
|
|
5662
|
+
if (self.pending_image_spill.len > 0) return self.pending_image_spill;
|
|
5663
|
+
return &self.pending_images;
|
|
5664
|
+
}
|
|
5665
|
+
|
|
5666
|
+
/// Stage one loop-side image terminal for the next drain —
|
|
5667
|
+
/// never dropping one (the `PendingImage` contract). Growth is
|
|
5668
|
+
/// geometric and earned only by bursts that outgrow the inline
|
|
5669
|
+
/// buffer; each staged entry answers exactly one `loadImage`
|
|
5670
|
+
/// call, so the stage can never grow past the caller's own
|
|
5671
|
+
/// call count between drains. An allocation failure refuses
|
|
5672
|
+
/// LOUDLY: with no counter to fold a loss into and no error
|
|
5673
|
+
/// channel back to the void-returning `loadImage`, dropping
|
|
5674
|
+
/// the entry would strand its issuer forever — a crash names
|
|
5675
|
+
/// the problem, silence never would.
|
|
5676
|
+
fn stagePendingImage(self: *Self, entry: PendingImage) void {
|
|
5677
|
+
const storage = self.pendingImageStorage();
|
|
5678
|
+
if (self.pending_image_len == storage.len) {
|
|
5679
|
+
const grown = self.allocator.alloc(PendingImage, storage.len * 2) catch
|
|
5680
|
+
@panic("effects: out of memory staging an image terminal - each staged entry is one loadImage call's only terminal and must never be dropped");
|
|
5681
|
+
for (grown[0..self.pending_image_len], 0..) |*slot, index| {
|
|
5682
|
+
slot.* = storage[(self.pending_image_head + index) % storage.len];
|
|
5683
|
+
}
|
|
5684
|
+
if (self.pending_image_spill.len > 0) self.allocator.free(self.pending_image_spill);
|
|
5685
|
+
self.pending_image_spill = grown;
|
|
5686
|
+
self.pending_image_head = 0;
|
|
5687
|
+
}
|
|
5688
|
+
const active = self.pendingImageStorage();
|
|
5689
|
+
active[(self.pending_image_head + self.pending_image_len) % active.len] = entry;
|
|
5690
|
+
self.pending_image_len += 1;
|
|
5691
|
+
self.wakeHost();
|
|
5692
|
+
}
|
|
5693
|
+
|
|
5694
|
+
/// Pop the image stage's head. Draining empty releases the
|
|
5695
|
+
/// spill: the burst that earned it is over, and the inline
|
|
5696
|
+
/// buffer covers the everyday case again.
|
|
5697
|
+
fn takePendingImage(self: *Self) PendingImage {
|
|
5698
|
+
const storage = self.pendingImageStorage();
|
|
5699
|
+
const entry = storage[self.pending_image_head];
|
|
5700
|
+
self.pending_image_head = (self.pending_image_head + 1) % storage.len;
|
|
5701
|
+
self.pending_image_len -= 1;
|
|
5702
|
+
if (self.pending_image_len == 0) {
|
|
5703
|
+
self.pending_image_head = 0;
|
|
5704
|
+
if (self.pending_image_spill.len > 0) {
|
|
5705
|
+
self.allocator.free(self.pending_image_spill);
|
|
5706
|
+
self.pending_image_spill = &.{};
|
|
5707
|
+
}
|
|
5708
|
+
}
|
|
5709
|
+
return entry;
|
|
5710
|
+
}
|
|
5711
|
+
|
|
5712
|
+
/// Take the next loop-side pending terminal in enqueue order,
|
|
5713
|
+
/// merging the ring and the image stage by their shared stamp
|
|
5714
|
+
/// so splitting the storage never reordered delivery. Entries
|
|
5715
|
+
/// stamped at or past `before` stay staged: they were produced
|
|
5716
|
+
/// during the current drain pass and belong to the next one
|
|
5717
|
+
/// (the `DrainBoundary` causality contract). Both structures
|
|
5718
|
+
/// are FIFO over the monotonic stamp, so refusing the merged
|
|
5719
|
+
/// head refuses everything younger too.
|
|
5720
|
+
fn takePendingMsg(self: *Self, before: u64) ?PendingMsg {
|
|
5721
|
+
const ring_has = self.pending_exit_len > 0 and
|
|
5722
|
+
self.pending_exit_seqs[self.pending_exit_head] < before;
|
|
5723
|
+
const image_has = self.pending_image_len > 0 and
|
|
5724
|
+
self.pendingImageStorage()[self.pending_image_head].seq < before;
|
|
5725
|
+
if (!ring_has and !image_has) return null;
|
|
5726
|
+
const take_image = if (ring_has and image_has)
|
|
5727
|
+
self.pendingImageStorage()[self.pending_image_head].seq < self.pending_exit_seqs[self.pending_exit_head]
|
|
5728
|
+
else
|
|
5729
|
+
image_has;
|
|
5730
|
+
if (take_image) {
|
|
5731
|
+
const staged = self.takePendingImage();
|
|
5732
|
+
return .{ .image = .{
|
|
5733
|
+
.result = staged.result,
|
|
5734
|
+
.image_fn = staged.image_fn,
|
|
5735
|
+
.regenerates = staged.regenerates,
|
|
5736
|
+
} };
|
|
5737
|
+
}
|
|
4574
5738
|
const pending = self.pending_exits[self.pending_exit_head];
|
|
4575
5739
|
self.pending_exit_head = (self.pending_exit_head + 1) % max_effect_pending_exits;
|
|
4576
5740
|
self.pending_exit_len -= 1;
|
|
@@ -4591,6 +5755,84 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4591
5755
|
return null;
|
|
4592
5756
|
}
|
|
4593
5757
|
|
|
5758
|
+
/// The shared admission gate for the keyed effect families
|
|
5759
|
+
/// (spawn/fetch/file/clipboard/image — they share the slots
|
|
5760
|
+
/// and one key space): a key is occupied while its effect runs
|
|
5761
|
+
/// AND through the posted-but-undelivered terminal window that
|
|
5762
|
+
/// follows. Under session replay the same request is a parked
|
|
5763
|
+
/// `.running` fake until its journaled terminal feeds at the
|
|
5764
|
+
/// recorded delivery position, so any admission that accepted
|
|
5765
|
+
/// a key live inside that window would journal a Msg stream
|
|
5766
|
+
/// replay rejects (`ReplayEffectDivergence`). Delivery ends
|
|
5767
|
+
/// the window on both sides at the same causal instant — the
|
|
5768
|
+
/// drain retires the slot before the terminal Msg reaches
|
|
5769
|
+
/// update — so reissuing a key from its own terminal handler
|
|
5770
|
+
/// is always accepted.
|
|
5771
|
+
fn keyOccupiedUntilDelivery(self: *Self, key: u64) bool {
|
|
5772
|
+
if (self.findActiveSlot(key) != null) return true;
|
|
5773
|
+
if (self.findUndeliveredTerminalSlot(key) != null) return true;
|
|
5774
|
+
if (self.stagedImageOccupiesKey(key)) return true;
|
|
5775
|
+
return false;
|
|
5776
|
+
}
|
|
5777
|
+
|
|
5778
|
+
/// A staged loop-side image terminal that is executor truth
|
|
5779
|
+
/// (`regenerates = false` — start failures, fake cancels)
|
|
5780
|
+
/// occupies its id until the drain delivers it. Those
|
|
5781
|
+
/// terminals journal as worker truth and FEED under session
|
|
5782
|
+
/// replay, where the request they answer stays parked in its
|
|
5783
|
+
/// slot until the recorded delivery position — so live
|
|
5784
|
+
/// admission must hold the key through the same window, or a
|
|
5785
|
+
/// key accepted here live is one replay rejects. Regenerating
|
|
5786
|
+
/// validation refusals deliberately do NOT occupy: replay
|
|
5787
|
+
/// re-runs the same loop-side validation at the same dispatch,
|
|
5788
|
+
/// so both sides refuse (and stage) identically with the key
|
|
5789
|
+
/// never held on either side.
|
|
5790
|
+
fn stagedImageOccupiesKey(self: *Self, id: u64) bool {
|
|
5791
|
+
const storage = self.pendingImageStorage();
|
|
5792
|
+
var index: usize = 0;
|
|
5793
|
+
while (index < self.pending_image_len) : (index += 1) {
|
|
5794
|
+
const entry = &storage[(self.pending_image_head + index) % storage.len];
|
|
5795
|
+
if (!entry.regenerates and entry.result.id == id) return true;
|
|
5796
|
+
}
|
|
5797
|
+
return false;
|
|
5798
|
+
}
|
|
5799
|
+
|
|
5800
|
+
/// A slot of ANY kind holding `key` whose terminal is posted
|
|
5801
|
+
/// but not yet delivered: state `.draining` with its
|
|
5802
|
+
/// per-family delivery marker still set. Delivery IS the
|
|
5803
|
+
/// marker handoff, derived from how each family's drain
|
|
5804
|
+
/// retires the slot: the drain takes `fetch_buffer` (fetch,
|
|
5805
|
+
/// file, clipboard, host, and image terminals) or
|
|
5806
|
+
/// `collect_buffer` (collect spawns) before the terminal Msg
|
|
5807
|
+
/// reaches update, and clears `exit_undelivered` (`.lines`
|
|
5808
|
+
/// spawns, which own no delivery buffer) at the same instant;
|
|
5809
|
+
/// every loop-side retire (`releaseFetchSlot`,
|
|
5810
|
+
/// `releaseSpawnSlot`) resets the markers too. So a set marker
|
|
5811
|
+
/// under `.draining` means exactly "terminal still pending" —
|
|
5812
|
+
/// the moment update sees the result, the key is free for
|
|
5813
|
+
/// reuse. Loop-thread only: the state load is the acquire
|
|
5814
|
+
/// pairing with the worker's `.draining` release store (the
|
|
5815
|
+
/// worker's last slot access), and the markers are only ever
|
|
5816
|
+
/// cleared on the loop thread.
|
|
5817
|
+
fn findUndeliveredTerminalSlot(self: *Self, key: u64) ?usize {
|
|
5818
|
+
for (&self.slots, 0..) |*slot, index| {
|
|
5819
|
+
if (slot.key != key) continue;
|
|
5820
|
+
if (slot.state.load(.acquire) != .draining) continue;
|
|
5821
|
+
if (slotTerminalUndelivered(slot)) return index;
|
|
5822
|
+
}
|
|
5823
|
+
return null;
|
|
5824
|
+
}
|
|
5825
|
+
|
|
5826
|
+
/// The per-family "terminal still pending" predicate for a
|
|
5827
|
+
/// slot already observed `.draining` (see
|
|
5828
|
+
/// `findUndeliveredTerminalSlot` for the marker derivations).
|
|
5829
|
+
fn slotTerminalUndelivered(slot: *const Slot) bool {
|
|
5830
|
+
return switch (slot.kind) {
|
|
5831
|
+
.spawn => slot.collect_buffer != null or slot.exit_undelivered,
|
|
5832
|
+
.fetch, .file, .clipboard, .host, .image => slot.fetch_buffer != null,
|
|
5833
|
+
};
|
|
5834
|
+
}
|
|
5835
|
+
|
|
4594
5836
|
/// The most recent no-longer-running occupant with `key` (done
|
|
4595
5837
|
/// or already reclaimed to idle) — the spawn a racing cancel is
|
|
4596
5838
|
/// aimed at.
|
|
@@ -4657,13 +5899,16 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4657
5899
|
joinWorker(slot);
|
|
4658
5900
|
slot.state.store(.idle, .release);
|
|
4659
5901
|
},
|
|
4660
|
-
// A draining slot is reusable once the drain
|
|
4661
|
-
//
|
|
4662
|
-
//
|
|
4663
|
-
//
|
|
5902
|
+
// A draining slot is reusable once the drain
|
|
5903
|
+
// delivered its terminal (took the fetch body or
|
|
5904
|
+
// collected stdout, or cleared a `.lines` exit's
|
|
5905
|
+
// marker). Its worker is already finished either
|
|
5906
|
+
// way: retire the thread now.
|
|
4664
5907
|
.draining => {
|
|
4665
5908
|
joinWorker(slot);
|
|
4666
|
-
if (slot.fetch_buffer == null and slot.collect_buffer == null
|
|
5909
|
+
if (slot.fetch_buffer == null and slot.collect_buffer == null and !slot.exit_undelivered) {
|
|
5910
|
+
slot.state.store(.idle, .release);
|
|
5911
|
+
}
|
|
4667
5912
|
},
|
|
4668
5913
|
else => {},
|
|
4669
5914
|
}
|
|
@@ -4939,10 +6184,32 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4939
6184
|
slot.stderr_ring = ctx.stderr_ring;
|
|
4940
6185
|
slot.stderr_total = ctx.stderr_total;
|
|
4941
6186
|
}
|
|
6187
|
+
// Mark-then-post: the `.lines` undelivered-exit marker must
|
|
6188
|
+
// be published BEFORE the exit is consumable. `postExit`'s
|
|
6189
|
+
// enqueue releases the queue mutex the drain's dequeue
|
|
6190
|
+
// acquires, so this write is ordered before any consume —
|
|
6191
|
+
// the drain-side clear always follows this set. Posting
|
|
6192
|
+
// first would let a drain riding another wake consume the
|
|
6193
|
+
// exit (clearing a still-false marker) before the mark
|
|
6194
|
+
// landed, leaving a set marker no terminal can ever clear:
|
|
6195
|
+
// `reclaimSlots` would hold the slot out of `.idle` forever
|
|
6196
|
+
// and the key would never readmit.
|
|
6197
|
+
if (ctx.output_mode != .collect) slot.exit_undelivered = true;
|
|
4942
6198
|
self.postExit(slot, @intCast(slot_index), generation, io, exit);
|
|
4943
|
-
//
|
|
4944
|
-
//
|
|
4945
|
-
slot
|
|
6199
|
+
// Park in `.draining` until the drain delivers the exit: a
|
|
6200
|
+
// collect slot still owns its stdout buffer (fetch-style),
|
|
6201
|
+
// and a `.lines` slot holds the marker set above — either
|
|
6202
|
+
// way the key stays occupied through the window between the
|
|
6203
|
+
// post and the drain, which is exactly the span session
|
|
6204
|
+
// replay keeps the parked fake occupying
|
|
6205
|
+
// (`findUndeliveredTerminalSlot`). This store must stay
|
|
6206
|
+
// AFTER the post: `reclaimSlots` joins any `.draining`
|
|
6207
|
+
// worker, and `postExit` can park in a full-queue retry
|
|
6208
|
+
// only the loop thread can relieve — staying `.running`
|
|
6209
|
+
// keeps this thread unjoinable until the post lands. It is
|
|
6210
|
+
// also the release the loop's acquire loads pair with, and
|
|
6211
|
+
// the worker's last slot access.
|
|
6212
|
+
slot.state.store(.draining, .release);
|
|
4946
6213
|
self.wakeHost();
|
|
4947
6214
|
}
|
|
4948
6215
|
|
|
@@ -5525,6 +6792,300 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
5525
6792
|
return if (err == error.Canceled) .cancelled else .io_failed;
|
|
5526
6793
|
}
|
|
5527
6794
|
|
|
6795
|
+
// ------------------------------------------------------ image worker
|
|
6796
|
+
|
|
6797
|
+
/// Supervises one image load, mirroring `fetchWorkerMain`: the
|
|
6798
|
+
/// whole source cascade (local probe, cache read, network
|
|
6799
|
+
/// fetch, cache install) runs as ONE cancelable `Io` task,
|
|
6800
|
+
/// polled against `cancel`, `shutdown`, and the effect timeout.
|
|
6801
|
+
/// Unlike bare file effects — which have no deadline at all and
|
|
6802
|
+
/// need the abandon safety net — every blocking phase here is
|
|
6803
|
+
/// deadline-bounded: the timeout (or teardown) cancels the task
|
|
6804
|
+
/// the same way it cancels a fetch exchange, so teardown joins
|
|
6805
|
+
/// image workers unconditionally, exactly like fetch workers.
|
|
6806
|
+
fn imageWorkerMain(self: *Self, slot_index: usize, generation: u32, io: std.Io) void {
|
|
6807
|
+
const slot = &self.slots[slot_index];
|
|
6808
|
+
supervise: {
|
|
6809
|
+
var future = self.startImageExchange(slot, io) catch {
|
|
6810
|
+
// Same refusal as fetch: an inline cascade would
|
|
6811
|
+
// evade cancel, the timeout, and teardown's
|
|
6812
|
+
// unconditional join.
|
|
6813
|
+
slot.fetch_status = 0;
|
|
6814
|
+
slot.body_len = 0;
|
|
6815
|
+
slot.image_outcome = .rejected;
|
|
6816
|
+
if (self.fetch_start_rejections.fetchAdd(1, .monotonic) == 0) {
|
|
6817
|
+
std.debug.print(
|
|
6818
|
+
"effects image: the executor could not start a cancelable load for image id {d}; rejecting the load (an inline cascade would evade cancel, the timeout, and teardown)\n",
|
|
6819
|
+
.{slot.key},
|
|
6820
|
+
);
|
|
6821
|
+
}
|
|
6822
|
+
break :supervise;
|
|
6823
|
+
};
|
|
6824
|
+
const poll_ms: u64 = 5;
|
|
6825
|
+
var waited_ms: u64 = 0;
|
|
6826
|
+
var timed_out = false;
|
|
6827
|
+
while (true) {
|
|
6828
|
+
if (slot.fetch_done.load(.acquire)) break;
|
|
6829
|
+
if (self.shutdown.load(.acquire) or slot.cancel_requested.load(.acquire)) {
|
|
6830
|
+
future.cancel(io);
|
|
6831
|
+
break;
|
|
6832
|
+
}
|
|
6833
|
+
if (waited_ms >= slot.timeout_ms) {
|
|
6834
|
+
timed_out = true;
|
|
6835
|
+
future.cancel(io);
|
|
6836
|
+
break;
|
|
6837
|
+
}
|
|
6838
|
+
std.Io.sleep(io, std.Io.Duration.fromMilliseconds(poll_ms), .awake) catch {};
|
|
6839
|
+
waited_ms += poll_ms;
|
|
6840
|
+
}
|
|
6841
|
+
future.await(io);
|
|
6842
|
+
if (!slot.fetch_done.load(.acquire)) {
|
|
6843
|
+
// Interrupted before the task recorded a terminal
|
|
6844
|
+
// state (never expected — the task always records).
|
|
6845
|
+
slot.fetch_status = 0;
|
|
6846
|
+
slot.body_len = 0;
|
|
6847
|
+
slot.image_outcome = if (timed_out) .timed_out else .cancelled;
|
|
6848
|
+
} else if (timed_out and slot.image_outcome != .loaded) {
|
|
6849
|
+
// The interruption was the deadline, not the app:
|
|
6850
|
+
// every non-staged outcome here is the timeout's
|
|
6851
|
+
// doing. A cascade that staged its bytes before the
|
|
6852
|
+
// deadline fired stays a delivered `.loaded`.
|
|
6853
|
+
slot.image_outcome = .timed_out;
|
|
6854
|
+
slot.fetch_status = 0;
|
|
6855
|
+
slot.body_len = 0;
|
|
6856
|
+
}
|
|
6857
|
+
}
|
|
6858
|
+
self.postImage(slot, @intCast(slot_index), generation, io);
|
|
6859
|
+
slot.state.store(.draining, .release);
|
|
6860
|
+
self.wakeHost();
|
|
6861
|
+
}
|
|
6862
|
+
|
|
6863
|
+
/// Start the blocking cascade as a cancelable task on the
|
|
6864
|
+
/// executor io — the only way it may run (see
|
|
6865
|
+
/// `startFetchExchange` for the contract). Shares the fetch
|
|
6866
|
+
/// injection switch so tests pin the rejection path.
|
|
6867
|
+
fn startImageExchange(self: *Self, slot: *Slot, io: std.Io) std.Io.ConcurrentError!std.Io.Future(void) {
|
|
6868
|
+
if (!self.fetch_concurrent_start) return error.ConcurrencyUnavailable;
|
|
6869
|
+
return std.Io.concurrent(io, imageTask, .{ self, slot, io });
|
|
6870
|
+
}
|
|
6871
|
+
|
|
6872
|
+
/// The blocking cascade, run as a cancelable task. Always
|
|
6873
|
+
/// records a terminal source-stage state in the slot before
|
|
6874
|
+
/// `fetch_done`.
|
|
6875
|
+
fn imageTask(self: *Self, slot: *Slot, io: std.Io) void {
|
|
6876
|
+
defer slot.fetch_done.store(true, .release);
|
|
6877
|
+
self.runImageLoad(slot, io) catch |err| {
|
|
6878
|
+
slot.body_len = 0;
|
|
6879
|
+
slot.image_outcome = classifyImageFetchError(err);
|
|
6880
|
+
};
|
|
6881
|
+
}
|
|
6882
|
+
|
|
6883
|
+
/// The source cascade, the `playAudio` resolution order made
|
|
6884
|
+
/// byte-producing: local file first (a MISSING file is the one
|
|
6885
|
+
/// local failure that falls through to the url — everything
|
|
6886
|
+
/// else is terminal, because retrying a different source would
|
|
6887
|
+
/// mask the real problem), then a verified cache entry, then
|
|
6888
|
+
/// the network with a cache install behind it. On success the
|
|
6889
|
+
/// encoded bytes sit in the slot buffer with
|
|
6890
|
+
/// `image_outcome == .loaded`; the drain decodes and registers
|
|
6891
|
+
/// them on the loop thread.
|
|
6892
|
+
fn runImageLoad(self: *Self, slot: *Slot, io: std.Io) !void {
|
|
6893
|
+
const buffer = slot.fetch_buffer.?;
|
|
6894
|
+
const cwd = std.Io.Dir.cwd();
|
|
6895
|
+
if (slot.image_path_len > 0) {
|
|
6896
|
+
if (cwd.openFile(io, slot.imagePath(), .{})) |file_value| {
|
|
6897
|
+
var file = file_value;
|
|
6898
|
+
defer file.close(io);
|
|
6899
|
+
const len = file.readPositionalAll(io, buffer, 0) catch |err| {
|
|
6900
|
+
slot.image_outcome = if (err == error.Canceled) .cancelled else .io_failed;
|
|
6901
|
+
slot.body_len = 0;
|
|
6902
|
+
return;
|
|
6903
|
+
};
|
|
6904
|
+
if (len > max_effect_image_bytes) {
|
|
6905
|
+
slot.image_outcome = .too_large;
|
|
6906
|
+
slot.body_len = 0;
|
|
6907
|
+
return;
|
|
6908
|
+
}
|
|
6909
|
+
slot.body_len = len;
|
|
6910
|
+
slot.image_outcome = .loaded;
|
|
6911
|
+
return;
|
|
6912
|
+
} else |err| {
|
|
6913
|
+
if (err == error.Canceled) {
|
|
6914
|
+
slot.image_outcome = .cancelled;
|
|
6915
|
+
slot.body_len = 0;
|
|
6916
|
+
return;
|
|
6917
|
+
}
|
|
6918
|
+
if (err != error.FileNotFound or slot.url_len == 0) {
|
|
6919
|
+
slot.image_outcome = if (err == error.FileNotFound) .not_found else .io_failed;
|
|
6920
|
+
slot.body_len = 0;
|
|
6921
|
+
return;
|
|
6922
|
+
}
|
|
6923
|
+
// Missing local file with a url: fall through.
|
|
6924
|
+
}
|
|
6925
|
+
}
|
|
6926
|
+
if (slot.image_cache_len > 0) {
|
|
6927
|
+
// The probe's only error is a spent cancel: terminate
|
|
6928
|
+
// `.cancelled` like the local-probe arm above — never
|
|
6929
|
+
// fall through to a network fetch the cancel can no
|
|
6930
|
+
// longer reach.
|
|
6931
|
+
const cached = self.readImageCache(slot, io, buffer) catch {
|
|
6932
|
+
slot.image_outcome = .cancelled;
|
|
6933
|
+
slot.body_len = 0;
|
|
6934
|
+
return;
|
|
6935
|
+
};
|
|
6936
|
+
if (cached) return;
|
|
6937
|
+
}
|
|
6938
|
+
try self.fetchImageBytes(slot, io, buffer);
|
|
6939
|
+
if (slot.image_outcome == .loaded and slot.image_cache_len > 0) {
|
|
6940
|
+
self.installImageCache(slot, io, buffer[0..slot.body_len]);
|
|
6941
|
+
}
|
|
6942
|
+
}
|
|
6943
|
+
|
|
6944
|
+
/// Probe the cache entry for a url source: it qualifies only
|
|
6945
|
+
/// when readable, within the source bound, and — when the
|
|
6946
|
+
/// caller declared `expected_bytes` — exactly that size (the
|
|
6947
|
+
/// integrity gate). Anything else falls through to the network,
|
|
6948
|
+
/// which refreshes the entry — except a cancel interruption,
|
|
6949
|
+
/// which propagates: cancel delivery is ONE-SHOT (the Canceled
|
|
6950
|
+
/// error return IS the delivery), so treating it as a cache
|
|
6951
|
+
/// miss would consume it and send a cancelled load into the
|
|
6952
|
+
/// network fetch with nothing left to interrupt it.
|
|
6953
|
+
fn readImageCache(self: *Self, slot: *Slot, io: std.Io, buffer: []u8) error{Canceled}!bool {
|
|
6954
|
+
_ = self;
|
|
6955
|
+
const cwd = std.Io.Dir.cwd();
|
|
6956
|
+
var file = cwd.openFile(io, slot.imageCache(), .{}) catch |err|
|
|
6957
|
+
return if (err == error.Canceled) error.Canceled else false;
|
|
6958
|
+
defer file.close(io);
|
|
6959
|
+
const len = file.readPositionalAll(io, buffer, 0) catch |err|
|
|
6960
|
+
return if (err == error.Canceled) error.Canceled else false;
|
|
6961
|
+
if (len == 0 or len > max_effect_image_bytes) return false;
|
|
6962
|
+
if (slot.image_expected_bytes != 0 and len != slot.image_expected_bytes) return false;
|
|
6963
|
+
slot.body_len = len;
|
|
6964
|
+
// `fetch_status` stays 0 on a cache hit: no HTTP exchange
|
|
6965
|
+
// occurred, and 0 says so honestly (the documented
|
|
6966
|
+
// `EffectImageResult.status` contract) — fabricating the
|
|
6967
|
+
// origin's 200 would claim an exchange that never happened.
|
|
6968
|
+
slot.image_outcome = .loaded;
|
|
6969
|
+
return true;
|
|
6970
|
+
}
|
|
6971
|
+
|
|
6972
|
+
/// GET the url whole into the slot buffer, mirroring
|
|
6973
|
+
/// `runFetch`'s exchange discipline. Non-2xx statuses terminate
|
|
6974
|
+
/// as `.http_status` with the body discarded — an error page is
|
|
6975
|
+
/// not an image — and bodies over the source bound terminate
|
|
6976
|
+
/// `.too_large` whole (a cut image can never decode, so there
|
|
6977
|
+
/// is no truncated delivery).
|
|
6978
|
+
fn fetchImageBytes(self: *Self, slot: *Slot, io: std.Io, buffer: []u8) !void {
|
|
6979
|
+
const uri = try std.Uri.parse(slot.fetchUrl());
|
|
6980
|
+
var client: std.http.Client = .{ .allocator = self.allocator, .io = io };
|
|
6981
|
+
defer client.deinit();
|
|
6982
|
+
var request = client.request(.GET, uri, .{
|
|
6983
|
+
.keep_alive = false,
|
|
6984
|
+
.redirect_behavior = @enumFromInt(3),
|
|
6985
|
+
}) catch |err| {
|
|
6986
|
+
// See `runFetch`: an untyped failure here is a connect
|
|
6987
|
+
// failure.
|
|
6988
|
+
if (err == error.Unexpected) return error.ConnectPhaseFailed;
|
|
6989
|
+
return err;
|
|
6990
|
+
};
|
|
6991
|
+
defer request.deinit();
|
|
6992
|
+
try request.sendBodiless();
|
|
6993
|
+
var redirect_buffer: [8 * 1024]u8 = undefined;
|
|
6994
|
+
var response = try request.receiveHead(&redirect_buffer);
|
|
6995
|
+
slot.fetch_status = @intFromEnum(response.head.status);
|
|
6996
|
+
if (response.head.status.class() != .success) {
|
|
6997
|
+
slot.image_outcome = .http_status;
|
|
6998
|
+
slot.body_len = 0;
|
|
6999
|
+
return;
|
|
7000
|
+
}
|
|
7001
|
+
const decompress_buffer: []u8 = switch (response.head.content_encoding) {
|
|
7002
|
+
.identity => &.{},
|
|
7003
|
+
.zstd => try self.allocator.alloc(u8, std.compress.zstd.default_window_len),
|
|
7004
|
+
.deflate, .gzip => try self.allocator.alloc(u8, std.compress.flate.max_window_len),
|
|
7005
|
+
.compress => return error.UnsupportedCompressionMethod,
|
|
7006
|
+
};
|
|
7007
|
+
defer if (decompress_buffer.len > 0) self.allocator.free(decompress_buffer);
|
|
7008
|
+
var transfer_buffer: [4096]u8 = undefined;
|
|
7009
|
+
var decompress: std.http.Decompress = undefined;
|
|
7010
|
+
const reader = response.readerDecompressing(&transfer_buffer, &decompress, decompress_buffer);
|
|
7011
|
+
var body_writer = std.Io.Writer.fixed(buffer);
|
|
7012
|
+
var over_bound = false;
|
|
7013
|
+
_ = reader.streamRemaining(&body_writer) catch |err| switch (err) {
|
|
7014
|
+
// The buffer's spare byte filled: the source is over
|
|
7015
|
+
// the bound, whole-failure by policy.
|
|
7016
|
+
error.WriteFailed => over_bound = true,
|
|
7017
|
+
error.ReadFailed => return response.bodyErr() orelse error.ReadFailed,
|
|
7018
|
+
};
|
|
7019
|
+
if (over_bound or body_writer.end > max_effect_image_bytes) {
|
|
7020
|
+
slot.image_outcome = .too_large;
|
|
7021
|
+
slot.body_len = 0;
|
|
7022
|
+
return;
|
|
7023
|
+
}
|
|
7024
|
+
slot.body_len = body_writer.end;
|
|
7025
|
+
slot.image_outcome = .loaded;
|
|
7026
|
+
}
|
|
7027
|
+
|
|
7028
|
+
/// Best-effort cache install behind a successful fetch: write
|
|
7029
|
+
/// a writer-unique temp beside the cache path (see
|
|
7030
|
+
/// `imageCachePartialPath` — concurrent installs toward one
|
|
7031
|
+
/// cache path must never share a temp, across channels and
|
|
7032
|
+
/// processes included) and atomically rename into place, and
|
|
7033
|
+
/// only when `expected_bytes` (if declared) verifies — a
|
|
7034
|
+
/// partial or wrong-size download never occupies the cache
|
|
7035
|
+
/// name. Failures cost only the next load a re-fetch, and a
|
|
7036
|
+
/// failed install deletes its own temp so the cache directory
|
|
7037
|
+
/// never accumulates this process's debris; only a hard crash
|
|
7038
|
+
/// mid-install can leave one behind, in the OS-purgeable
|
|
7039
|
+
/// caches directory.
|
|
7040
|
+
fn installImageCache(self: *Self, slot: *Slot, io: std.Io, bytes: []const u8) void {
|
|
7041
|
+
_ = self;
|
|
7042
|
+
if (slot.image_expected_bytes != 0 and bytes.len != slot.image_expected_bytes) return;
|
|
7043
|
+
const cache_path = slot.imageCache();
|
|
7044
|
+
const cwd = std.Io.Dir.cwd();
|
|
7045
|
+
if (std.fs.path.dirname(cache_path)) |parent| {
|
|
7046
|
+
cwd.createDirPath(io, parent) catch return;
|
|
7047
|
+
}
|
|
7048
|
+
// The uniqueness token comes from the operation's own
|
|
7049
|
+
// executor io — the CSPRNG seam every worker already
|
|
7050
|
+
// carries — so the name is unique across channels and
|
|
7051
|
+
// processes, not merely within this channel's generation
|
|
7052
|
+
// counter. Freestanding builds never reach this function
|
|
7053
|
+
// (no executor io exists there to run a worker), so the
|
|
7054
|
+
// entropy source needs no target gate.
|
|
7055
|
+
var token_bytes: [8]u8 = undefined;
|
|
7056
|
+
io.random(&token_bytes);
|
|
7057
|
+
const token = std.mem.readInt(u64, &token_bytes, .little);
|
|
7058
|
+
var partial_buffer: [max_effect_image_path_bytes + 48]u8 = undefined;
|
|
7059
|
+
const partial_path = imageCachePartialPath(&partial_buffer, cache_path, slot.generation, token) catch return;
|
|
7060
|
+
cwd.writeFile(io, .{ .sub_path = partial_path, .data = bytes }) catch {
|
|
7061
|
+
cwd.deleteFile(io, partial_path) catch {};
|
|
7062
|
+
return;
|
|
7063
|
+
};
|
|
7064
|
+
cwd.rename(partial_path, cwd, cache_path, io) catch {
|
|
7065
|
+
cwd.deleteFile(io, partial_path) catch {};
|
|
7066
|
+
};
|
|
7067
|
+
}
|
|
7068
|
+
|
|
7069
|
+
/// The terminal image entry must never be dropped: retry until
|
|
7070
|
+
/// the loop thread drains space, giving up only on shutdown.
|
|
7071
|
+
fn postImage(self: *Self, slot: *Slot, slot_index: u16, generation: u32, io: std.Io) void {
|
|
7072
|
+
var entry: Entry = .{
|
|
7073
|
+
.kind = .image,
|
|
7074
|
+
.slot_index = slot_index,
|
|
7075
|
+
.generation = generation,
|
|
7076
|
+
.key = slot.key,
|
|
7077
|
+
.line_len = @intCast(slot.body_len),
|
|
7078
|
+
.status = slot.fetch_status,
|
|
7079
|
+
.image_outcome = slot.image_outcome,
|
|
7080
|
+
.image_fn = slot.on_image,
|
|
7081
|
+
};
|
|
7082
|
+
while (!self.enqueue(&entry)) {
|
|
7083
|
+
if (self.shutdown.load(.acquire)) return;
|
|
7084
|
+
self.wakeHost();
|
|
7085
|
+
std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake) catch {};
|
|
7086
|
+
}
|
|
7087
|
+
}
|
|
7088
|
+
|
|
5528
7089
|
/// The terminal file result must never be dropped: retry until
|
|
5529
7090
|
/// the loop thread drains space, giving up only on shutdown.
|
|
5530
7091
|
fn postFile(self: *Self, slot: *Slot, slot_index: u16, generation: u32, io: std.Io, outcome: EffectFileOutcome, read_len: usize) void {
|
|
@@ -5579,6 +7140,66 @@ test "fx timer platform ids stay inside the reserved range and clear of the mark
|
|
|
5579
7140
|
try std.testing.expect(effect_timer_platform_id_base + max_effect_timers - 1 < 0xffff_ffff_2e70_a11c);
|
|
5580
7141
|
}
|
|
5581
7142
|
|
|
7143
|
+
test "image cache probe propagates a cancel interruption and reads every other failure as a miss" {
|
|
7144
|
+
const TestMsg = union(enum) { result: EffectImageResult };
|
|
7145
|
+
const Channel = Effects(TestMsg);
|
|
7146
|
+
const Fakes = struct {
|
|
7147
|
+
fn openCanceled(userdata: ?*anyopaque, dir: std.Io.Dir, sub_path: []const u8, options: std.Io.Dir.OpenFileOptions) std.Io.File.OpenError!std.Io.File {
|
|
7148
|
+
_ = userdata;
|
|
7149
|
+
_ = dir;
|
|
7150
|
+
_ = sub_path;
|
|
7151
|
+
_ = options;
|
|
7152
|
+
return error.Canceled;
|
|
7153
|
+
}
|
|
7154
|
+
fn openOk(userdata: ?*anyopaque, dir: std.Io.Dir, sub_path: []const u8, options: std.Io.Dir.OpenFileOptions) std.Io.File.OpenError!std.Io.File {
|
|
7155
|
+
_ = userdata;
|
|
7156
|
+
_ = dir;
|
|
7157
|
+
_ = sub_path;
|
|
7158
|
+
_ = options;
|
|
7159
|
+
return .{ .handle = 0, .flags = .{ .nonblocking = false } };
|
|
7160
|
+
}
|
|
7161
|
+
fn readCanceled(userdata: ?*anyopaque, file: std.Io.File, data: []const []u8, offset: u64) std.Io.File.ReadPositionalError!usize {
|
|
7162
|
+
_ = userdata;
|
|
7163
|
+
_ = file;
|
|
7164
|
+
_ = data;
|
|
7165
|
+
_ = offset;
|
|
7166
|
+
return error.Canceled;
|
|
7167
|
+
}
|
|
7168
|
+
fn closeNoop(userdata: ?*anyopaque, files: []const std.Io.File) void {
|
|
7169
|
+
_ = userdata;
|
|
7170
|
+
_ = files;
|
|
7171
|
+
}
|
|
7172
|
+
};
|
|
7173
|
+
|
|
7174
|
+
var slot: Channel.Slot = .{};
|
|
7175
|
+
const cache_path = "caches/images/probe.png";
|
|
7176
|
+
@memcpy(slot.image_cache_storage[0..cache_path.len], cache_path);
|
|
7177
|
+
slot.image_cache_len = cache_path.len;
|
|
7178
|
+
var buffer: [32]u8 = undefined;
|
|
7179
|
+
|
|
7180
|
+
// Cancel delivery is one-shot: a cancel that interrupts the probe's
|
|
7181
|
+
// open IS the delivery, so the probe must surface it — reading it
|
|
7182
|
+
// as a miss would send the cancelled load into the network fetch
|
|
7183
|
+
// with nothing left to interrupt.
|
|
7184
|
+
var open_cancel_vtable = std.Io.failing.vtable.*;
|
|
7185
|
+
open_cancel_vtable.dirOpenFile = Fakes.openCanceled;
|
|
7186
|
+
const open_cancel_io: std.Io = .{ .userdata = null, .vtable = &open_cancel_vtable };
|
|
7187
|
+
try std.testing.expectError(error.Canceled, Channel.readImageCache(undefined, &slot, open_cancel_io, &buffer));
|
|
7188
|
+
|
|
7189
|
+
// A cancel landing in the positional read, same contract.
|
|
7190
|
+
var read_cancel_vtable = std.Io.failing.vtable.*;
|
|
7191
|
+
read_cancel_vtable.dirOpenFile = Fakes.openOk;
|
|
7192
|
+
read_cancel_vtable.fileReadPositional = Fakes.readCanceled;
|
|
7193
|
+
read_cancel_vtable.fileClose = Fakes.closeNoop;
|
|
7194
|
+
const read_cancel_io: std.Io = .{ .userdata = null, .vtable = &read_cancel_vtable };
|
|
7195
|
+
try std.testing.expectError(error.Canceled, Channel.readImageCache(undefined, &slot, read_cancel_io, &buffer));
|
|
7196
|
+
|
|
7197
|
+
// Every other failure stays an honest miss that falls through to
|
|
7198
|
+
// the network (`std.Io.failing`'s empty filesystem opens report
|
|
7199
|
+
// FileNotFound).
|
|
7200
|
+
try std.testing.expectEqual(false, try Channel.readImageCache(undefined, &slot, std.Io.failing, &buffer));
|
|
7201
|
+
}
|
|
7202
|
+
|
|
5582
7203
|
test "fetch errors map onto the documented taxonomy" {
|
|
5583
7204
|
try std.testing.expectEqual(EffectFetchOutcome.cancelled, classifyFetchError(error.Canceled));
|
|
5584
7205
|
try std.testing.expectEqual(EffectFetchOutcome.connect_failed, classifyFetchError(error.ConnectionRefused));
|