gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4a2d71254a2d7125initial commitmre4a2d7125/plugins/proc/server.hl

9.4 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 &' ]).
  21. A command that CANNOT START has no exit — it is an ERROR: caught by
  22. `on server.Error(e)` (this instance) or the global `on Error(e)`; uncaught
  23. it is a located crash naming the binary and the errno. A finished child
  24. retires its event source, so a program whose children are done simply ends.
  25. THE PROGRAM PATH follows the Hybriel path rule: `spawn('bin/llama-server …')`
  26. resolves bin/ against THIS SCRIPT's directory (like imports and hl:fs) — a
  27. copied build folder next to your .hl file just works. A bare name searches
  28. PATH; absolute stays absolute.
  29. OPTIONS, the last argument of spawn and spawnArgs, all optional: `cwd`
  30. is the directory the child starts in — a relative one resolves against
  31. THIS SCRIPT's directory, like the program path; `env` is added to the
  32. environment the child inherits (`{ LANG = 'C' DEBUG = null }` sets LANG
  33. and removes DEBUG); `binary = true` delivers the output as Bytes (above);
  34. `stdin = 'pipe'` makes the child's stdin writable with write() and end()
  35. — without it the child inherits this program's stdin, as before.
  36. let st = spawn('git', [ 'status', '--short' ], { cwd = 'repo' })
  37. There is NO SHELL — no quoting, no expansion (the safety property, not a
  38. gap). `spawnArgs([ 'sh', '-c', pipeline ])` is how you ASK for one,
  39. explicitly. *\
  40. /* Gate file (docs campaign): __native lives only here; the Process class
  41. (process.hl) carries the instance events and their declarations. */
  42. \* Start a child from ONE command string: split on WHITESPACE RUNS (spaces,
  43. tabs AND newlines — a command written across lines is one command); a
  44. quoted argument is GROUPED and its quotes stripped ('{"a": 1}' arrives as
  45. one argv entry). A command that cannot start is an Error — caught by
  46. `on server.Error(e)` or the global `on Error(e)`. *\
  47. spawn(
  48. String cmd \\ the program and its arguments, whitespace-separated — or, with `args`, the program alone
  49. Hybrid args = null \\ the arguments as a list, each passed as is (cmd is then not split)
  50. Hybrid options = null \\ { cwd, env, stdin, binary } — see the head of this file
  51. ) {
  52. if (args == null) {
  53. return new Process().start(cmd, options)
  54. }
  55. return new Process().startArgs([cmd] + args, options)
  56. }
  57. \* The LIST form — argv exactly as given: for arguments that contain spaces,
  58. or to ask for a shell explicitly: spawnArgs([ 'sh', '-c', pipeline ]). *\
  59. spawnArgs(
  60. Hybrid args \\ argv entries, one string each — args[0] is the program
  61. Hybrid options = null \\ { cwd, env, stdin, binary }, as for spawn
  62. ) {
  63. return new Process().startArgs(args, options)
  64. }
  65. \* RUN A PROGRAM AND WAIT FOR IT (ticket #80): the blocking twin of spawn,
  66. for code that needs the output NOW — a page's root that is being
  67. constructed on the server, a build step, a script. It answers once the
  68. program has exited:
  69. let log = run([ 'git', 'log', '--oneline', '-5' ], { cwd = 'repo' })
  70. log.exit \\ the exit status (128 + n when signal n ended it)
  71. log.lines \\ stdout, one String per line, no newlines
  72. log.stderr \\ stderr, the same
  73. `command` is a String (split like spawn's) or an argv list, passed as is.
  74. `options` are spawn's `cwd`, `env` and `binary` — with `binary = true`
  75. stdout comes back whole as `data`, a Bytes, instead of `lines` — and
  76. `timeout`, in seconds: past it the program is killed and the call is an
  77. Error. The program's stdin is empty (/dev/null); a program that waited
  78. for input would otherwise hold the caller forever.
  79. The wait holds the fiber that called, as fetch() does, so a server keeps
  80. a run short or bounds it with `timeout`. A command that cannot start is an
  81. Error, as for spawn; a program that ran and failed is not — read `exit`. *\
  82. run(
  83. command \\ the program and its arguments: a String, whitespace-separated, or a list
  84. Hybrid options = null \\ { cwd, env, binary, timeout } — see above
  85. ) {
  86. let r = __native("proc.run", command, options)
  87. // a failure an `on Error` handler absorbed leaves the call yielding null
  88. if (r == null) {
  89. return null
  90. }
  91. if (options != null && options.binary == true) {
  92. return { exit = r.exit data = toBytes(r.out) stderr = linesOf(r.err) }
  93. }
  94. return { exit = r.exit lines = linesOf(r.out) stderr = linesOf(r.err) }
  95. }
  96. // A stream's text as its lines: split at each newline, and a final newline ends
  97. // the last line rather than starting an empty one — spawn's `line` events say the same.
  98. linesOf(String text) {
  99. let nl = "
  100. "
  101. let out = []
  102. if (text == '') {
  103. return out
  104. }
  105. for (l of text.split(nl)) {
  106. out.push(l)
  107. }
  108. if (text.endsWith(nl)) {
  109. out.pop()
  110. }
  111. return out
  112. }
  113. \* The terminal's own input as instance events — `input = stdin()`, then
  114. `on input.line(text)` per typed line and `on input.eof()` at Ctrl-D. A joined
  115. terminal realm (SPEC "Joining a realm") reads its keyboard here. *\
  116. stdin() {
  117. return new Stdin().start()
  118. }
  119. \* One environment variable, null when unset. The runtime loads a `.env`
  120. beside the project's entry BEFORE anything runs (KEY=VALUE lines, #
  121. comments; the real environment always wins), so env('OPENROUTER_KEY')
  122. reads either source through one call. *\
  123. env(
  124. String name \\ the variable's name
  125. ) {
  126. return __native("proc.env", name)
  127. }
  128. \* THE PROGRAM'S OWN ARGUMENTS (mission 317) — the strings that follow the
  129. entry on the command line, in order, as a list; an empty list when there
  130. were none.
  131. hybriel lex.hl a b // args() is ['a', 'b']
  132. ./lex a b // the compiled binary answers the same
  133. A flag after the file is the program's too (`hybriel t.hl x --post` gives
  134. ['x', '--post']), except hybriel's own — --port=, --strict, --offline,
  135. --strict-boundaries — which stay hybriel's anywhere; after `--` every
  136. argument is the program's, verbatim.
  137. A project DIRECTORY entry is unchanged: `hybriel <projectDir> <realm>` still
  138. JOINS that realm, so the positional there is the realm's name and never an
  139. argument (SPEC "Joining a realm").
  140. Built here rather than handed over whole because a `__native` call answers
  141. with ONE value and a list is what the caller wants: the count and the i-th
  142. argument are the two questions the runtime can answer, and the list is the
  143. language's own. *\
  144. args() {
  145. let out = []
  146. let n = __native("proc.argc")
  147. let i = 0
  148. while (i < n) {
  149. out.push(__native("proc.arg", i))
  150. i = i + 1
  151. }
  152. return out
  153. }
  154. \* WRITE TO THIS PROGRAM'S OWN STDOUT, RAW (ruling on ticket #42): a String's
  155. bytes or a Bytes, exactly as they are — no newline, no text form, unlike
  156. console.log. For a binary protocol on stdout. Answers how many bytes went. *\
  157. writeStdout(
  158. data \\ a String or a Bytes
  159. ) {
  160. if (hlTypeName(data) == 'Bytes') {
  161. return __native("proc.write_stdout_hex", data.hex())
  162. }
  163. return __native("proc.write_stdout", data)
  164. }
  165. \* WRITE TO THIS PROGRAM'S OWN STDERR, RAW: writeStdout's twin on the error
  166. stream — a String's bytes or a Bytes, exactly as they are, no newline.
  167. Answers how many bytes went. *\
  168. writeStderr(
  169. data \\ a String or a Bytes
  170. ) {
  171. if (hlTypeName(data) == 'Bytes') {
  172. return __native("proc.write_stderr_hex", data.hex())
  173. }
  174. return __native("proc.write_stderr", data)
  175. }
  176. \* END THIS PROGRAM NOW with an exit status (ruling on ticket #36): 0 is
  177. success, anything up to 255 tells the shell it failed. Nothing after the
  178. call runs. An uncaught error still exits 1 by itself. *\
  179. exit(
  180. Number code = 0 \\ the exit status, 0-255
  181. ) {
  182. __native("proc.exit", code)
  183. }
  184. \* THE PROGRAM'S OWN DIRECTORY, absolute (ticket #37): the directory of the
  185. entry script on the interpreter, the directory the executable sits in for
  186. a compiled binary (a binary knows no build directory). cwd() is where the
  187. user launched from; file() is the calling file's path. *\
  188. dir() {
  189. return __native("proc.dir")
  190. }
  191. \* The process's current working directory, absolute — where the USER
  192. launched from. Your own files resolve script-relatively everywhere
  193. (imports, hl:fs, spawn); cwd() is for tools whose subject is the caller's
  194. directory: a formatter, a scaffolder. *\
  195. cwd() {
  196. return __native("proc.cwd")
  197. }

Branches

Latest commits

  • 4a2d7125initial commitmre