protocol.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Wire protocol between the host runtime and the worker bootstrap. Everything
  3. * crossing the message port is structured-clone-plain and versionless — both
  4. * ends ship in this package, always at the same version. The host treats
  5. * inbound traffic as HOSTILE (the worker runs model code, which can reach
  6. * `parentPort` via `import('node:worker_threads')` and forge any of these
  7. * shapes); the worker treats inbound traffic as trusted.
  8. *
  9. * @module @deepseek-ai/dsh-code-runtime-worker/src/protocol
  10. */
  11. import type { CodeLogEntry } from '@deepseek-ai/dsh-code-runtime'
  12. /** What the host hands the worker at spawn, via `workerData`. */
  13. export interface WorkerBootData {
  14. /** The type-stripped (plain JS) program body. */
  15. code: string
  16. /** Binding namespaces to materialize: the global name plus the function names (functions themselves stay host-side). */
  17. namespaces: { global: string; names: string[] }[]
  18. /** Shared byte budget for captured log text; exceeding it drops further entries after one in-band marker. */
  19. maxLogBytes: number
  20. /** Byte cap for the rendered completion value (see the value-preparation contract in bootstrap.ts). */
  21. maxValueBytes: number
  22. }
  23. /** Worker → host: one bridged binding call. */
  24. export interface CallMessage {
  25. type: 'call'
  26. /** Worker-issued correlation id; the host answers each id at most once and ignores duplicates. */
  27. id: number
  28. /** The namespace global the call targets. */
  29. global: string
  30. /** The function name within the namespace. */
  31. name: string
  32. /** The single argument, structured-clone-plain. */
  33. args: unknown
  34. }
  35. /** Worker → host: one captured log entry, streamed eagerly so output survives a mid-run termination (timeout, abort, OOM). */
  36. export interface LogMessage {
  37. type: 'log'
  38. entry: CodeLogEntry
  39. }
  40. /**
  41. * Worker → host: the program settled. `error` carries a program exception
  42. * (the only failure the bootstrap itself can report — budgets, aborts, and
  43. * substrate death are observed host-side). `value` is present only on a
  44. * clean completion that produced one (already size-capped and
  45. * clone-safe per the bootstrap's value preparation). Logs are NOT carried
  46. * here — they streamed eagerly as {@link LogMessage}s.
  47. */
  48. export interface DoneMessage {
  49. type: 'done'
  50. value?: unknown
  51. error?: { message: string }
  52. }
  53. /** Every message the worker sends. */
  54. export type WorkerToHost = CallMessage | LogMessage | DoneMessage
  55. /** Host → worker: the answer to one {@link CallMessage}. */
  56. export type ReplyMessage =
  57. | { type: 'reply'; id: number; ok: true; value: unknown }
  58. | { type: 'reply'; id: number; ok: false; message: string }
  59. /**
  60. * The in-band marker entry text announcing that log capture stopped at the
  61. * byte budget. Shared wire vocabulary: the worker's LogBuffer emits it when
  62. * ITS budget exhausts, and the host emits the identical text when its own
  63. * ledger drops an entry first (forged port traffic, stray pipe bytes) — so
  64. * a truncated run reads the same however the cap was hit.
  65. * @param maxBytes - the configured `maxLogBytes` the marker names.
  66. * @returns the marker line.
  67. */
  68. export function logTruncationMarker(maxBytes: number): string {
  69. return `[dsh-code-runtime-worker] log capture truncated at ${maxBytes} bytes`
  70. }