helpers.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Internal shell-quoting and sandbox-result classification helpers.
  3. *
  4. * @module @deepseek-ai/dsh-bash-sandbox/helpers
  5. */
  6. import type { BashRunResult } from '@deepseek-ai/dsh-bash'
  7. /**
  8. * Quote one string as a single-quoted POSIX shell word.
  9. * @param text - raw argv element to preserve through the outer shell parse.
  10. * @returns the quoted shell word.
  11. */
  12. export function shellQuote(text: string): string {
  13. return `'${text.replaceAll("'", String.raw`'\''`)}'`
  14. }
  15. /**
  16. * Classify a failed run against the selected backend's denial dialect.
  17. * @param result - settled foreground run.
  18. * @param signatures - case-insensitive denial substrings from the active wrap.
  19. * @returns whether the failed run matches that denial dialect.
  20. */
  21. export function classifyDenial(result: BashRunResult, signatures: readonly string[]): boolean {
  22. return matchesSignature(result.exitCode, result.stderr.text, signatures)
  23. }
  24. /**
  25. * Classify a failed run against the selected backend's runner-failure dialect.
  26. * @param result - settled foreground run.
  27. * @param signatures - case-insensitive runner-failure substrings from the active wrap.
  28. * @returns whether the failed run matches that runner-failure dialect.
  29. */
  30. export function classifyRunnerFailure(result: BashRunResult, signatures: readonly string[]): boolean {
  31. return matchesSignature(result.exitCode, result.stderr.text, signatures)
  32. }
  33. /**
  34. * Match a non-zero exit against case-insensitive stderr signatures.
  35. * @param exitCode - process exit code; null means signal termination.
  36. * @param stderr - collected stderr text.
  37. * @param signatures - substrings identifying the selected backend's dialect.
  38. * @returns whether this is a non-zero exit whose stderr matches a signature.
  39. */
  40. export function matchesSignature(exitCode: number | null, stderr: string, signatures: readonly string[]): boolean {
  41. if (exitCode === null || exitCode === 0) return false
  42. const lowered = stderr.toLowerCase()
  43. return signatures.some(signature => lowered.includes(signature.toLowerCase()))
  44. }