Skip to content

A JS effect gets and answers Bend's own text and converts it itself; an export converts all of its text - #1825

Open
nicolas-abril wants to merge 10 commits into
mainfrom
ai/effect-text-explicit
Open

nicolas-abril wants to merge 10 commits into
mainfrom
ai/effect-text-explicit

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

A C effect gets a String as a Bend term and turns it into bytes itself: io_cstr validates and writes U+FFFD for a Char that is no Unicode scalar, io_cbuf copies raw bytes, and io_str builds a text answer. The JS side now follows the same rule: an effect converts its own text, in both directions. Before this, the compiler's foreign-effect wrapper also ran str_out on top-level text arguments and str_in on text answers. That converted text twice on most paths (the wrapper, then io_bytes, and a third time in get_env.js), and it left nested text, such as Process.run's List(String) args, to the effect anyway.

Change

  • comp.ts: an effect's wrapper no longer converts text: an effect gets and answers Bend's own text, at any depth, and converts it itself. A module export converts all of its text, at any depth.
    • host() becomes host_in(A, x): nat_in for a Nat, Math.fround for an F32, applied to an effect's answer and to an export's arguments.
    • run_lib runs an export's arguments through host_text(·, str_in, str_out) and its answer through host_text(·, str_out, str_in). The walk goes by value, not type: in the JS runtime only Char and String are strings, so every string it meets is Bend text. It therefore covers what a type-directed conversion can't: a polymorphic def (poly(-A, x: A) -> A), text in a ctor, list or array, and a function in either direction, which gets wrapped with the directions swapped for its arguments. On main only a top-level String or Char was converted.
    • A value with no text to change comes back as itself (===), so GUIDE.md's "an Array argument is the caller's own array, updated in place" still holds. A shared value is walked once (memoized by identity), so sharing can't make the walk exponential. Cost: a 1M-element Array<U32> takes 25 ms on Bun and 32 ms on Node per crossing.
  • Base effects: each converts its own text.
    • args.js needs no str_in, and says why: Node and Bun decode argv as UTF-8, a bad byte as U+FFFD, so it can't hold a lone surrogate (checked with CESU-8 ED A0 80 and ED B0 80, on Bun 1.3.14 and Node 26.3.1).
    • get_env.js answers through str_in, because process.env can be set from JS, and Bun keeps a lone surrogate set there.
    • window_open.js checks for a NUL in str_out(title), as window.c runs io_cstr then io_nul. Bend's text for a non-scalar Char holds zeros (Chr{55296} is "\udc00\^@\^[\^@"), so a check on the raw title would read a NUL that isn't there.
    • A conversion that cannot change its text is left out: hosts go to io_addr, which already rejects whatever str_out would change; Process.run's output is decoded UTF-8; a UDP peer's host is a dotted quad; Window.open's message is ASCII.
    • UDP.send_to hands its unsent text back in Fail (and the try_ twin in Wait) exactly, on both lanes. In C, udp_send_to.c reads the datagram without consuming it (udp_send_buf, io_cbuf's walk done with term_peek) and keeps the value in w->item, which hands it back on Fail or Wait and drops it after a send. Before, the C lane rebuilt the value from the UTF-8 copy, so Chr{55296} came back as U+FFFD while JS gave 55296.
  • bend-lint (tools/bend-lint/src/bend/effects.js): every String crossing the lint channel goes in with str_in and out with str_out, nested text included. That includes a source's text, which can be an editor's unsaved buffer.
  • guide/EFFECTS.md: the JS section's text guidance, nested text included:
    • Text arrives Bend-encoded. Text leaving Bend goes through io_bytes/str_out; text coming in goes through str_in/io_text.
    • Bend's text is not a host string. A non-scalar Char is \uDC00 and three 11-bit parts, which may be 0, so a check for a NUL, a length or a pattern tests the converted text, as window_open.js does.
    • A Char answer is char_new(code), not io_text, which can give "" or several characters.
    • Host text that may hold a lone surrogate (JSON.parse, text built from code units, process.env) needs str_in, because a lone \uDC00 would read as a packed Char with no error. Text that cannot hold one (a literal of valid text, io_text, argv) goes in as it is.
    • A module export converts text itself, at any depth: its caller gives and gets host strings, and a lone surrogate going in or a non-scalar Char coming out reads as U+FFFD. A function that crosses converts its own arguments and answer. GUIDE.md says text crosses as a host string, and that a value with no text to change crosses without a copy.
  • Tests:
    • marshal_char_scalar has echo (returns the Char, which reads back as 55296) and seen (returns str_in(str_out(c)), which reads back as 65533).
    • marshal_string_scalar's valid answers stay raw host strings, which need no str_in; torn (a lone surrogate) calls str_in itself.
    • window_open_args adds a title holding Chr{55296}: accepted on every lane. Without the window_open.js fix, the JS lane and the interpreter answered EILSEQ.
    • marshal_string_scalar adds torn_low (str_in("a\uDC00bcd")), which counts 5. It counts 2 without str_in, the packed-Char misread. torn now prints its codes, 97;65533;, so a missing str_in shows as 97;55296;; its old count was 2 either way.
    • udp_bad_address sends SCon{Chr{55296}, "x"} to a bad host and reads the kept text's first code: 55296 on every lane (65533 on C before). It then sends the same datagram successfully.
    • marshal_char_scalar adds echo_list: a List<String> holding Chr{55296} crosses an effect and back as it is, 55296.
    • New text_host_export builds a .mjs and calls its exports:
      • Valid and astral text round-trips.
      • The packed form passed in ("\uDC00\0\^[\0") is just host text with a lone surrogate: it becomes "\uFFFD\0\^[\0", counts 5 Chars plus 1 for "x", and a lone "\uDC00" Char reads 65533.
      • Chr{55296} reaches the caller as U+FFFD in a String, a Char, a list, an array, a tuple through a polymorphic def, a returned closure's answer, and a host callback's argument. The callback's lone-surrogate answer comes back cleaned.
      • An Array with no text comes back === and updated in place, and so does a ctor with no text to change.
      • Removing the walk fails it, and so does always copying.

Breaking for third-party .js effects and for JS callers of exports. An effect that passed a top-level String straight to a host API now calls str_out or io_bytes, and one that answers a host string that may hold a lone surrogate calls str_in. A caller of an export now gets nested text converted too (on main only a top-level String/Char was), and a function it gets or passes converts on each call. Valid text behaves the same either way; only non-scalar Chars and lone surrogates differ.

This replaces most of #1549, which moves the extra str_out calls the other way (out of the effects, kept in the wrapper).

Checks

  • repo: merged with current main, comp.ts is 66,118 ttok, 118 over its 66,000 cap, so gates/repo.ts passes 53 / 54. GUIDE.md is 7,999 of 8,000. The export walk costs about 200 ttok, and it needs a simplification elsewhere in comp.ts before merging.
  • Export walk: the 13 tests that build a module pass from the repo root. run_lib is only used by exports, so other programs' output is unchanged. The suite-wide numbers below predate the walk.
  • Local, after the latest commit: every test, C and JS lanes, on main and on main + this PR, with a 5 s alarm. The results are the same apart from the new and changed tests, which pass (876 C, 916 JS). tools/bend-lint's bun test: 134 / 134. The tests that build a module run their JS lane from the repo root; all 13 pass on both trees. The 3 C failures are this machine's: gfx/window and gfx/clicks expect "no display", and io/stack_fault_trap exits 138 under the runner.
  • Local (macOS, after the rebase onto main): the 50 io and gfx tests that use the changed effects, interpreted, as JS and native, windows opened on a desktop. The only failures also fail on main on the same machine: clicks and window expect "no display", tcp_recv_max_huge differs by a trailing space, and process_run_node_module's JS lane.
  • The cluster gates (test, ping, perf) have not run since the rebase. Before it: test 1707/1707, ping 72/72, perf 123/124 (histogram PAR-CPU at 1.25x the pin, which also missed on A long loop over F32 is a call on the host too: nbody 1.19x SEQ, 1.15x PAR (#1240) #1814, whose C is byte-identical to main's).

🤖 Generated with Claude Code

nicolas-abril and others added 6 commits October 28, 2026 16:52
… as a C effect does

The foreign-effect wrapper no longer runs str_out on top-level text
arguments or str_in on text answers. An effect converts its text with the
runtime's helpers: io_bytes or str_out on the way out (as io_cstr in C),
io_text or str_in on the way in (as io_str). The base effects already did,
so text no longer goes through str_out twice, and UDP.send_to's unsent
text comes back exact. Module exports still convert their text, since no
effect stands between them and the host. Nat and F32 answers keep their
checks.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…CTS.md says so for Char too

The base effects that answered host strings raw (IO.args, IO.get_env,
Process.run, UDP.recv_from's host, Window.open's message) now answer
through str_in, and those that hand Bend's host text to io_addr pass it
through str_out. bend-lint's effects convert every String field both
ways. marshal_string_scalar answers each host literal through str_in.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ost, an export's crossing with its text

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…for an effect answer, so comp.ts fits its 66k ttok cap

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ttok cap, to be made up elsewhere

This reverts commit 6c3dc0a.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril force-pushed the ai/effect-text-explicit branch from c6d3ab1 to a23de92 Compare October 28, 2026 15:52
nicolas-abril and others added 3 commits October 28, 2026 17:07
…ffects drop conversions that cannot change their text

A Char that is no Unicode scalar is Bend text that holds zeros, so the JS
title check read Chr{55296} as a NUL; it now runs on str_out(title), as
window.c's io_cstr then io_nul. io_addr already rejects whatever str_out
would change, Process.run's output is decoded UTF-8, a peer's host is a
dotted quad, and Window.open's message is ASCII: none converts.
marshal_string_scalar's valid answers go raw again; only torn calls str_in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t cannot change their text; EFFECTS.md says what Bend's text is

host_in(A, x, text) is any value in from a host: a Nat through nat_in, an
F32 rounded, and, for an export's caller (text), a String or Char through
str_in; an effect answers Bend's own text, so its answer passes
text=false. host_out is an export's answer, its text through str_out.
The direction is the name, as with str_in and str_out, in place of
host_in plus a lib_host taking an out flag.

args.js and get_env.js drop str_in: Node and Bun decode argv and env as
UTF-8, a bad byte as U+FFFD (checked with ED A0 80 and ED B0 80 on Bun
1.3 and Node 26), so neither holds a lone surrogate. bend-lint passes a
source's path and text as they are, read as UTF-8, instead of scanning
every source's whole text on each rule run.

EFFECTS.md's JS section says that Bend's text is not a host string (a
non-scalar Char is \uDC00 and three 11-bit parts, zeros included), so a
check on it tests the converted text, as window_open.js does; that a
Char answer is char_new(code), not io_text; and which host text needs
str_in (what may hold a lone surrogate) and which does not.

Tests: text_host_export pins an export's str_in and str_out (each,
removed, fails it); marshal_string_scalar's torn_low pins str_in on a
lone DC00 (counts 2 without it, 5 with); marshal_char_scalar's echo_list
pins that nested text crosses an effect as it is. window_open_args'
header is reflowed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ctly in C too; get_env and bend-lint keep str_in

An export now passes text as it is, both ways and at any depth, as an
effect does: only its caller knows which strings are host text and which
are Bend's, so it converts them itself. host_out goes, and host_in is
Nat and F32 only, for an effect's answer and an export's argument alike.
EFFECTS.md says so, with str_in's and str_out's one-line rules, and
GUIDE.md points there.

udp_send_to.c reads its datagram without taking it (udp_send_buf, as
io_cbuf but by term_peek) and keeps it in w->item: Fail and Wait hand
back exactly what was given, Chr{55296} as 55296, as the JS lane does,
and a sent one is dropped. udp_bad_address pins it (65533 on C before).

get_env.js runs str_in again: process.env may be set from JS, and Bun
keeps a lone surrogate set there. bend-lint runs str_in on a source's
path and text again: a source may be an editor's unsaved buffer.
EFFECTS.md no longer calls every literal safe, only one of valid text.
marshal_string_scalar's torn prints its codes, so a missing str_in shows
(97;55296; for 97;65533;).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An export's arguments and answer are walked at runtime: every JS string
in them is Bend text (only Char and String are strings), so the walk
needs no type and covers a polymorphic def, nested data, arrays and
closures in either direction. A value with no text to change stays
itself, so an Array argument is still updated in place, and a shared
value is walked once. The packing of a non-scalar Char no longer
reaches a caller, which gets U+FFFD, as for a lone surrogate going in.
Effects still get and answer Bend's own text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril changed the title A JS effect gets and answers Bend's own text, and converts it itself, as a C effect does A JS effect gets and answers Bend's own text and converts it itself; an export converts all of its text Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant