loader-composition.e2e.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. '../../../../examples/headless-agent/tests/fixtures/telemetry-otel-driver.ts',
  16. import.meta.url,
  17. ))
  18. const configPath = fileURLToPath(new URL(
  19. '../../../../examples/headless-agent/tests/fixtures/telemetry-otel.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. async function jsonlFiles(dir: string): Promise<string[]> {
  38. const entries = await readdir(dir, { withFileTypes: true })
  39. const paths = await Promise.all(entries.map(async (entry) => {
  40. const path = join(dir, entry.name)
  41. if (entry.isDirectory()) return jsonlFiles(path)
  42. return entry.isFile() && entry.name.endsWith('.jsonl') ? [path] : []
  43. }))
  44. return paths.flat()
  45. }
  46. describe('session-telemetry-otel through a real headless cordis.yml', () => {
  47. it('exports redacted ledger records to the collector while the canonical log keeps the secret', async () => {
  48. let captures: OtlpCapture[] = []
  49. let logContent = ''
  50. const { stderr } = await runLoaderSmoke({
  51. label: 'session-telemetry-otel loader smoke',
  52. tempDirPrefix: 'telemetry-otel-e2e-',
  53. binScript: driver,
  54. libBinScript: driver,
  55. configPath,
  56. tsconfigPath: repoTsconfig,
  57. inspect: async (cwd) => {
  58. captures = JSON.parse(await readFile(join(cwd, 'otlp-captures.json'), 'utf8')) as OtlpCapture[]
  59. const logs = await jsonlFiles(join(cwd, '.sessions'))
  60. expect(logs).toHaveLength(1)
  61. logContent = await readFile(logs[0] as string, 'utf8')
  62. },
  63. })
  64. expect(stderr).not.toContain('UNHANDLED')
  65. const records = captures.flatMap(capture => capture.resourceLogs.flatMap(resource =>
  66. resource.scopeLogs.flatMap(scoped => scoped.logRecords.map(record => ({ scope: scoped.scope.name, record })))))
  67. expect(records.length).toBeGreaterThan(0)
  68. const eventTypes = records.flatMap(({ record }) =>
  69. record.attributes?.flatMap(attribute =>
  70. attribute.key === 'event.type' && typeof attribute.value['stringValue'] === 'string'
  71. ? [attribute.value['stringValue']]
  72. : []) ?? [])
  73. for (const expected of ['turn/start', 'user/message', 'tool/call', 'tool/result', 'assistant/message', 'turn/end']) {
  74. expect(eventTypes, expected).toContain(expected)
  75. }
  76. expect(records.some(({ scope }) => scope.endsWith('/ops'))).toBe(true)
  77. // The deployment-mounted rule on the wire: the fixture credential never
  78. // leaves the process, its surrounding prose does, and the placeholder
  79. // marks the spot — the seam itself ships no rules.
  80. const wire = JSON.stringify(captures)
  81. expect(wire).not.toContain(FIXTURE_SECRET)
  82. expect(wire).toContain(FIXTURE_PLACEHOLDER)
  83. expect(wire).toContain('prove telemetry with key')
  84. // The canonical session log is never rewritten.
  85. expect(logContent).toContain(FIXTURE_SECRET)
  86. expect(logContent).not.toContain(FIXTURE_PLACEHOLDER)
  87. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  88. })