# Session 19: JetStream Core

## Summary

JetStream engine core — initialization, API handlers, error definitions, event types, versioning, and batching. The central JetStream coordination layer.

## Scope

| Go File | Features | Feature IDs | Go LOC |
|---------|----------|-------------|--------|
| server/jetstream.go | 84 | 1368–1451 | 2,481 |
| server/jetstream_api.go | 56 | 1452–1507 | 4,269 |
| server/jetstream_errors.go | 5 | 1751–1755 | 62 |
| server/jetstream_errors_generated.go | 203 | 1756–1958 | 1,924 |
| server/jetstream_events.go | 1 | 1959 | 25 |
| server/jetstream_versioning.go | 13 | 1960–1972 | 175 |
| server/jetstream_batching.go | 12 | 1508–1519 | 568 |
| **Total** | **374** | | **9,504** |

## .NET Classes

- `JetStreamEngine` — JetStream lifecycle, enable/disable, account tracking
- `JetStreamApi` — REST-like API handlers for stream/consumer CRUD
- `JetStreamErrors` — error code registry (208 entries)
- `JetStreamEvents` — advisory event types
- `JetStreamVersioning` — feature version compatibility
- `JetStreamBatching` — batch message processing
- `JsAccount` — per-account JetStream state
- `JsOutQ` — JetStream output queue

## Test Files

| Test File | Tests | Test IDs |
|-----------|-------|----------|
| server/jetstream_test.go | 320 | 1466–1785 |
| server/jetstream_errors_test.go | 4 | 1381–1384 |
| server/jetstream_versioning_test.go | 18 | 1791–1808 |
| server/jetstream_batching_test.go | 29 | 716–744 |
| server/jetstream_jwt_test.go | 18 | 1385–1402 |
| server/jetstream_tpm_test.go | 5 | 1786–1790 |
| server/jetstream_benchmark_test.go | 12 | 745–756 |
| **Total** | **406** | |

## Dependencies

- Session 01 (Foundation Types)
- Session 03 (Configuration)
- Session 08 (Client Connection)
- Session 09 (Server Core Part 1)
- Session 11 (Accounts)
- Session 17 (Store Interfaces)

## .NET Target Location

- `dotnet/src/ZB.MOM.NatsNet.Server/JetStream/`

## Notes

- **This is a multi-sitting session** — 9.5K Go LOC and 406 tests
- JetStream errors generated file is 203 features but mostly boilerplate error codes
- jetstream_test.go has 320 tests — the largest test file
- Suggested sub-batching:
  - **19a**: Error definitions and events (209 features, 2K LOC) — mostly mechanical
  - **19b**: JetStream engine core (84 features, 2.5K LOC)
  - **19c**: JetStream API (56 features, 4.3K LOC)
  - **19d**: Versioning + batching (25 features, 743 LOC)
  - **19e**: Tests (406 tests, batched by test file)
