gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/proc/server.hl

11.3 KB

  1. \* hl:proc — child processes as INSTANCE EVENTS.
  2. let server = spawn('llama-server -m ' + model + ' --port 8480')
  3. on server.line({ line, stream }) { … } \\ stream: 'stdout' | 'stderr'
  4. on server.exit({ exit, pid }) { … } \\ always arrives, kill or not
  5. server.kill() \\ SIGTERM; exit still fires
  6. server.write(text) \\ to its stdin ({ stdin = 'pipe' }): String or Bytes, raw
  7. server.end() \\ close its stdin: it reads EOF
  8. A child spawned with `{ binary = true }` delivers its output as raw Bytes
  9. instead of text lines — `on server.data({ data, stream })`, one event per
  10. chunk as it arrives, nothing split and nothing replaced. The default stays
  11. text lines.
  12. `stream` names the PIPE, not a verdict — most servers (llama.cpp included)
  13. write their whole LOG to stderr, keeping stdout for data.
  14. A CHILD DIES WITH ITS PARENT: every child is started with the kernel's
  15. parent-death signal set to SIGTERM (Linux PR_SET_PDEATHSIG), so when this
  16. program exits — any exit, a crash or Ctrl-C included — its children get
  17. SIGTERM and no orphan keeps a port. There is no option to turn it off. The
  18. setting is the child's own and is not inherited by what IT starts, so a
  19. process meant to outlive you is started one level down, by the child:
  20. spawnArgs([ 'sh', '-c', 'long-job &' ]). A child whose parent is already
  21. gone when it starts (the parent died between the fork and the setting)
  22. ends at once instead of running on. Neither a dead parent nor a fork racing
  23. a thread's allocation may leave a child spinning (ticket #421;
  24. projects/selfhost/tests/orphans.sh holds this).
  25. A command that CANNOT START has no exit — it is an ERROR: caught by
  26. `on server.Error(e)` (this instance) or the global `on Error(e)`; uncaught
  27. it is a located crash naming the binary and the errno. A finished child
  28. retires its event source, so a program whose children are done simply ends.
  29. THE PROGRAM PATH follows the Hybriel path rule: `spawn('bin/llama-server …')`
  30. resolves bin/ against THIS SCRIPT's directory (like imports and hl:fs) — a
  31. copied build folder next to your .hl file just works. A bare name searches
  32. PATH; absolute stays absolute.
  33. OPTIONS, the last argument of spawn and spawnArgs, all optional: `cwd`
  34. is the directory the child starts in — a relative one resolves against
  35. THIS SCRIPT's directory, like the program path; `env` is added to the
  36. environment the child inherits (`{ LANG = 'C' DEBUG = null }` sets LANG
  37. and removes DEBUG); `binary = true` delivers the output as Bytes (above);
  38. `stdin = 'pipe'` makes the child's stdin writable with write() and end()
  39. — without it the child inherits this program's stdin, as before.
  40. let st = spawn('git', [ 'status', '--short' ], { cwd = 'repo' })
  41. There is NO SHELL — no quoting, no expansion (the safety property, not a
  42. gap). `spawnArgs([ 'sh', '-c', pipeline ])` is how you ASK for one,
  43. explicitly. *\
  44. /* Gate file (docs campaign): __native lives only here; the Process class
  45. (process.hl) carries the instance events and their declarations. */
  46. \* Start a child from ONE command string: split on WHITESPACE RUNS (spaces,
  47. tabs AND newlines — a command written across lines is one command); a
  48. quoted argument is GROUPED and its quotes stripped ('{"a": 1}' arrives as
  49. one argv entry). A command that cannot start is an Error — caught by
  50. `on server.Error(e)` or the global `on Error(e)`. *\
  51. spawn(
  52. String cmd \\ the program and its arguments, whitespace-separated — or, with `args`, the program alone
  53. Hybrid args = null \\ the arguments as a list, each passed as is (cmd is then not split)
  54. Hybrid options = null \\ { cwd, env, stdin, binary } — see the head of this file
  55. ) {
  56. if (args == null) {
  57. return new Process().start(cmd, options)
  58. }
  59. return new Process().startArgs([cmd] + args, options)
  60. }
  61. \* The LIST form — argv exactly as given: for arguments that contain spaces,
  62. or to ask for a shell explicitly: spawnArgs([ 'sh', '-c', pipeline ]). *\
  63. spawnArgs(
  64. Hybrid args \\ argv entries, one string each — args[0] is the program
  65. Hybrid options = null \\ { cwd, env, stdin, binary }, as for spawn
  66. ) {
  67. return new Process().startArgs(args, options)
  68. }
  69. \* RUN A PROGRAM AND WAIT FOR IT (ticket #80): the blocking twin of spawn,
  70. for code that needs the output NOW — a page's root that is being
  71. constructed on the server, a build step, a script. It answers once the
  72. program has exited:
  73. let log = run([ 'git', 'log', '--oneline', '-5' ], { cwd = 'repo' })
  74. log.exit \\ the exit status (128 + n when signal n ended it)
  75. log.lines \\ stdout, one String per line, no newlines
  76. log.stderr \\ stderr, the same
  77. `command` is a String (split like spawn's) or an argv list, passed as is.
  78. `options` are spawn's `cwd`, `env` and `binary` — with `binary = true`
  79. stdout comes back whole as `data`, a Bytes, instead of `lines` — and
  80. `timeout`, in seconds: past it the program is killed and the call is an
  81. Error. `stdin`, a String or a Bytes (ticket #91), is what the program
  82. reads: written to it whole, then closed, so it reads EOF —
  83. let r = run([ 'git', 'upload-pack', '--stateless-rpc', dir ], { stdin = req.bytes binary = true })
  84. Without `stdin` the program's stdin is empty (/dev/null); a program that
  85. waited for input would otherwise hold the caller forever.
  86. The wait holds the fiber that called, as fetch() does, so a server keeps
  87. a run short or bounds it with `timeout`. A command that cannot start is an
  88. Error, as for spawn; a program that ran and failed is not — read `exit`. *\
  89. run(
  90. command \\ the program and its arguments: a String, whitespace-separated, or a list
  91. Hybrid options = null \\ { cwd, env, binary, stdin, timeout } — see above
  92. ) {
  93. // stdin crosses the plugin boundary as hex (the ABI has no Bytes), beside the options
  94. let o = options
  95. let feed = null
  96. if (o != null && o.stdin != null) {
  97. let t = hlTypeName(o.stdin)
  98. if (t == 'Bytes') {
  99. feed = o.stdin.hex()
  100. o.stdin = null
  101. } else if (t == 'String') {
  102. feed = toBytes(o.stdin).hex()
  103. o.stdin = null
  104. }
  105. }
  106. let r = __native("proc.run", command, o, feed)
  107. // a failure an `on Error` handler absorbed leaves the call yielding null
  108. if (r == null) {
  109. return null
  110. }
  111. if (options != null && options.binary == true) {
  112. return { exit = r.exit data = toBytes(r.out) stderr = linesOf(r.err) }
  113. }
  114. return { exit = r.exit lines = linesOf(r.out) stderr = linesOf(r.err) }
  115. }
  116. // A stream's text as its lines: split at each newline, and a final newline ends
  117. // the last line rather than starting an empty one — spawn's `line` events say the same.
  118. linesOf(String text) {
  119. let nl = "
  120. "
  121. let out = []
  122. if (text == '') {
  123. return out
  124. }
  125. for (l of text.split(nl)) {
  126. out.push(l)
  127. }
  128. if (text.endsWith(nl)) {
  129. out.pop()
  130. }
  131. return out
  132. }
  133. \* The terminal's own input as instance events — `input = stdin()`, then
  134. `on input.line(text)` per typed line and `on input.eof()` at Ctrl-D. A joined
  135. terminal realm (SPEC "Joining a realm") reads its keyboard here. *\
  136. stdin() {
  137. return new Stdin().start()
  138. }
  139. \* One environment variable, null when unset. The runtime loads a `.env`
  140. beside the project's entry BEFORE anything runs (KEY=VALUE lines, #
  141. comments; the real environment always wins), so env('OPENROUTER_KEY')
  142. reads either source through one call. *\
  143. env(
  144. String name \\ the variable's name
  145. ) {
  146. return __native("proc.env", name)
  147. }
  148. \* Set one variable of this process's environment; null removes it. The
  149. programs it starts from then on inherit it; a runtime setting read while
  150. the program runs (HL_GC_PACING) takes it at once. *\
  151. setEnv(
  152. String name \\ the variable's name
  153. value \\ its new value, a String, or null
  154. ) {
  155. return __native("proc.set_env", name, value)
  156. }
  157. \* THE PROGRAM'S OWN ARGUMENTS (mission 317) — the strings that follow the
  158. entry on the command line, in order, as a list; an empty list when there
  159. were none.
  160. hybriel lex.hl a b // args() is ['a', 'b']
  161. ./lex a b // the compiled binary answers the same
  162. EVERYTHING AFTER THE ENTRY IS THE PROGRAM'S, unchanged: `hybriel
  163. [runtime flags] <file> [app args]` (creator ruling 2026-09-27). Flags
  164. included — `hybriel t.hl x --post --strict --` gives
  165. ['x', '--post', '--strict', '--']; hybriel's own flags (--strict,
  166. --offline) are written BEFORE the entry.
  167. A project DIRECTORY entry is the same: every argument after it is the
  168. program's. Joining a realm is a runtime flag, written before the entry:
  169. `hybriel --realm=<name> <projectDir>` (SPEC "Joining a realm").
  170. Built here rather than handed over whole because a `__native` call answers
  171. with ONE value and a list is what the caller wants: the count and the i-th
  172. argument are the two questions the runtime can answer, and the list is the
  173. language's own. *\
  174. args() {
  175. let out = []
  176. let n = __native("proc.argc")
  177. let i = 0
  178. while (i < n) {
  179. out.push(__native("proc.arg", i))
  180. i = i + 1
  181. }
  182. return out
  183. }
  184. \* WRITE TO THIS PROGRAM'S OWN STDOUT, RAW (ruling on ticket #42): a String's
  185. bytes or a Bytes, exactly as they are — no newline, no text form, unlike
  186. console.log. For a binary protocol on stdout. Answers how many bytes went. *\
  187. writeStdout(
  188. data \\ a String or a Bytes
  189. ) {
  190. if (hlTypeName(data) == 'Bytes') {
  191. return __native("proc.write_stdout_hex", data.hex())
  192. }
  193. return __native("proc.write_stdout", data)
  194. }
  195. \* WRITE TO THIS PROGRAM'S OWN STDERR, RAW: writeStdout's twin on the error
  196. stream — a String's bytes or a Bytes, exactly as they are, no newline.
  197. Answers how many bytes went. *\
  198. writeStderr(
  199. data \\ a String or a Bytes
  200. ) {
  201. if (hlTypeName(data) == 'Bytes') {
  202. return __native("proc.write_stderr_hex", data.hex())
  203. }
  204. return __native("proc.write_stderr", data)
  205. }
  206. \* END THIS PROGRAM NOW with an exit status (ruling on ticket #36): 0 is
  207. success, anything up to 255 tells the shell it failed. Nothing after the
  208. call runs. An uncaught error still exits 1 by itself. *\
  209. exit(
  210. Number code = 0 \\ the exit status, 0-255
  211. ) {
  212. __native("proc.exit", code)
  213. }
  214. \* BECOME ANOTHER PROGRAM (mission 326 rung 12): this process is replaced by
  215. `argv` — its stdin, stdout and stderr are the program's, its exit status
  216. is the run's, and nothing after the call runs. `options.cwd` is where it
  217. starts; `options.unlink = true` removes argv[0]'s file once it is open
  218. (a scratch binary leaves nothing behind), and with it `options.argv0` is
  219. the name the program then sees in place of that path. Answers only when
  220. the program cannot start: an Error saying why. *\
  221. exec(
  222. Hybrid argv \\ argv[0] is the program (a path, or a name on PATH), the rest its arguments
  223. Hybrid options = null \\ { cwd, unlink, argv0 }
  224. ) {
  225. return __native("proc.exec", argv, options)
  226. }
  227. \* THE PROGRAM'S OWN DIRECTORY, absolute (ticket #37): the directory of the
  228. entry script on the interpreter, the directory the executable sits in for
  229. a compiled binary (a binary knows no build directory). cwd() is where the
  230. user launched from; file() is the calling file's path. *\
  231. dir() {
  232. return __native("proc.dir")
  233. }
  234. \* The process's current working directory, absolute — where the USER
  235. launched from. Your own files resolve script-relatively everywhere
  236. (imports, hl:fs, spawn); cwd() is for tools whose subject is the caller's
  237. directory: a formatter, a scaffolder. *\
  238. cwd() {
  239. return __native("proc.cwd")
  240. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre