bootstrap.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * Worker-side execution logic, written as plain functions over an injected port so the unit
  3. * suite can run every line IN-PROCESS against a fake port (a real worker thread is a separate
  4. * V8 isolate the coverage provider cannot observe).
  5. * @module @deepseek-ai/dsh-code-runtime-worker/src/bootstrap
  6. */
  7. import { inspect } from 'node:util'
  8. import { serialize } from 'node:v8'
  9. import type { CodeLogEntry } from '@deepseek-ai/dsh-code-runtime'
  10. import { logTruncationMarker } from './protocol.ts'
  11. import type { DoneMessage, ReplyMessage, WorkerBootData, WorkerToHost } from './protocol.ts'
  12. /** The port surface the bootstrap needs — satisfied by `parentPort` and by the tests' fake. */
  13. export interface BootstrapPort {
  14. postMessage(message: WorkerToHost): void
  15. on(event: 'message', listener: (message: ReplyMessage) => void): void
  16. }
  17. /**
  18. * A writable stream's `write` slot, as the bootstrap patches it (see
  19. * {@link captureStreamWrites}). Method-typed so the real
  20. * `process.stdout`/`process.stderr` (narrower chunk parameters) remain
  21. * assignable.
  22. */
  23. export interface PatchableStream {
  24. write(chunk: unknown, ...rest: unknown[]): boolean
  25. }
  26. /**
  27. * Ordered log capture under one shared byte budget, delivered to a sink as
  28. * each entry lands (the real sink streams entries over the port eagerly, so
  29. * captured output survives a mid-run termination). Once the budget is
  30. * exhausted it emits exactly one in-band marker entry (on the `stderr`
  31. * diagnostics channel) and silently drops everything after — the cap is a
  32. * blast-radius bound, so "how much was lost" intentionally stays unmeasured.
  33. */
  34. export class LogBuffer {
  35. private remaining: number
  36. private truncated = false
  37. // Explicit fields, not constructor parameter properties: this module loads
  38. // under Node's native strip-only mode, which rejects non-erasable syntax —
  39. // and parameter properties are non-erasable.
  40. private readonly maxBytes: number
  41. private readonly sink: (entry: CodeLogEntry) => void
  42. constructor(maxBytes: number, sink: (entry: CodeLogEntry) => void) {
  43. this.maxBytes = maxBytes
  44. this.sink = sink
  45. this.remaining = maxBytes
  46. }
  47. /**
  48. * Emit one entry to the sink, charging its text against the budget (drops + marks once exhausted).
  49. * @param entry - the log entry to deliver.
  50. */
  51. push(entry: CodeLogEntry): void {
  52. if (this.truncated) return
  53. const cost = Buffer.byteLength(entry.text, 'utf8')
  54. if (cost > this.remaining) {
  55. this.truncated = true
  56. this.sink({ source: 'stderr', text: logTruncationMarker(this.maxBytes) })
  57. return
  58. }
  59. this.remaining -= cost
  60. this.sink(entry)
  61. }
  62. }
  63. /** The five console methods the shim captures, in the seam's level vocabulary. */
  64. const CONSOLE_LEVELS = ['log', 'info', 'warn', 'error', 'debug'] as const
  65. /**
  66. * A `console` replacement whose five leveled methods render their arguments
  67. * `util.inspect`-style (matching real console formatting closely enough for
  68. * a model to recognize its own output) into the buffer. Only these five
  69. * exist — the program gets a deliberately small console, not Node's full
  70. * surface.
  71. * @param logs - the buffer every rendered line is pushed into.
  72. * @returns the five-method console object handed to the program.
  73. */
  74. export function makeConsoleShim(logs: LogBuffer): Record<(typeof CONSOLE_LEVELS)[number], (...args: unknown[]) => void> {
  75. const render = (args: unknown[]): string =>
  76. args.map(arg => typeof arg === 'string' ? arg : inspect(arg, INSPECT_OPTIONS)).join(' ')
  77. const shim = Object.create(null) as Record<(typeof CONSOLE_LEVELS)[number], (...args: unknown[]) => void>
  78. for (const level of CONSOLE_LEVELS) {
  79. shim[level] = (...args: unknown[]) => { logs.push({ source: 'console', level, text: render(args) }) }
  80. }
  81. return shim
  82. }
  83. /**
  84. * Redirect a stream's `write` into the log buffer (the program-visible
  85. * `process.stdout`/`process.stderr` in the real worker), so raw writes land in emission order
  86. * alongside console output instead of racing down a pipe. It preserves Node's optional callback
  87. * contract: the callback runs asynchronously after admission, even when the log budget drops
  88. * the write.
  89. *
  90. * @param logs - the buffer captured writes are pushed into.
  91. * @param stream - the stream whose `write` slot is patched.
  92. * @param source - the log source the captured writes are attributed to.
  93. * @returns the restore function (the in-process tests un-patch; the real
  94. * worker never needs to).
  95. */
  96. export function captureStreamWrites(logs: LogBuffer, stream: PatchableStream, source: 'stdout' | 'stderr'): () => void {
  97. // The slot's VALUE is stored for restore and reassigned — never invoked
  98. // detached, so the unbound-method concern does not apply.
  99. // eslint-disable-next-line @typescript-eslint/unbound-method
  100. const original = stream.write
  101. stream.write = (chunk: unknown, ...rest: unknown[]): boolean => {
  102. logs.push({ source, text: typeof chunk === 'string' ? chunk : String(chunk) })
  103. // Node's optional-encoding shape: the callback is whichever of the next
  104. // two positions holds a function (a non-function there is the encoding).
  105. const callback = [rest[0], rest[1]].find(
  106. (arg): arg is (error?: Error | null) => void => typeof arg === 'function',
  107. )
  108. if (callback) queueMicrotask(() => { callback(null) })
  109. return true
  110. }
  111. return () => { stream.write = original }
  112. }
  113. /** Bounded inspect options: deep enough to be useful, bounded so a pathological value cannot explode the rendering. */
  114. const INSPECT_OPTIONS = { depth: 4, maxArrayLength: 100, maxStringLength: 10_000 } as const
  115. /**
  116. * The longest prefix of `text` whose UTF-8 encoding fits `maxBytes`, cut at
  117. * a code-point boundary (never mid-surrogate-pair). The byte caps are BYTE
  118. * caps — `String.prototype.slice` counts UTF-16 code units, up to 3× smaller
  119. * than what a multibyte string actually costs across the boundary.
  120. * @param text - the string to bound.
  121. * @param maxBytes - the UTF-8 byte budget the prefix must fit.
  122. * @returns the prefix (all of `text` when it already fits).
  123. */
  124. export function truncateUtf8Bytes(text: string, maxBytes: number): string {
  125. if (Buffer.byteLength(text, 'utf8') <= maxBytes) return text
  126. let bytes = 0
  127. let end = 0
  128. for (const char of text) {
  129. const cost = Buffer.byteLength(char, 'utf8')
  130. if (bytes + cost > maxBytes) break
  131. bytes += cost
  132. end += char.length
  133. }
  134. return text.slice(0, end)
  135. }
  136. /**
  137. * Prepare the program's completion value for the done message: a value whose MEASURED
  138. * cross-boundary size fits `maxValueBytes` crosses raw — exact bytes for a string, the
  139. * structured-clone wire size (`v8.serialize`) for everything else, so a huge container whose
  140. * bounded inspect rendering happens to be small cannot smuggle itself past the cap. Oversized
  141. * or non-cloneable values are replaced by a bounded string rendering with an in-band marker.
  142. *
  143. * @param value - the program's completion value.
  144. * @param maxValueBytes - the byte cap for the value.
  145. * @returns the done-message fragment: `{}` for `undefined`, else `{ value }`.
  146. */
  147. export function prepareValue(value: unknown, maxValueBytes: number): { value?: unknown } {
  148. if (value === undefined) return {}
  149. if (typeof value === 'string') {
  150. if (Buffer.byteLength(value, 'utf8') <= maxValueBytes) return { value }
  151. } else {
  152. let size: number | undefined
  153. try {
  154. size = serialize(value).byteLength
  155. } catch {
  156. // Only the verdict matters: the value has parts the structured-clone
  157. // algorithm rejects (functions, classes, …) and must cross as its
  158. // rendering instead.
  159. size = undefined
  160. }
  161. if (size !== undefined && size <= maxValueBytes) return { value }
  162. }
  163. const rendered = typeof value === 'string' ? value : inspect(value, INSPECT_OPTIONS)
  164. const capped = Buffer.byteLength(rendered, 'utf8') > maxValueBytes
  165. ? `${truncateUtf8Bytes(rendered, maxValueBytes)}… [truncated]`
  166. : rendered
  167. return { value: capped }
  168. }
  169. /** One awaited binding call's settlement handles, keyed by call id in the pending map. */
  170. export interface PendingCall {
  171. resolve(value: unknown): void
  172. reject(error: Error): void
  173. }
  174. /**
  175. * Route host replies into the pending-call map: each reply settles its call
  176. * at most once, and a reply for an unknown id (stray, or a duplicate answer
  177. * to an id already settled) is ignored. Shared wiring between
  178. * {@link runWorkerMain} and the tests that exercise {@link makeNamespaces}
  179. * standalone.
  180. * @param port - the port whose `message` events carry the replies.
  181. * @param pending - the id-keyed map of unsettled binding calls.
  182. */
  183. export function wireReplies(port: BootstrapPort, pending: Map<number, PendingCall>): void {
  184. port.on('message', (message: ReplyMessage) => {
  185. const entry = pending.get(message.id)
  186. if (!entry) return
  187. pending.delete(message.id)
  188. if (message.ok) entry.resolve(message.value)
  189. else entry.reject(new Error(message.message))
  190. })
  191. }
  192. /**
  193. * Build the binding namespace objects the program sees: one null-prototype global per
  194. * namespace, each declared name an own enumerable async function that bridges over the port
  195. * (`__proto__`/`constructor`/`toString` are ordinary keys, never prototype collisions).
  196. * Non-cloneable arguments and host failure replies reject only the corresponding call.
  197. *
  198. * @param data - the boot payload's namespace declarations (globals + names).
  199. * @param port - the port binding calls are posted to.
  200. * @param pending - the id-keyed map each posted call parks its handles in.
  201. * @param nextId - the shared mutable id counter (worker-issued correlation ids).
  202. * @returns one namespace object per declaration, in declaration order.
  203. */
  204. export function makeNamespaces(
  205. data: Pick<WorkerBootData, 'namespaces'>,
  206. port: BootstrapPort,
  207. pending: Map<number, PendingCall>,
  208. nextId: { value: number },
  209. ): Record<string, unknown>[] {
  210. return data.namespaces.map(({ global, names }) => {
  211. const namespace = Object.create(null) as Record<string, unknown>
  212. for (const name of names) {
  213. Object.defineProperty(namespace, name, {
  214. enumerable: true,
  215. value: (args: unknown): Promise<unknown> => new Promise((resolve, reject) => {
  216. const id = nextId.value++
  217. pending.set(id, { resolve, reject })
  218. try {
  219. port.postMessage({ type: 'call', id, global, name, args })
  220. } catch (error: unknown) {
  221. pending.delete(id)
  222. reject(new Error(`binding arguments must be structured-cloneable: ${error instanceof Error ? error.message : String(error)}`))
  223. }
  224. }),
  225. })
  226. }
  227. return namespace
  228. })
  229. }
  230. /**
  231. * Run one strict async-function body, allowing top-level `await` and `return`, and post exactly
  232. * one terminal {@link DoneMessage}; a thrown program error becomes its `error` field.
  233. * @param port - host message port or test double.
  234. * @param data - the boot payload the host sent.
  235. * @param streams - stdout/stderr objects captured as program logs.
  236. * @returns after posting the done message.
  237. */
  238. export async function runWorkerMain(
  239. port: BootstrapPort,
  240. data: WorkerBootData,
  241. streams: { stdout: PatchableStream; stderr: PatchableStream },
  242. ): Promise<void> {
  243. const logs = new LogBuffer(data.maxLogBytes, (entry) => { port.postMessage({ type: 'log', entry }) })
  244. captureStreamWrites(logs, streams.stdout, 'stdout')
  245. captureStreamWrites(logs, streams.stderr, 'stderr')
  246. const pending = new Map<number, PendingCall>()
  247. wireReplies(port, pending)
  248. const nextId = { value: 1 }
  249. const namespaces = makeNamespaces(data, port, pending, nextId)
  250. const consoleShim = makeConsoleShim(logs)
  251. let done: DoneMessage
  252. try {
  253. // The async function constructor, reached through an instance because
  254. // `AsyncFunction` is not a global. The program body is strict-mode.
  255. /* v8 ignore next -- the arrow exists only to reach the AsyncFunction constructor; it is never invoked. */
  256. const AsyncFunction = (async () => {}).constructor as new (...args: string[]) => (...fnArgs: unknown[]) => Promise<unknown>
  257. const fn = new AsyncFunction(...data.namespaces.map(namespace => namespace.global), 'console', `'use strict';\n${data.code}`)
  258. const value = await fn(...namespaces, consoleShim)
  259. done = { type: 'done', ...prepareValue(value, data.maxValueBytes) }
  260. } catch (error: unknown) {
  261. const message = error instanceof Error ? error.stack ?? error.message : String(error)
  262. done = { type: 'done', error: { message } }
  263. }
  264. port.postMessage(done)
  265. }