shipped-composition.e2e.ts 5.4 KB

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