1
0

shipped-composition.e2e.ts 6.0 KB

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