loader-composition.e2e.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * REAL-composition tier: explicit feedback through the shipped headless
  3. * Loader profile with a mock model and real shell. The collector observes
  4. * only the redacted authorized prefix; the canonical log keeps every event.
  5. */
  6. import { readFile, readdir } from 'node:fs/promises'
  7. import { join } from 'node:path'
  8. import { fileURLToPath } from 'node:url'
  9. import { describe, expect, it } from 'vitest'
  10. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  11. const driver = fileURLToPath(new URL(
  12. './fixtures/driver.ts',
  13. import.meta.url,
  14. ))
  15. const configPath = fileURLToPath(new URL(
  16. './fixtures/telemetry.patch.yml',
  17. import.meta.url,
  18. ))
  19. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  20. const FIXTURE_SECRET = 'sk-e2efixture1234567890'
  21. const FIXTURE_PLACEHOLDER = '[E2E-REDACTED]'
  22. interface OtlpLogRecord {
  23. attributes?: { key: string; value: Record<string, unknown> }[]
  24. body?: unknown
  25. }
  26. interface OtlpCapture {
  27. resourceLogs: {
  28. scopeLogs: {
  29. scope: { name: string }
  30. logRecords: OtlpLogRecord[]
  31. }[]
  32. }[]
  33. }
  34. interface FixtureOutput {
  35. captures: OtlpCapture[]
  36. logContent: string
  37. }
  38. async function jsonlFiles(dir: string): Promise<string[]> {
  39. const entries = await readdir(dir, { withFileTypes: true })
  40. const paths = await Promise.all(entries.map(async (entry) => {
  41. const path = join(dir, entry.name)
  42. if (entry.isDirectory()) return jsonlFiles(path)
  43. return entry.isFile() && entry.name.endsWith('.jsonl') ? [path] : []
  44. }))
  45. return paths.flat()
  46. }
  47. async function readFixtureOutput(cwd: string): Promise<FixtureOutput> {
  48. const captures = JSON.parse(await readFile(join(cwd, 'otlp-captures.json'), 'utf8')) as OtlpCapture[]
  49. const logs = await jsonlFiles(join(cwd, '.sessions'))
  50. expect(logs).toHaveLength(1)
  51. return { captures, logContent: await readFile(logs[0] as string, 'utf8') }
  52. }
  53. function allRecords(captures: OtlpCapture[]) {
  54. return captures.flatMap(capture => capture.resourceLogs.flatMap(resource =>
  55. resource.scopeLogs.flatMap(scoped => scoped.logRecords.map(record => ({ scope: scoped.scope.name, record })))))
  56. }
  57. function eventTypes(captures: OtlpCapture[]): string[] {
  58. return allRecords(captures).flatMap(({ record }) =>
  59. record.attributes?.flatMap(attribute =>
  60. attribute.key === 'event.type' && typeof attribute.value['stringValue'] === 'string'
  61. ? [attribute.value['stringValue']]
  62. : []) ?? [])
  63. }
  64. describe('session-telemetry-otel through the production headless profile', () => {
  65. it('rejects FULL before any session can be uploaded', async () => {
  66. const { stdout, stderr } = await runLoaderSmoke({
  67. label: 'session-telemetry-otel rejected FULL loader smoke',
  68. tempDirPrefix: 'telemetry-otel-full-e2e-',
  69. binScript: driver,
  70. libBinScript: driver,
  71. configPath,
  72. tsconfigPath: repoTsconfig,
  73. env: { DSH_TELEMETRY_E2E_MODE: 'FULL' },
  74. expectedExitCode: 1,
  75. })
  76. expect(stdout + stderr).toContain('FULL')
  77. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  78. it('exports the redacted feedback-authorized prefix while the canonical log keeps the secret', async () => {
  79. let output!: FixtureOutput
  80. const { stderr } = await runLoaderSmoke({
  81. label: 'session-telemetry-otel loader smoke',
  82. tempDirPrefix: 'telemetry-otel-e2e-',
  83. binScript: driver,
  84. libBinScript: driver,
  85. configPath,
  86. tsconfigPath: repoTsconfig,
  87. inspect: async (cwd) => { output = await readFixtureOutput(cwd) },
  88. })
  89. expect(stderr).not.toContain('UNHANDLED')
  90. const records = allRecords(output.captures)
  91. expect(records.length).toBeGreaterThan(0)
  92. const types = eventTypes(output.captures)
  93. for (const expected of ['turn/start', 'user/message', 'tool/call', 'tool/result', 'assistant/message', 'turn/end']) {
  94. expect(types, expected).toContain(expected)
  95. }
  96. const canonicalEvents = output.logContent.trim().split('\n')
  97. .map(line => JSON.parse(line) as { type: string; seq?: number })
  98. .filter(event => event.seq !== undefined)
  99. const feedbackIndex = canonicalEvents.findIndex(event => event.type === 'feedback/record')
  100. expect(feedbackIndex).toBeGreaterThanOrEqual(0)
  101. expect(types).toEqual(canonicalEvents.slice(0, feedbackIndex + 1).map(event => event.type))
  102. expect(types.at(-1)).toBe('feedback/record')
  103. expect(records.some(({ scope }) => scope.endsWith('/ops'))).toBe(false)
  104. const wire = JSON.stringify(output.captures)
  105. expect(wire).not.toContain(FIXTURE_SECRET)
  106. expect(wire).toContain(FIXTURE_PLACEHOLDER)
  107. expect(wire).toContain('prove telemetry with key')
  108. expect(wire).toContain('fixture feedback')
  109. expect(wire).not.toContain('post-feedback private suffix')
  110. expect(wire).not.toContain('private operational error')
  111. expect(wire).not.toContain('telemetry.op')
  112. expect(output.logContent).toContain('post-feedback private suffix')
  113. expect(output.logContent).toContain(FIXTURE_SECRET)
  114. expect(output.logContent).not.toContain(FIXTURE_PLACEHOLDER)
  115. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  116. it('never captures ordinary turns or shutdown without new feedback', async () => {
  117. let output!: FixtureOutput
  118. const { stderr } = await runLoaderSmoke({
  119. label: 'session-telemetry-otel no-feedback loader smoke',
  120. tempDirPrefix: 'telemetry-otel-no-feedback-e2e-',
  121. binScript: driver,
  122. libBinScript: driver,
  123. configPath,
  124. tsconfigPath: repoTsconfig,
  125. env: { DSH_TELEMETRY_E2E_FEEDBACK: 'none' },
  126. inspect: async (cwd) => { output = await readFixtureOutput(cwd) },
  127. })
  128. expect(stderr).not.toContain('UNHANDLED')
  129. expect(output.captures).toEqual([])
  130. expect(output.logContent).toContain('prove telemetry with key')
  131. expect(output.logContent).toContain('post-feedback private suffix')
  132. expect(output.logContent).not.toContain('feedback/record')
  133. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  134. it('keeps disabled feedback local and prints the stable warning', async () => {
  135. let output!: FixtureOutput
  136. const { stdout } = await runLoaderSmoke({
  137. label: 'session-telemetry-otel disabled loader smoke',
  138. tempDirPrefix: 'telemetry-otel-disabled-e2e-',
  139. binScript: driver,
  140. libBinScript: driver,
  141. configPath,
  142. tsconfigPath: repoTsconfig,
  143. env: { DSH_TELEMETRY_E2E_MODE: 'DISABLED' },
  144. inspect: async (cwd) => { output = await readFixtureOutput(cwd) },
  145. })
  146. expect(output.captures).toEqual([])
  147. expect(output.logContent).toContain('fixture feedback')
  148. expect(stdout.match(/OpenTelemetry session upload is DISABLED; this feedback is not uploaded through OpenTelemetry/)?.[0])
  149. .toMatchInlineSnapshot('"OpenTelemetry session upload is DISABLED; this feedback is not uploaded through OpenTelemetry"')
  150. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  151. })