load-path.spec.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Real-load-path guard for @deepseek-ai/dsh-tool-fs-search. `tool-fs-search` is
  3. * a NAMESPACE plugin with `inject` — so a stray `export default apply` would
  4. * make the cordis Loader's `unwrapExports` (`exports.default ?? exports`)
  5. * collapse the module to the bare `apply` function, DROPPING `inject`. The
  6. * plugin would then read `ctx.bash` without having injected it and throw
  7. * `cannot get property … without inject` the moment it loads (postmortem 0001).
  8. *
  9. * A hand-built `ctx.plugin({ apply, inject })` mount CANNOT catch that — it
  10. * bypasses `unwrapExports`. So this test unwraps the module through the REAL
  11. * `Loader.prototype.unwrapExports` and mounts the result over a bash executor,
  12. * exercising the exact path the Loader uses. Prove the guard bites: add
  13. * `export default apply` to `src/index.ts`, watch this go red, revert.
  14. */
  15. import { describe, expect, it } from 'vitest'
  16. import { Context } from 'cordis'
  17. import Loader from '@cordisjs/plugin-loader'
  18. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  19. import ToolRegistry from '@deepseek-ai/dsh-tools'
  20. import { BashExecutor } from '@deepseek-ai/dsh-bash'
  21. import type { BashExecRequest, BashExecSpec, BashProcess, BashRunResult } from '@deepseek-ai/dsh-bash'
  22. import * as toolFsSearch from '@deepseek-ai/dsh-tool-fs-search'
  23. const RG_PROBE_COMMAND = 'command -v rg >/dev/null 2>&1'
  24. /**
  25. * Deterministic bash service for this Loader guard: the test wants to exercise
  26. * the real unwrap/inject path, not depend on whether the host image has rg.
  27. */
  28. class ProbeSuccessBashExecutor extends BashExecutor {
  29. override resolve(request: BashExecRequest): BashExecSpec {
  30. return {
  31. command: request.command,
  32. workdir: request.workdir ?? '/work',
  33. timeoutMs: request.timeoutMs ?? 60_000,
  34. stdoutMaxBytes: request.stdoutMaxBytes ?? 64_000,
  35. signal: request.signal,
  36. sandboxPolicy: request.sandboxPolicy,
  37. }
  38. }
  39. override run(spec: BashExecSpec): Promise<BashRunResult> {
  40. if (spec.command !== RG_PROBE_COMMAND) {
  41. throw new Error(`unexpected command in load-path guard: ${spec.command}`)
  42. }
  43. return Promise.resolve({
  44. exitCode: 0,
  45. signal: null,
  46. timedOut: false,
  47. aborted: false,
  48. timeoutMs: spec.timeoutMs,
  49. stdout: { text: '', truncated: false },
  50. stderr: { text: '', truncated: false },
  51. })
  52. }
  53. override start(): BashProcess {
  54. throw new Error('load-path guard must not start background processes')
  55. }
  56. }
  57. describe('dsh-tool-fs-search real-load-path guard', () => {
  58. it('has no default export and keeps name/inject/Config through unwrapExports', () => {
  59. expect('default' in toolFsSearch).toBe(false)
  60. const loader = Object.create(Loader.prototype) as Loader
  61. const unwrapped = loader.unwrapExports(toolFsSearch) as Record<string, unknown>
  62. expect(unwrapped).toBe(toolFsSearch)
  63. expect(unwrapped.name).toBe('tool-fs-search')
  64. expect(unwrapped.inject).toEqual(['tools', 'systemPrompt', 'bash'])
  65. expect(typeof unwrapped.Config).toBe('function')
  66. expect(typeof unwrapped.apply).toBe('function')
  67. })
  68. it('boots over ctx.bash through the unwrapped module without an inject error', async () => {
  69. const ctx = new Context()
  70. await ctx.plugin(SystemPrompt)
  71. await ctx.plugin(ToolRegistry)
  72. await ctx.plugin(ProbeSuccessBashExecutor)
  73. const loader = Object.create(Loader.prototype) as Loader
  74. const unwrapped = loader.unwrapExports(toolFsSearch) as Parameters<Context['plugin']>[0]
  75. // A collapsed export shape (dropped inject) would throw "without inject" here.
  76. const fiber = await ctx.plugin(unwrapped)
  77. expect(ctx.tools.schemas().map(s => s.name)).toEqual(expect.arrayContaining(['glob', 'grep']))
  78. await fiber.dispose()
  79. })
  80. })