protocol.ts 3.0 KB

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