shipped-composition.e2e.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { readdir, readFile } from 'node:fs/promises'
  2. import { fileURLToPath } from 'node:url'
  3. import { join } from 'node:path'
  4. import { describe, expect, it } from 'vitest'
  5. import { LOADER_SMOKE_TEST_TIMEOUT_MS } from '@deepseek-ai/dsh-loader-smoke'
  6. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  7. import { COMPOSITION_REPLY_TEXT } from './fixtures/composition-echo-llm.ts'
  8. import { COMPOSITION_SETTLED_MARKER } from './fixtures/composition-settled.ts'
  9. import { runTuiPtySmoke } from './pty-harness.ts'
  10. const dshBinScript = fileURLToPath(new URL('../src/bin.ts', import.meta.url))
  11. const tsconfigPath = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  12. // An overlay over the shipped tree, so the catalog under test is the one
  13. // `base.cordis.yml` + `tui.cordis.yml` assemble; the tail only swaps the model
  14. // and redirects session artifacts.
  15. const keylessTail = fileURLToPath(new URL('./fixtures/composition-keyless-tail.cordis.yml', import.meta.url))
  16. /**
  17. * The catalog the shipped `dsh` TUI puts in front of the model, as the loop
  18. * logged it, minus the ripgrep-dependent pair below.
  19. * The absences are the composition's security decisions, not incidental gaps:
  20. * the `cordis_*` toolset executes model-written JavaScript that no sandbox row
  21. * confines, `web_fetch` chooses its own request target, and `mcp_*` servers
  22. * spawn outside `ctx.bash`. The composition Agent Note owns the rationale and
  23. * its sources.
  24. */
  25. const EXPECTED_TUI_TOOLS = [
  26. 'ask_user_question',
  27. 'bash',
  28. 'create_goal',
  29. 'edit',
  30. 'exit_plan_mode',
  31. 'get_goal',
  32. 'ralph',
  33. 'read',
  34. 'session_event_read',
  35. 'session_event_search',
  36. 'session_event_trace',
  37. 'session_search',
  38. 'session_trace',
  39. 'skill',
  40. 'str_replace_editor',
  41. 'subagent',
  42. 'subagent_fork',
  43. 'task_kill',
  44. 'task_list',
  45. 'task_output',
  46. 'todo_write',
  47. 'update_goal',
  48. 'web_search',
  49. 'workflow',
  50. 'write',
  51. ]
  52. /**
  53. * `glob` and `grep` come from `dsh-tool-fs-search`, which probes `command -v rg`
  54. * through the mounted bash executor at load and registers neither tool when
  55. * ripgrep is absent. That is a host dependency, not a composition decision, so the
  56. * pair is asserted separately — present together or absent together.
  57. */
  58. const RIPGREP_TOOLS = ['glob', 'grep']
  59. /** The assembled request header the smoke asserts on. */
  60. interface LoggedHeader {
  61. /** Assembled tool names, sorted. */
  62. names: string[]
  63. /** `bash`'s assembled parameter properties; the escalation pair is present only under a confining executor. */
  64. bashArguments: Record<string, unknown>
  65. }
  66. /**
  67. * Read the request header the loop assembled for its first request from the
  68. * session log the smoke's workspace persisted — the model-visible composition
  69. * itself, not a registry projection taken beside it.
  70. * @param cwd - the smoke's temporary workspace.
  71. * @returns the assembled catalog, system prompt, and `bash` argument shape.
  72. */
  73. async function loggedHeader(cwd: string): Promise<LoggedHeader> {
  74. const sessionsDir = join(cwd, '.sessions')
  75. const entries = await readdir(sessionsDir, { recursive: true })
  76. // A single keyless run writes one session log.
  77. const logRelPath = entries.find(name => name.endsWith('.jsonl'))
  78. if (logRelPath === undefined) throw new Error(`no session log written under ${sessionsDir}`)
  79. const lines = (await readFile(join(sessionsDir, logRelPath), 'utf8')).split('\n').filter(Boolean)
  80. for (const line of lines) {
  81. const event = JSON.parse(line) as SessionEvent
  82. if (event.type !== 'request/header') continue
  83. const tools = event.data.header.tools ?? []
  84. const bash = tools.find(schema => schema.name === 'bash')
  85. return {
  86. names: tools.map(schema => schema.name).sort(),
  87. bashArguments: (bash?.parameters as { properties?: Record<string, unknown> } | undefined)?.properties ?? {},
  88. }
  89. }
  90. throw new Error(`session log ${logRelPath} has no request/header event`)
  91. }
  92. describe('shipped dsh composition (real Loader tree in a PTY)', () => {
  93. it('assembles exactly the shipped TUI catalog', async () => {
  94. let observed: LoggedHeader | undefined
  95. const output = await runTuiPtySmoke({
  96. label: 'dsh shipped composition',
  97. tempDirPrefix: 'dsh-shipped-tui-',
  98. binScript: dshBinScript,
  99. tsconfigPath,
  100. configPath: keylessTail,
  101. env: { DEEPSEEK_API_KEY: 'keyless-composition-no-call', DSH_TELEMETRY_DISABLED: '1' },
  102. // Artifact CI builds and smokes concurrently on a contended runner.
  103. ...(process.env.DSH_EXAMPLE_MODE === 'lib' ? { timeoutMs: 60_000 } : {}),
  104. actions: [
  105. { waitFor: COMPOSITION_SETTLED_MARKER, send: 'Describe the shipped composition.\r' },
  106. { waitFor: COMPOSITION_REPLY_TEXT, send: '/exit\r' },
  107. ],
  108. inspect: async (cwd) => { observed = await loggedHeader(cwd) },
  109. })
  110. expect(output).toContain(COMPOSITION_REPLY_TEXT)
  111. expect(observed?.names.filter(name => !RIPGREP_TOOLS.includes(name))).toEqual(EXPECTED_TUI_TOOLS)
  112. expect([[], RIPGREP_TOOLS]).toContainEqual(observed?.names.filter(name => RIPGREP_TOOLS.includes(name)))
  113. // The TUI mounts the unrestricted local executors, so `tool-bash` emits no
  114. // escalation pair. Pinning its absence keeps a later sandbox change from
  115. // arriving here unannounced.
  116. expect(Object.keys(observed?.bashArguments ?? {})).not.toContain('sandbox_permissions')
  117. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  118. })