loader-composition.e2e.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * REAL-composition tier: boot the examples-owned telemetry Loader fixture as
  3. * a subprocess (per testing policy, through the same app/boot path a
  4. * deployment uses), run one mocked-model turn with a real bash round trip,
  5. * and assert against what the mock OTLP collector actually received on the
  6. * wire: ledger mirroring, the deployment-mounted redact rule applied to the
  7. * exported copy, ops markers, and the untouched canonical log.
  8. */
  9. import { readFile, readdir } from 'node:fs/promises'
  10. import { join } from 'node:path'
  11. import { fileURLToPath } from 'node:url'
  12. import { describe, expect, it } from 'vitest'
  13. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  14. const driver = fileURLToPath(new URL(
  15. './fixtures/driver.ts',
  16. import.meta.url,
  17. ))
  18. const configPath = fileURLToPath(new URL(
  19. './fixtures/cordis.yml',
  20. import.meta.url,
  21. ))
  22. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  23. const FIXTURE_SECRET = 'sk-e2efixture1234567890'
  24. const FIXTURE_PLACEHOLDER = '[E2E-REDACTED]'
  25. interface OtlpLogRecord {
  26. attributes?: { key: string; value: Record<string, unknown> }[]
  27. body?: unknown
  28. }
  29. interface OtlpCapture {
  30. resourceLogs: {
  31. scopeLogs: {
  32. scope: { name: string }
  33. logRecords: OtlpLogRecord[]
  34. }[]
  35. }[]
  36. }
  37. interface FixtureOutput {
  38. captures: OtlpCapture[]
  39. logContent: string
  40. }
  41. async function jsonlFiles(dir: string): Promise<string[]> {
  42. const entries = await readdir(dir, { withFileTypes: true })
  43. const paths = await Promise.all(entries.map(async (entry) => {
  44. const path = join(dir, entry.name)
  45. if (entry.isDirectory()) return jsonlFiles(path)
  46. return entry.isFile() && entry.name.endsWith('.jsonl') ? [path] : []
  47. }))
  48. return paths.flat()
  49. }
  50. async function readFixtureOutput(cwd: string): Promise<FixtureOutput> {
  51. const captures = JSON.parse(await readFile(join(cwd, 'otlp-captures.json'), 'utf8')) as OtlpCapture[]
  52. const logs = await jsonlFiles(join(cwd, '.sessions'))
  53. expect(logs).toHaveLength(1)
  54. return { captures, logContent: await readFile(logs[0] as string, 'utf8') }
  55. }
  56. function allRecords(captures: OtlpCapture[]) {
  57. return captures.flatMap(capture => capture.resourceLogs.flatMap(resource =>
  58. resource.scopeLogs.flatMap(scoped => scoped.logRecords.map(record => ({ scope: scoped.scope.name, record })))))
  59. }
  60. function eventTypes(captures: OtlpCapture[]): string[] {
  61. return allRecords(captures).flatMap(({ record }) =>
  62. record.attributes?.flatMap(attribute =>
  63. attribute.key === 'event.type' && typeof attribute.value['stringValue'] === 'string'
  64. ? [attribute.value['stringValue']]
  65. : []) ?? [])
  66. }
  67. describe('session-telemetry-otel through a real headless cordis.yml', () => {
  68. it('exports redacted ledger records to the collector while the canonical log keeps the secret', async () => {
  69. let output!: FixtureOutput
  70. const { stderr } = await runLoaderSmoke({
  71. label: 'session-telemetry-otel loader smoke',
  72. tempDirPrefix: 'telemetry-otel-e2e-',
  73. binScript: driver,
  74. libBinScript: driver,
  75. configPath,
  76. tsconfigPath: repoTsconfig,
  77. inspect: async (cwd) => { output = await readFixtureOutput(cwd) },
  78. })
  79. expect(stderr).not.toContain('UNHANDLED')
  80. const records = allRecords(output.captures)
  81. expect(records.length).toBeGreaterThan(0)
  82. const types = eventTypes(output.captures)
  83. for (const expected of ['turn/start', 'user/message', 'tool/call', 'tool/result', 'assistant/message', 'turn/end']) {
  84. expect(types, expected).toContain(expected)
  85. }
  86. expect(records.some(({ scope }) => scope.endsWith('/ops'))).toBe(true)
  87. // The deployment-mounted rule on the wire: the fixture credential never
  88. // leaves the process, its surrounding prose does, and the placeholder
  89. // marks the spot — the seam itself ships no rules.
  90. const wire = JSON.stringify(output.captures)
  91. expect(wire).not.toContain(FIXTURE_SECRET)
  92. expect(wire).toContain(FIXTURE_PLACEHOLDER)
  93. expect(wire).toContain('prove telemetry with key')
  94. // The canonical session log is never rewritten.
  95. expect(output.logContent).toContain(FIXTURE_SECRET)
  96. expect(output.logContent).not.toContain(FIXTURE_PLACEHOLDER)
  97. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  98. it('exports only prefixes ending in feedback under feedback-only mode', async () => {
  99. let output!: FixtureOutput
  100. const { stderr } = await runLoaderSmoke({
  101. label: 'session-telemetry-otel feedback-only loader smoke',
  102. tempDirPrefix: 'telemetry-otel-feedback-e2e-',
  103. binScript: driver,
  104. libBinScript: driver,
  105. configPath,
  106. tsconfigPath: repoTsconfig,
  107. env: { DSH_TELEMETRY_E2E_MODE: 'FEEDBACK_ONLY' },
  108. inspect: async (cwd) => { output = await readFixtureOutput(cwd) },
  109. })
  110. expect(stderr).not.toContain('UNHANDLED')
  111. const wire = JSON.stringify(output.captures)
  112. expect(eventTypes(output.captures)).toContain('feedback/record')
  113. expect(wire).toContain('fixture feedback')
  114. expect(wire).toContain('prove telemetry with key')
  115. expect(wire).not.toContain('post-feedback private suffix')
  116. expect(output.logContent).toContain('post-feedback private suffix')
  117. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  118. it('keeps disabled feedback local and prints the stable warning', async () => {
  119. let output!: FixtureOutput
  120. const { stdout } = await runLoaderSmoke({
  121. label: 'session-telemetry-otel disabled loader smoke',
  122. tempDirPrefix: 'telemetry-otel-disabled-e2e-',
  123. binScript: driver,
  124. libBinScript: driver,
  125. configPath,
  126. tsconfigPath: repoTsconfig,
  127. env: { DSH_TELEMETRY_E2E_MODE: 'DISABLED' },
  128. inspect: async (cwd) => { output = await readFixtureOutput(cwd) },
  129. })
  130. expect(output.captures).toEqual([])
  131. expect(output.logContent).toContain('fixture feedback')
  132. expect(stdout.match(/session telemetry is DISABLED; nothing will be shared and this feedback remains local/)?.[0])
  133. .toMatchInlineSnapshot('"session telemetry is DISABLED; nothing will be shared and this feedback remains local"')
  134. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  135. })